Changeset 1897 for trunk/LMDZ.TITAN/libf/muphytitan/argparse.F90
- Timestamp:
- Jan 24, 2018, 10:24:24 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.TITAN/libf/muphytitan/argparse.F90
r1814 r1897 1 ! Copyright Jérémie Burgalat (2010-2015 )1 ! Copyright Jérémie Burgalat (2010-2015,2017) 2 2 ! 3 ! burgalat.jeremie@gmail.com3 ! jeremie.burgalat@univ-reims.fr 4 4 ! 5 5 ! This software is a computer program whose purpose is to provide configuration … … 33 33 34 34 !! file: argparse.F90 35 !! summary: Command-line parser source file 36 !! author: burgalat37 !! date: 2013-2015 35 !! summary: Command-line parser source file. 36 !! author: J. Burgalat 37 !! date: 2013-2015,2017 38 38 39 39 #include "defined.h" … … 47 47 !! 48 48 !! If you only wish to have an overview of argparse usage, you'd better go 49 !! [p_argparse](here). 49 !! [here](|url|/page/swift/p01_argparse.html). 50 50 51 USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY : stdout=>OUTPUT_UNIT, stderr=>ERROR_UNIT 51 52 USE ERRORS 52 53 USE FSYSTEM, ONLY : fs_termsize 53 USE STRINGS, getpar => format_paragraph, & 54 splitstr => format_string, & 55 ap_string => st_string, & !! String value type ID 56 ap_complex => st_complex, & !! Complex value type ID 57 ap_logical => st_logical, & !! Logical value type ID 58 ap_integer => st_integer, & !! Integer value type ID 59 ap_real => st_real !! Floating point value type ID 54 USE STRING_OP, getpar => format_paragraph, splitstr => format_string 60 55 IMPLICIT NONE 61 56 … … 66 61 PUBLIC :: noerror,error, error_to_string,aborting 67 62 ! from strings 68 #if ! HAVE_FTNDTSTR69 63 PUBLIC :: st_slen, st_llen 70 #endif71 PUBLIC :: ap_string, ap_complex, ap_logical, ap_integer, ap_real72 64 PUBLIC :: stderr, stdout 73 65 ! argparse module … … 92 84 ! PARAMETERS (INTRISIC TYPES) 93 85 ! =========================== 86 87 INTEGER, PARAMETER, PUBLIC :: ap_string = st_string 88 !! String value type identifier. 89 INTEGER, PARAMETER, PUBLIC :: ap_complex = st_complex 90 !! Complex value type identifier. 91 INTEGER, PARAMETER, PUBLIC :: ap_logical = st_logical 92 !! Logical value type identifier. 93 INTEGER, PARAMETER, PUBLIC :: ap_integer = st_integer 94 !! Integer value type identifier. 95 INTEGER, PARAMETER, PUBLIC :: ap_real = st_real 96 !! Real value type identifier. 97 98 !> List of all available actions 94 99 95 100 INTEGER, PARAMETER, PUBLIC :: ap_store = 1 … … 120 125 121 126 !> Add an option to the parser 127 !! 128 !! ``` 129 !! FUNCTION argparser_add_option(this,dest,sflag,lflag,type,action,default,nrec,help,meta) RESULT(err) 130 !! argparser_add_option(this,dest,flag,type,action,default,nrec,help,meta) RESULT(err) 131 !! ``` 132 !! 133 !! The function defines a new argument based on input parameters, checks it and finally sets it 134 !! in the parser. 135 !! 136 !! In its first version both short (`sflag`) and long (`lflag`) options flags are mandatory. In its second 137 !! form, a single flag (`flag`) is expected: the method will automatically deduce if it belongs to short or 138 !! a long option flag based on the number of hyphens given. 139 !! 140 !! `type` value should be one of the following module constants (which are aliases from [[string_op(module)]]): 141 !! 142 !! - `ap_string` ([[string_op(module):st_string(variable)]]) 143 !! - `ap_complex` ([[string_op(module):st_complex(variable)]]) 144 !! - `ap_logical` ([[string_op(module):st_logical(variable)]]) 145 !! - `ap_integer` ([[string_op(module):st_integer(variable)]]) 146 !! - `ap_real` ([[string_op(module):st_real(variable)]]) 147 !! 148 !! `action` value should be one of the following module constants: 149 !! 150 !! - [[argparse(module):ap_store(variable)]] 151 !! - [[argparse(module):ap_append(variable)]] 152 !! - [[argparse(module):ap_count(variable)]] 153 !! - [[argparse(module):ap_help(variable)]] 154 !! 155 !! `nrec` string can take the following forms: 156 !! 157 !! tag | description 158 !! :---: | : ------------- 159 !! "?" | zero or one argument's value 160 !! "*" | any number of arguments 161 !! "+" | one or more argument's value(s) 162 !! "X" | Exactly X values. Where X is the string representation of an integer (0 is accepted). 163 !! 164 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 165 !! produced by misuse of the function arguments. In such case, the program should be 166 !! stopped: note that such error should not occur in _released_ programs. 122 167 INTERFACE argparser_add_option 123 168 MODULE PROCEDURE ap_add_option_1, ap_add_option_2 … … 131 176 !> Get optional argument value(s) 132 177 !! 133 !! This is the generic method that can be used to retrieve any kind of argument value(s) from the parser. 178 !! ``` 179 !! FUNCTION argparser_get_value(this,name,output) RESULT(err) 180 !! ``` 181 !! 182 !! This is the generic method that can be used to retrieve any kind of argument value(s) from the parser for a given 183 !! argument name (as defined by the `dest` argument of [[argparse(module):argparser_add_option(interface)]]. 134 184 !! All the methods have the same dummy arguments only `output` dummy argument differs in type and shape. 135 185 !! … … 137 187 !! For string vector, `output` is expected to be an allocatable vector of **assumed length** 138 188 !! strings (thus string length is left to user responsability). 139 !! A good compromise for strings length is to use the [[string s(module):st_slen(variable)]]189 !! A good compromise for strings length is to use the [[string_op(module):st_slen(variable)]] 140 190 !! parameter. 141 191 INTERFACE argparser_get_value … … 287 337 PROCEDURE, PRIVATE :: ap_get_sv_ve 288 338 PROCEDURE, PRIVATE :: ap_check_state 289 !PROCEDURE, PUBLIC :: reset_values => argparser_reset_values290 ! !! Resets values stored in the parser291 339 PROCEDURE, PUBLIC :: throw_error => argparser_throw_error 292 340 !! Throw an error and exit the program … … 415 463 416 464 FUNCTION argparser_add_positionals(this,nargs,meta,help) RESULT(err) 417 !! Add positional arguments definition to the parser 465 !! Add positional arguments definition to the parser. 466 !! 418 467 !! The method initializes the entry for positional arguments in the parser. 419 468 !! Positional arguments are always seen by the parser as strings and the 420 469 !! default associated action is 'store'. 421 422 470 OBJECT(argparser), INTENT(inout) :: this 423 471 !! An argparser object reference … … 533 581 IF (zauto) THEN 534 582 IF (rhelp) CALL argparser_help(this) 535 IF (err /= 0 .AND. err /= 255) CALL argparser_throw_error(this,err,2)583 IF (err /= 0) CALL argparser_throw_error(this,err,2) 536 584 ENDIF 537 585 RETURN … … 751 799 !! in the parser. Both **short and long options flags** are mandatory input arguments of the function. 752 800 !! 753 !! `type` value should be one of the following module constants (which are aliases from [[string s(module)]]):754 !! - ap_string ([[string s(module):st_string(variable)]])755 !! - ap_complex ([[string s(module):st_complex(variable)]])756 !! - ap_logical ([[string s(module):st_logical(variable)]])757 !! - ap_integer ([[string s(module):st_integer(variable)]])758 !! - ap_real ([[string s(module):st_real(variable)]])801 !! `type` value should be one of the following module constants (which are aliases from [[string_op(module)]]): 802 !! - ap_string ([[string_op(module):st_string(variable)]]) 803 !! - ap_complex ([[string_op(module):st_complex(variable)]]) 804 !! - ap_logical ([[string_op(module):st_logical(variable)]]) 805 !! - ap_integer ([[string_op(module):st_integer(variable)]]) 806 !! - ap_real ([[string_op(module):st_real(variable)]]) 759 807 !! 760 808 !! `action` value should be one of the following module constants: … … 772 820 !! X | Exactly X values. Where X is the string representation of an integer (0 is accepted). 773 821 !! 774 !! See also [[argparse(module):ap_add_option_2(function)]]documentation.822 !! See also ap_add_option_2 documentation. 775 823 OBJECT(argparser), INTENT(inout) :: this 776 824 !! An argparser object reference … … 828 876 !! Add an argument to the parser (interface #2) 829 877 !! 830 !! The function is a wrapper to [[argparse(module):ap_add_option_1(function)]]. In this version,878 !! The function is a wrapper to ap_add_option_1. In this version, 831 879 !! only one option flag is required. The method only checks for the (trimmed) length of **flag** in 832 880 !! order to choose wether it is a **short** or **long** option flag. Then the function simply calls 833 !! [[argparse(module):ap_add_option_1(function)]]to set the argument.881 !! ap_add_option_1 to set the argument. 834 882 !! 835 !! Other dummy arguments have the same meaning as in [[argparse(module):ap_add_option_1(function)]].883 !! Other dummy arguments have the same meaning as in ap_add_option_1. 836 884 OBJECT(argparser), INTENT(inout) :: this 837 885 !! An argparser object reference … … 886 934 !! An argc object to check 887 935 TYPE(error) :: err 888 !! Error object with - 15id if object is already set, no error otherwise.936 !! Error object with -8 id if object is already set, no error otherwise. 889 937 INTEGER :: i 890 938 err = noerror … … 914 962 !! An argparser object reference 915 963 TYPE(words), INTENT(inout) :: cmd 916 !! A [[string s(module):words(type)]] object with the command-line to parse964 !! A [[string_op(module):words(type)]] object with the command-line to parse 917 965 LOGICAL, INTENT(out) :: help_req 918 966 !! An output logical flag with `.true.` if help option has been found, `.false.` otherwise … … 987 1035 !! An argparser object reference 988 1036 TYPE(words), INTENT(inout) :: cmd 989 !! A [[string s(module):words(type)]] object with the command-line to parse1037 !! A [[string_op(module):words(type)]] object with the command-line to parse 990 1038 TYPE(error) :: err 991 1039 !! Error object with the first error encountered in the process … … 1068 1116 ! Gets values as a function of the expected number of records 1069 1117 IF (arg%nrec == 0) THEN 1070 ! parse_args(main parsing method) reset all values: for 0 nrec1118 ! argparser_parse (main parsing method) reset all values: for 0 nrec 1071 1119 ! we should have at least one value saved which is the default. 1072 1120 ! if it is not the case we set default as values of the argument … … 1136 1184 !! @warning 1137 1185 !! For compilers that does not support allocatable strings in derived types, 1138 !! computation are highly dependent of [[string s(module):st_slen(variable):1186 !! computation are highly dependent of [[string_op(module):st_slen(variable): 1139 1187 !! tokens length are limited by this parameter. 1140 1188 IMPLICIT NONE … … 1144 1192 !! A string to process 1145 1193 TYPE(words), INTENT(out) :: new_cmd 1146 !! An output [[string s(module):words(type)]] object with the processed command line1194 !! An output [[string_op(module):words(type)]] object with the processed command line 1147 1195 LOGICAL, INTENT(out) :: rhelp 1148 1196 !! An output boolean flag with `.true.` if help is requested, `.false.` otherwise … … 1155 1203 err = noerror ; rhelp = .false. 1156 1204 IF (LEN_TRIM(string) == 0) THEN 1157 err = error('internal error (empty string)',-2 )1205 err = error('internal error (empty string)',-255) 1158 1206 RETURN 1159 1207 ENDIF 1160 1208 ! split input command line in words ! 1161 splitted = new_words(string," ",.true.) ! tokens may be truncated :(1209 call words_extend(splitted,string," ",.true.) 1162 1210 ! reset iterator 1163 1211 CALL words_reset(splitted) … … 1330 1378 1331 1379 SUBROUTINE ap_format_usage(this,optmw) 1332 !! Format command line usage 1380 !! Format command line usage. 1381 !! 1333 1382 !! The subroutine creates and formats the command line usage of the 1334 1383 !! given argparser object. If [[argparser(type):usg(variable)]] is already set (i.e. not empty) … … 1693 1742 !! The function calls all the tests to perform on argc members. Some of these tests can 1694 1743 !! alter argc's member values to fit argparser's requirements. 1695 !! @note 1696 !! Normally, program should be stopped if returned error is not 0 ! 1744 !! 1745 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1746 !! produced by misuse of the function arguments. In such case, the program should be 1747 !! stopped: note that such ezrror should not occur in _released_ programs. 1697 1748 TYPE(argc), INTENT(inout) :: this 1698 1749 !! An argc object … … 1740 1791 !! The method checks if input argument's options are valid and update the argc object 1741 1792 !! consequently. 1793 !! 1794 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1795 !! produced by misuse of the function arguments. 1742 1796 TYPE(argc), INTENT(inout) :: this 1743 1797 !! An argc object to update … … 1756 1810 ret = noerror 1757 1811 eprf = 'argparse: '//"Invalid argument `"//TRIM(this%name)//"'" 1758 uty = error(eprf//" (type)",- 2)1759 ina = error(eprf//" (inconsistent nargs)",- 2)1812 uty = error(eprf//" (type)",-9) 1813 ina = error(eprf//" (inconsistent nargs)",-9) 1760 1814 zna = TRIM(na) 1761 1815 ! Checks action … … 1763 1817 this%paction = ac 1764 1818 ELSE 1765 ret = error(eprf//" (action)",- 2) ; RETURN1819 ret = error(eprf//" (action)",-9) ; RETURN 1766 1820 ENDIF 1767 1821 ! Checks and sets type and default as a function of the action … … 1885 1939 terr = error(eprf//" (inconsistent default value: expected '"// & 1886 1940 TRIM(st_type_names(this%ptype))//"', found '"// & 1887 TRIM(st_type_names(t))//"')",- 2)1941 TRIM(st_type_names(t))//"')",-9) 1888 1942 RETURN 1889 1943 ENDIF … … 1977 2031 SELECT CASE(this%nrec) 1978 2032 CASE(-3,-2,-1) 1979 zmeta = str_to_upper(meta(1))2033 zmeta = to_upper(meta(1)) 1980 2034 blk = INDEX(TRIM(zmeta),CHAR(32)) - 1 1981 2035 IF (blk <= 0) blk=LEN_TRIM(zmeta) … … 1987 2041 DO i=1,this%nrec 1988 2042 j=j+1 ; IF (j>ms) j=1 1989 zmeta = str_to_upper(meta(j))2043 zmeta = to_upper(meta(j)) 1990 2044 blk=INDEX(TRIM(zmeta),CHAR(32))-1 1991 2045 IF (blk <= 0) blk=LEN_TRIM(zmeta) … … 1994 2048 END SELECT 1995 2049 ELSE 1996 zmeta = str_to_upper(TRIM(this%name))2050 zmeta = to_upper(TRIM(this%name)) 1997 2051 SELECT CASE(this%nrec) 1998 2052 CASE(-3,-2,-1) … … 2223 2277 !! - -7 : argument not found (i.e. does not set in the parser) 2224 2278 !! - -19 : parsing not done yet 2225 !! - -20 : (previous)parsing failed2279 !! - -20 : parsing failed 2226 2280 !! - -21 : inconsistent destination type 2227 2281 !! @note … … 2480 2534 !! @param[in,out] this An argc object 2481 2535 !! @param[out] output A scalar with the first value of the argument 2482 !! @return An errors::error object with - 12if the destination variable's type2536 !! @return An errors::error object with -21 if the destination variable's type 2483 2537 !! is inconsistent, 0 otherwise. 2484 2538 FUNCTION ac_get_dv_sc(this, output) RESULT(ret) … … 2492 2546 !! Output value 2493 2547 TYPE(error) :: ret 2494 !! Error object with the - 12if the destination variable's type is inconsistent, 0 otherwise2548 !! Error object with the -21 if the destination variable's type is inconsistent, 0 otherwise 2495 2549 ret = noerror 2496 2550 IF (this%ptype /= ap_real) THEN
Note: See TracChangeset
for help on using the changeset viewer.