Changeset 3083 for trunk/LMDZ.TITAN/libf/muphytitan/argparse.F90
- Timestamp:
- Oct 12, 2023, 10:30:22 AM (15 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.TITAN/libf/muphytitan/argparse.F90
r1897 r3083 1 ! Copyright Jérémie Burgalat (2010-2015,2017) 2 ! 3 ! jeremie.burgalat@univ-reims.fr 4 ! 5 ! This software is a computer program whose purpose is to provide configuration 6 ! file and command line arguments parsing features to Fortran programs. 7 ! 8 ! This software is governed by the CeCILL-B license under French law and 9 ! abiding by the rules of distribution of free software. You can use, 10 ! modify and/ or redistribute the software under the terms of the CeCILL-B 11 ! license as circulated by CEA, CNRS and INRIA at the following URL 12 ! "http://www.cecill.info". 13 ! 14 ! As a counterpart to the access to the source code and rights to copy, 15 ! modify and redistribute granted by the license, users are provided only 16 ! with a limited warranty and the software's author, the holder of the 17 ! economic rights, and the successive licensors have only limited 18 ! liability. 19 ! 20 ! In this respect, the user's attention is drawn to the risks associated 21 ! with loading, using, modifying and/or developing or reproducing the 22 ! software by the user in light of its specific status of free software, 23 ! that may mean that it is complicated to manipulate, and that also 24 ! therefore means that it is reserved for developers and experienced 25 ! professionals having in-depth computer knowledge. Users are therefore 26 ! encouraged to load and test the software's suitability as regards their 27 ! requirements in conditions enabling the security of their systems and/or 28 ! data to be ensured and, more generally, to use and operate it in the 29 ! same conditions as regards security. 30 ! 31 ! The fact that you are presently reading this means that you have had 32 ! knowledge of the CeCILL-B license and that you accept its terms. 1 ! Copyright (c) (2013-2015,2017) Jeremie Burgalat (jeremie.burgalat@univ-reims.fr). 2 ! 3 ! This file is part of SWIFT 4 ! 5 ! Permission is hereby granted, free of charge, to any person obtaining a copy of 6 ! this software and associated documentation files (the "Software"), to deal in 7 ! the Software without restriction, including without limitation the rights to 8 ! use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 ! the Software, and to permit persons to whom the Software is furnished to do so, 10 ! subject to the following conditions: 11 ! 12 ! The above copyright notice and this permission notice shall be included in all 13 ! copies or substantial portions of the Software. 14 ! 15 ! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 ! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 ! FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 ! COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 ! IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 ! CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 21 34 22 !! file: argparse.F90 35 23 !! summary: Command-line parser source file. 36 24 !! author: J. Burgalat 37 !! date: 2013-2015,2017 25 !! date: 2013-2015,2017,2022 38 26 39 27 #include "defined.h" … … 45 33 !! For your own sanity, private methods that call Ancient Gods powers through 46 34 !! evil black magic rituals are not described here. 47 !! 35 !! 48 36 !! If you only wish to have an overview of argparse usage, you'd better go 49 37 !! [here](|url|/page/swift/p01_argparse.html). … … 52 40 USE ERRORS 53 41 USE FSYSTEM, ONLY : fs_termsize 54 USE STRING_OP, getpar => format_paragraph, splitstr => format_string 42 USE STRING_OP, getpar => format_paragraph, splitstr => format_string 55 43 IMPLICIT NONE 56 44 … … 68 56 argparser_add_option, & 69 57 argparser_add_positionals, & 70 argparser_throw_error, & 58 argparser_throw_error, & 71 59 argparser_parse, & 72 60 argparser_help, & … … 85 73 ! =========================== 86 74 87 INTEGER, PARAMETER, PUBLIC :: ap_string = st_string 75 INTEGER, PARAMETER, PUBLIC :: ap_string = st_string 88 76 !! String value type identifier. 89 77 INTEGER, PARAMETER, PUBLIC :: ap_complex = st_complex … … 98 86 !> List of all available actions 99 87 100 INTEGER, PARAMETER, PUBLIC :: ap_store = 188 INTEGER, PARAMETER, PUBLIC :: ap_store = 1 101 89 !! store action ID : Each time the option is seen, values are replaced. 102 INTEGER, PARAMETER, PUBLIC :: ap_append = 2103 !! append action ID : Each time the option is seen, values are appended. 104 INTEGER, PARAMETER, PUBLIC :: ap_count = 390 INTEGER, PARAMETER, PUBLIC :: ap_append = 2 91 !! append action ID : Each time the option is seen, values are appended. 92 INTEGER, PARAMETER, PUBLIC :: ap_count = 3 105 93 !! count action ID : increase a counter each time the option is seen. 106 INTEGER, PARAMETER, PUBLIC :: ap_help = 494 INTEGER, PARAMETER, PUBLIC :: ap_help = 4 107 95 !! help action ID : help is requested ! 96 INTEGER, PARAMETER, PUBLIC :: ap_version = 5 97 !! version action ID : version is requested ! 108 98 109 99 !> List of all available actions 110 INTEGER, DIMENSION( 4), PARAMETER, PRIVATE :: ap_actions = (/ap_store, &100 INTEGER, DIMENSION(5), PARAMETER, PRIVATE :: ap_actions = (/ap_store, & 111 101 ap_append, & 112 102 ap_count, & 113 ap_help/) 103 ap_help, & 104 ap_version/) 114 105 !> List of all recognized types by the parser 115 INTEGER, DIMENSION(5), PARAMETER, PRIVATE :: ap_types = (/ap_string, & 106 INTEGER, DIMENSION(5), PARAMETER, PRIVATE :: ap_types = (/ap_string, & 116 107 ap_logical, & 117 108 ap_complex, & 118 109 ap_integer, & 119 ap_real/) 110 ap_real/) 120 111 !> The unknown flag 121 112 !! 122 !! This flag is only intended to initialize flags. It is set by default during initialization 113 !! This flag is only intended to initialize flags. It is set by default during initialization 123 114 !! and quielty replaced by default flags, if user does not provide the relevant feature. 124 115 INTEGER, PARAMETER :: ap_undef = -1 125 116 126 117 !> Add an option to the parser 127 118 !! … … 131 122 !! ``` 132 123 !! 133 !! The function defines a new argument based on input parameters, checks it and finally sets it 134 !! in the parser. 135 !! 124 !! The function defines a new argument based on input parameters, checks it and finally sets it 125 !! in the parser. 126 !! 136 127 !! In its first version both short (`sflag`) and long (`lflag`) options flags are mandatory. In its second 137 128 !! form, a single flag (`flag`) is expected: the method will automatically deduce if it belongs to short or 138 129 !! a long option flag based on the number of hyphens given. 139 !! 130 !! 140 131 !! `type` value should be one of the following module constants (which are aliases from [[string_op(module)]]): 141 132 !! … … 145 136 !! - `ap_integer` ([[string_op(module):st_integer(variable)]]) 146 137 !! - `ap_real` ([[string_op(module):st_real(variable)]]) 147 !! 138 !! 148 139 !! `action` value should be one of the following module constants: 149 140 !! … … 162 153 !! "X" | Exactly X values. Where X is the string representation of an integer (0 is accepted). 163 154 !! 164 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 155 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 165 156 !! produced by misuse of the function arguments. In such case, the program should be 166 157 !! stopped: note that such error should not occur in _released_ programs. … … 168 159 MODULE PROCEDURE ap_add_option_1, ap_add_option_2 169 160 END INTERFACE 170 161 171 162 !> Get positional argument value(s) 172 163 INTERFACE argparser_get_positional 173 164 MODULE PROCEDURE ap_get_positional_sc, ap_get_positional_ve 174 END INTERFACE 165 END INTERFACE 175 166 176 167 !> Get optional argument value(s) … … 179 170 !! FUNCTION argparser_get_value(this,name,output) RESULT(err) 180 171 !! ``` 181 !! 172 !! 182 173 !! This is the generic method that can be used to retrieve any kind of argument value(s) from the parser for a given 183 174 !! argument name (as defined by the `dest` argument of [[argparse(module):argparser_add_option(interface)]]. 184 175 !! All the methods have the same dummy arguments only `output` dummy argument differs in type and shape. 185 !! 176 !! 186 177 !! @note 187 178 !! For string vector, `output` is expected to be an allocatable vector of **assumed length** 188 179 !! strings (thus string length is left to user responsability). 189 !! A good compromise for strings length is to use the [[string_op(module):st_slen(variable)]] 180 !! A good compromise for strings length is to use the [[string_op(module):st_slen(variable)]] 190 181 !! parameter. 191 182 INTERFACE argparser_get_value … … 197 188 !> Interface to [[argparse(module):argc(type)]] getters 198 189 !! 199 !! All the functions have the same prototype, only kind and type of arguments changed 190 !! All the functions have the same prototype, only kind and type of arguments changed 200 191 !! from a function to the other. 201 INTERFACE argc_get_value 192 INTERFACE argc_get_value 202 193 MODULE PROCEDURE ac_get_dv_sc, ac_get_rv_sc, ac_get_iv_sc, ac_get_lv_sc, & 203 194 ac_get_cv_sc, ac_get_sv_sc, ac_get_dv_ve, ac_get_rv_ve, & … … 230 221 !! Defines a command-line argument. 231 222 !! 232 !! An [[argparse(module):argc(type)]] object stores all information about a command-line 223 !! An [[argparse(module):argc(type)]] object stores all information about a command-line 233 224 !! argument, that is: 234 225 !! 235 226 !! - its name 236 !! - its optional flags 227 !! - its optional flags 237 228 !! - its type 238 229 !! - its action … … 253 244 TYPE(words) :: meta 254 245 !! Meta variable name(s) of the argument 255 #if HAVE_FTNDTSTR 246 #if HAVE_FTNDTSTR 256 247 CHARACTER(len=:), ALLOCATABLE :: default 257 !! Default flag 248 !! Default flag 258 249 CHARACTER(len=:), ALLOCATABLE :: name 259 250 !! Name of the argument (needed to check and retrieve its value(s)) 260 CHARACTER(len=:), ALLOCATABLE :: lflag 251 CHARACTER(len=:), ALLOCATABLE :: lflag 261 252 !! Long flag option (st_short_len max chars !) 262 253 CHARACTER(len=:), ALLOCATABLE :: help … … 264 255 #else 265 256 CHARACTER(len=st_slen) :: default = "" 266 !! Default flag 257 !! Default flag 267 258 CHARACTER(len=st_slen) :: name 268 259 !! Name of the argument (needed to check and retrieve its value(s)) … … 278 269 !! Command-line parser 279 270 !! 280 !! This is the main object of the module. It stores definitions of CLI arguments and 271 !! This is the main object of the module. It stores definitions of CLI arguments and 281 272 !! their value(s) once the command-line have been parsed. 282 273 TYPE(argc), PRIVATE, ALLOCATABLE, DIMENSION(:) :: args … … 298 289 #if HAVE_FTNDTSTR 299 290 CHARACTER(len=:), PRIVATE, ALLOCATABLE :: usg 300 !! Program command usage 291 !! Program command usage 301 292 CHARACTER(len=:), PRIVATE, ALLOCATABLE :: descr 302 293 !! Program help description 303 CHARACTER(len=:), PRIVATE, ALLOCATABLE :: eplg 294 CHARACTER(len=:), PRIVATE, ALLOCATABLE :: eplg 304 295 !! Program help epilog 296 CHARACTER(len=:), PRIVATE, ALLOCATABLE :: vers 297 !! Application version string 305 298 #else 306 299 CHARACTER(len=st_llen), PRIVATE :: usg 307 !! Program command usage 300 !! Program command usage 308 301 CHARACTER(len=st_llen), PRIVATE :: descr 309 302 !! Program help description 310 CHARACTER(len=st_llen), PRIVATE :: eplg 303 CHARACTER(len=st_llen), PRIVATE :: eplg 311 304 !! Program help epilog 305 CHARACTER(len=st_llen), PRIVATE :: vers 306 !! Application version string 312 307 #endif 313 INTEGER, PRIVATE :: mxhlpos = 20 308 INTEGER, PRIVATE :: mxhlpos = 20 314 309 !! Position of the short help for options 315 310 INTEGER, PRIVATE :: width = 0 316 !! Maximum width of the help 311 !! Maximum width of the help 317 312 LOGICAL, PRIVATE :: init = .false. 318 313 !! Initialization control flag 319 #if HAVE_FTNPROC 314 #if HAVE_FTNPROC 320 315 321 316 CONTAINS … … 341 336 PROCEDURE, PUBLIC :: parse => argparser_parse 342 337 !! Parse the command-line (or the given input string). 343 PROCEDURE, PUBLIC :: help => argparser_help 338 PROCEDURE, PUBLIC :: help => argparser_help 344 339 !! Compute and print help 340 PROCEDURE, PUBLIC :: version => argparser_version 341 !! print version string 345 342 PROCEDURE, PUBLIC :: found => argparser_found 346 343 !! Check if an optional argument has been found on the command-line … … 356 353 GENERIC, PUBLIC :: add_option => ap_add_option_1, & 357 354 ap_add_option_2 358 !> Get the values of the positionals stored in the parser. 355 !> Get the values of the positionals stored in the parser. 359 356 GENERIC, PUBLIC :: get_positional => ap_get_positional_sc, & 360 357 ap_get_positional_ve … … 372 369 ap_get_cv_ve, & 373 370 ap_get_sv_ve 374 #endif 371 #endif 375 372 END TYPE argparser 376 373 … … 380 377 ! ------------------------------- 381 378 382 FUNCTION new_argparser(usg, dsc, epg, add_help, width, max_help_pos) RESULT(this)379 FUNCTION new_argparser(usg, dsc, epg, add_help, add_version, version_string,width, max_help_pos) RESULT(this) 383 380 !! Initialize an argparser object. 384 !! 381 !! 385 382 !! The method initializes (properly) an [[argparse(module):argparser(type)]] object. 386 !! Even if all the arguments are optional, it is mandatory to **call** the method 383 !! Even if all the arguments are optional, it is mandatory to **call** the method 387 384 !! before using an argparser object. 388 385 CHARACTER(len=*), INTENT(in), OPTIONAL :: usg … … 394 391 !! An optional string with the epilog of the program's help 395 392 LOGICAL, INTENT(in), OPTIONAL :: add_help 396 !! An optional boolean flag with `.true.` to automatically set an option for program's help. 393 !! An optional boolean flag with `.true.` to automatically set an option for program's help. 397 394 !! Note that, the option flags `-h` and `--help` are no more available in that case. 395 LOGICAL, INTENT(in), OPTIONAL :: add_version 396 !! An optional boolean flag with `.true.` to automatically set an option for program's help. 397 !! Note that, the option flags `-V` and `--version` are no more available in that case. 398 CHARACTER(len=*), INTENT(in), OPTIONAL :: version_string 399 !! An optional string with the text to display if the *version* option is found on the 400 !! command line. The text is displayed without any format. 398 401 INTEGER, INTENT(in), OPTIONAL :: width 399 402 !! An optional integer with the maximum width the help text. 400 403 INTEGER, INTENT(in), OPTIONAL :: max_help_pos 401 !! An optional integer with the maximum position of the help string for each option of 402 !! the program when help is requested. Note that this value is just an indicator. The 403 !! helper computes the minimum position between this value and the maximum length of the 404 !! An optional integer with the maximum position of the help string for each option of 405 !! the program when help is requested. Note that this value is just an indicator. The 406 !! helper computes the minimum position between this value and the maximum length of the 404 407 !! options flags. 405 TYPE(argparser) :: this 408 TYPE(argparser) :: this 406 409 !! An initialized argparse object. 407 410 INTEGER :: zh … … 413 416 IF (PRESENT(dsc)) THEN ; this%descr=dsc ; ELSE ; this%descr='' ; ENDIF 414 417 IF (PRESENT(epg)) THEN ; this%eplg=epg ; ELSE ; this%eplg='' ; ENDIF 415 CALL fs_termsize(zh,this%width) 418 CALL fs_termsize(zh,this%width) 416 419 IF (PRESENT(width)) this%width = MAX(width,50) 417 420 IF(PRESENT(max_help_pos)) this%mxhlpos = MAX(5,max_help_pos) … … 422 425 action=ap_help, help="Print this help and quit") 423 426 ENDIF 427 IF (PRESENT(add_version)) THEN 428 IF (add_version) & 429 err = argparser_add_option(this,'version',sflag='-V',lflag='--version', & 430 action=ap_version, help="Print the application version and quit") 431 this%vers = '' 432 if (PRESENT(version_string)) this%vers = TRIM(version_string) 433 ENDIF 424 434 RETURN 425 435 END FUNCTION new_argparser … … 432 442 !! @note If **fccp** has not been built with support for finalization subroutine, 433 443 !! it should be called whenever the argparser object is no more used. 434 TYPE(argparser), INTENT(inout) :: this 444 TYPE(argparser), INTENT(inout) :: this 435 445 !! An argparser object 436 446 IF (ALLOCATED(this%args)) THEN … … 466 476 !! 467 477 !! The method initializes the entry for positional arguments in the parser. 468 !! Positional arguments are always seen by the parser as strings and the 478 !! Positional arguments are always seen by the parser as strings and the 469 479 !! default associated action is 'store'. 470 480 OBJECT(argparser), INTENT(inout) :: this … … 475 485 !! A vector of strings with the the displayed value name(s) of the positionals in the help command 476 486 CHARACTER(len=*), INTENT(in), OPTIONAL :: help 477 !! An optional string with a short description of the positional argument(s) 487 !! An optional string with a short description of the positional argument(s) 478 488 TYPE(error) :: err 479 489 !! Error object with the first error encountered in the process. … … 481 491 CHARACTER(len=:), ALLOCATABLE :: sf,lf,de 482 492 err = noerror 483 IF (.NOT.this%init) THEN 484 err = error("argparse: parser not initialized yet",-1) 493 IF (.NOT.this%init) THEN 494 err = error("argparse: parser not initialized yet",-1) 485 495 RETURN 486 496 ENDIF … … 504 514 RETURN 505 515 ENDIF 506 this%have_posal = this%posals%nrec /= 0 516 this%have_posal = this%posals%nrec /= 0 507 517 ENDIF 508 518 RETURN … … 514 524 !! given as optional argument and fills the parser's arguments. 515 525 !! @note 516 !! If `cmd_line` is provided it should not contains the name of the program or 517 !! the parsing process will certainly failed: program name will be seen as the 518 !! first positional argument and all tokens of the string will then be seen as 526 !! If `cmd_line` is provided it should not contains the name of the program or 527 !! the parsing process will certainly failed: program name will be seen as the 528 !! first positional argument and all tokens of the string will then be seen as 519 529 !! positional. 520 530 OBJECT(argparser), INTENT(inout) :: this … … 523 533 !! An optional string to parse that substitute for the actual command-line. 524 534 LOGICAL, INTENT(in), OPTIONAL :: auto 525 !! An optional boolean flag with `.true.` to instruct the parser wether to perform 535 !! An optional boolean flag with `.true.` to instruct the parser wether to perform 526 536 !! automatic actions or not when error occur during parsing. If `auto` is enabled, 527 537 !! then the parser dumps program's usage and stops the program on error. … … 530 540 CHARACTER(len=:), ALLOCATABLE :: cline,z 531 541 LOGICAL :: zauto 532 LOGICAL :: rhelp 533 INTEGER :: l 542 LOGICAL :: rhelp 543 LOGICAL :: rvers 544 INTEGER :: l 534 545 TYPE(words) :: cmd_tokens 535 546 err = noerror … … 537 548 err = error("parser not initialized yet",-1) ; RETURN 538 549 ENDIF 539 rhelp = .false. 550 rhelp = .false. ; rvers = .false. 540 551 zauto = .false. ; IF (PRESENT(auto)) zauto = auto 541 552 IF (PRESENT(cmd_line)) THEN 542 553 ALLOCATE(cline,source=cmd_line) 543 554 ELSE 544 CALL GET_COMMAND(length=l) 555 CALL GET_COMMAND(length=l) 545 556 ALLOCATE(CHARACTER(len=l) :: z) ; CALL GET_COMMAND(z) 546 557 CALL GET_COMMAND_ARGUMENT(0,length=l) … … 557 568 EXIT ! ... No :) 558 569 ELSE 559 err = ap_split_cmd(this,cline,cmd_tokens,rhelp) 570 err = ap_split_cmd(this,cline,cmd_tokens,rhelp) 560 571 ! we only stops processing if : 561 572 ! - the internal error (string length) is raised … … 565 576 CALL words_reset(cmd_tokens) ! not mandatory... at least theoretically 566 577 ! Parses the options 567 err = ap_parse_options(this,cmd_tokens,rhelp )578 err = ap_parse_options(this,cmd_tokens,rhelp,rvers) 568 579 IF (err /= noerror) EXIT 569 ! exit loop if help is requested. Parser is not completely filled but we570 ! expect someone to use the help action..571 IF (rhelp ) EXIT580 ! exit loop if help or version is requested. Parser is not completely filled but we 581 ! expect someone to use the help or version actions... 582 IF (rhelp.OR.rvers) EXIT 572 583 ! Parses positionals 573 err = ap_parse_positionals(this,cmd_tokens) 584 err = ap_parse_positionals(this,cmd_tokens) 574 585 EXIT ! A one iterated loop :) 575 586 ENDDO … … 581 592 IF (zauto) THEN 582 593 IF (rhelp) CALL argparser_help(this) 594 IF (rvers) CALL argparser_version(this) 583 595 IF (err /= 0) CALL argparser_throw_error(this,err,2) 584 596 ENDIF 585 RETURN 597 RETURN 586 598 END FUNCTION argparser_parse 599 600 SUBROUTINE argparser_version(this) 601 !! Print version string and exit program 602 OBJECT(argparser), INTENT(inout) :: this 603 !! An argparser object reference 604 WRITE(stdout,'(a)') TRIM(this%vers) 605 CALL argparser_clear(this) 606 CALL EXIT(0) 607 END SUBROUTINE argparser_version 587 608 588 609 SUBROUTINE argparser_help(this) … … 606 627 !! 607 628 !! The method performs the following actions: 608 !! 629 !! 609 630 !! - Print the usage command of the program 610 631 !! - Dump the provided @p error message … … 614 635 !! 615 636 !! The error message is always printed in standard error output. 616 !! @note 637 !! @note 617 638 !! If errors::error::id is 0 the method does nothing. 618 639 OBJECT(argparser), INTENT(inout) :: this … … 638 659 FUNCTION argparser_found(this,argname) RESULT(found) 639 660 !! Check wether an argument has been found in the command-line. 640 !! @note 661 !! @note 641 662 !! Keep in mind that arguments in the parser always have a default 642 663 !! value. This method is not intended to check if an argument has a value but … … 663 684 CHARACTER(len=*), INTENT(in) :: argname 664 685 !! Name of the argument to check. 665 INTEGER :: num 686 INTEGER :: num 666 687 !! The number of actual values stored in the argument 667 688 INTEGER :: idx … … 693 714 INTEGER :: ret 694 715 !! The number of actual positionals arguments 695 ret = 0 716 ret = 0 696 717 IF (this%have_posal) THEN 697 718 ret = words_length(this%posals%values) 698 719 ENDIF 699 END FUNCTION argparser_get_num_positional 720 END FUNCTION argparser_get_num_positional 700 721 701 722 ! argparser private methods … … 703 724 704 725 FUNCTION ap_check_state(this) RESULT(err) 705 !! Check current parser state 726 !! Check current parser state 706 727 !! The method returns an error based on the current parser's state: 707 728 !! - Parser is ready (0) … … 712 733 !! An argparser object reference 713 734 TYPE(error) :: err 714 !! Error object with the *status* of the parser 735 !! Error object with the *status* of the parser 715 736 err = noerror 716 737 IF (this%parsed == -1) THEN … … 719 740 err = error("argparse: command-line parsing failed",-20) 720 741 ELSE IF (.NOT.this%init) THEN 721 err = error("argparse: parser not initialized yet",-1) 742 err = error("argparse: parser not initialized yet",-1) 722 743 ENDIF 723 744 RETURN … … 732 753 INTEGER :: i 733 754 TYPE(argc), ALLOCATABLE, DIMENSION(:) :: tmp 734 TYPE(error) :: err 755 TYPE(error) :: err 735 756 IF (.NOT.this%init) THEN 736 757 err = error("parser not initialized yet",-1) ; RETURN … … 739 760 IF (this%nargs == 0) THEN 740 761 ALLOCATE(this%args(1)) 741 this%args(1) = arg 742 this%nargs = 1 762 this%args(1) = arg 763 this%nargs = 1 743 764 RETURN 744 765 ENDIF … … 749 770 ALLOCATE(tmp(this%nargs)) 750 771 DO i=1,this%nargs ; tmp(i) = this%args(i) ; ENDDO 751 CALL clear_argc(this%args) 752 DEALLOCATE(this%args) 772 CALL clear_argc(this%args) 773 DEALLOCATE(this%args) 753 774 this%nargs = this%nargs+1 ; ALLOCATE(this%args(this%nargs)) 754 775 DO i=1,this%nargs-1 ; this%args(i) = tmp(i) ; ENDDO … … 782 803 ! empty parser 783 804 IF (this%nargs == 0) RETURN 784 DO i=1, this%nargs 805 DO i=1, this%nargs 785 806 IF ((nn /= 0 .AND. TRIM(this%args(i)%name) == TRIM(lna)) .OR. & 786 807 (ns /= 0 .AND. TRIM(this%args(i)%sflag) == TRIM(lsf)) .OR. & … … 796 817 !! Add an argument to the parser (interface #1) 797 818 !! 798 !! The function defines a new argument based on input parameters, checks it and finally sets it 819 !! The function defines a new argument based on input parameters, checks it and finally sets it 799 820 !! in the parser. Both **short and long options flags** are mandatory input arguments of the function. 800 !! 821 !! 801 822 !! `type` value should be one of the following module constants (which are aliases from [[string_op(module)]]): 802 823 !! - ap_string ([[string_op(module):st_string(variable)]]) … … 805 826 !! - ap_integer ([[string_op(module):st_integer(variable)]]) 806 827 !! - ap_real ([[string_op(module):st_real(variable)]]) 807 !! 828 !! 808 829 !! `action` value should be one of the following module constants: 809 830 !! - [[argparse(module):ap_store(variable)]] … … 830 851 !! A string (3 characters minimum) with the long option flag of the argument 831 852 INTEGER, INTENT(in), OPTIONAL :: type 832 !! An integer with the type of the argument 853 !! An integer with the type of the argument 833 854 INTEGER, INTENT(in), OPTIONAL :: action 834 !! An integer with the action of the argument 855 !! An integer with the action of the argument 835 856 CHARACTER(len=*), INTENT(in), OPTIONAL :: default 836 857 !! A string with the default value of the argument if not provided in the CLI 837 858 CHARACTER(len=*), INTENT(in), OPTIONAL :: nrec 838 !! A string with the expected number of specified values for the argument in the CLI. 859 !! A string with the expected number of specified values for the argument in the CLI. 839 860 CHARACTER(len=*), INTENT(in), OPTIONAL :: help 840 861 !! A string with a short description of the argument … … 853 874 de ='' ; IF (PRESENT(default)) de = TRIM(default) 854 875 IF (.NOT.this%init) THEN 855 err = error("argparse: parser not initialized yet",-1) 876 err = error("argparse: parser not initialized yet",-1) 856 877 RETURN 857 878 ENDIF … … 876 897 !! Add an argument to the parser (interface #2) 877 898 !! 878 !! The function is a wrapper to ap_add_option_1. In this version, 899 !! The function is a wrapper to ap_add_option_1. In this version, 879 900 !! only one option flag is required. The method only checks for the (trimmed) length of **flag** in 880 !! order to choose wether it is a **short** or **long** option flag. Then the function simply calls 901 !! order to choose wether it is a **short** or **long** option flag. Then the function simply calls 881 902 !! ap_add_option_1 to set the argument. 882 !! 903 !! 883 904 !! Other dummy arguments have the same meaning as in ap_add_option_1. 884 905 OBJECT(argparser), INTENT(inout) :: this … … 891 912 !! A string with the type of the argument 892 913 INTEGER, INTENT(in), OPTIONAL :: action 893 !! A string with the action of the argument 914 !! A string with the action of the argument 894 915 CHARACTER(len=*), INTENT(in), OPTIONAL :: default 895 916 !! A string with the default value of the argument if not provided in the CLI 896 917 CHARACTER(len=*), INTENT(in), OPTIONAL :: nrec 897 !! A string with the expected number of specified values for the argument in the CLI. 918 !! A string with the expected number of specified values for the argument in the CLI. 898 919 CHARACTER(len=*), INTENT(in), OPTIONAL :: help 899 920 !! A string with a short description of the argument … … 956 977 END FUNCTION ap_check_in_parser 957 978 958 FUNCTION ap_parse_options(this,cmd,help_req ) RESULT(err)979 FUNCTION ap_parse_options(this,cmd,help_req,vers_req) RESULT(err) 959 980 !! Parse options of the internal command line 960 !! This (internal) function manages the parsing of the command line options. 981 !! This (internal) function manages the parsing of the command line options. 961 982 OBJECT(argparser), INTENT(inout), TARGET :: this 962 983 !! An argparser object reference … … 965 986 LOGICAL, INTENT(out) :: help_req 966 987 !! An output logical flag with `.true.` if help option has been found, `.false.` otherwise 988 LOGICAL, INTENT(out) :: vers_req 989 !! An output logical flag with `.true.` if version option has been found, `.false.` otherwise 967 990 TYPE(error) :: err 968 991 !! Error object with the first error encountered in the process … … 976 999 ! get current element 977 1000 elt = words_current(cmd) 978 ! check element kind: is it an option flag (-1/0) or a value (1)?1001 ! check element kind: is it an option flag (-1/0) or a value (1)? 979 1002 ic = ap_check_string(this,elt,arg_idx) 980 1003 IF (ic <= 0) THEN 981 1004 IF (arg_idx /= -1) THEN 982 1005 err = ap_fill_argument(this,cmd,this%args(arg_idx)) 983 IF (err == 0 .AND. this%args(arg_idx)%paction == ap_help) THEN 984 this%parsed = 1 985 err = argparser_get_value(this,'help',help_req) 986 EXIT 1006 IF (err == 0) THEN 1007 IF (this%args(arg_idx)%paction == ap_help) THEN 1008 this%parsed = 1 1009 err = argparser_get_value(this,'help',help_req) 1010 EXIT 1011 ELSE IF (this%args(arg_idx)%paction == ap_version) THEN 1012 this%parsed = 1 1013 err = argparser_get_value(this,'version',vers_req) 1014 EXIT 1015 ENDIF 987 1016 ENDIF 988 1017 IF (err /= 0) EXIT … … 998 1027 ! iterates to next value 999 1028 CALL words_next(cmd) 1000 ENDDO 1029 ENDDO 1001 1030 1002 1031 ! Do we need to check for error here ? … … 1020 1049 IF (this%args(i)%nrec < nv) THEN 1021 1050 err = ac_fmt_val_err(this%args(i),-18) ! extra values 1022 ELSE 1051 ELSE 1023 1052 err = ac_fmt_val_err(this%args(i),-17) ! missing values 1024 1053 ENDIF 1025 ENDIF 1054 ENDIF 1026 1055 ENDDO 1027 1056 IF (err /= 0) this%parsed = 0 1028 1057 RETURN 1029 END FUNCTION ap_parse_options 1058 END FUNCTION ap_parse_options 1030 1059 1031 1060 FUNCTION ap_parse_positionals(this,cmd) RESULT(err) 1032 1061 !! Parse positional arguments of the internal command line 1033 !! This (internal) function manages the parsing of the command line positional arguments. 1062 !! This (internal) function manages the parsing of the command line positional arguments. 1034 1063 OBJECT(argparser), INTENT(inout) :: this 1035 1064 !! An argparser object reference … … 1044 1073 1045 1074 ! no positional required but current word is valid 1046 ! Either : no positional required but valid element is present 1075 ! Either : no positional required but valid element is present 1047 1076 ! Or : positional required but no valid element is present 1048 1077 IF ((this%have_posal.AND..NOT.words_valid(cmd)) .OR. & … … 1054 1083 CALL words_clear(this%posals%values) 1055 1084 this%posals%fnd = .true. 1056 DO 1085 DO 1057 1086 na = words_length(this%posals%values) 1058 1087 IF (words_valid(cmd)) THEN … … 1090 1119 !! Fill an argument with values 1091 1120 !! 1092 !! The function parses remaining parts of the command line from the position of 1093 !! the given argument and attempts to retrieve its value(s) (if any). 1121 !! The function parses remaining parts of the command line from the position of 1122 !! the given argument and attempts to retrieve its value(s) (if any). 1094 1123 !! Several tests are performed and may raise errors. The function always stops 1095 1124 !! at the first error encountered which can be one of the following : … … 1103 1132 !! The command line 1104 1133 TYPE(argc), INTENT(inout), TARGET :: arg 1105 !! An argc object reference with the argument currently processed. 1134 !! An argc object reference with the argument currently processed. 1106 1135 TYPE(error) :: err 1107 1136 !! Error object with the first error encountered in the process 1108 INTEGER :: ca, isopt, itmp 1137 INTEGER :: ca, isopt, itmp 1109 1138 LOGICAL :: ltmp 1110 1139 CHARACTER(len=:), ALLOCATABLE :: elt … … 1126 1155 IF (arg%paction == ap_count) THEN 1127 1156 elt = words_pop(arg%values) 1128 READ(elt,*) itmp ; itmp = itmp + 1 1157 READ(elt,*) itmp ; itmp = itmp + 1 1129 1158 CALL words_append(arg%values,TO_STRING(itmp)) 1130 ELSE IF (arg%ptype == ap_logical) THEN 1159 ELSE IF (arg%ptype == ap_logical) THEN 1131 1160 elt = words_pop(arg%values) 1132 READ(elt,*) ltmp 1161 READ(elt,*) ltmp 1133 1162 CALL words_append(arg%values,TO_STRING(.NOT.ltmp)) 1134 1163 ENDIF 1135 1164 ELSE 1136 1165 ! For any other case, the algorithm is quite simple : 1137 ! We consume tokens of the command-line until either the end or 1166 ! We consume tokens of the command-line until either the end or 1138 1167 ! the next option (or '--' separator) 1139 1168 ! When the exit condition is met we perform some tests based on the … … 1155 1184 ! 1) we have consumed all argument of command line 1156 1185 ! 2) current argument is not a value ! 1157 ! 3) current argument the separator '--' 1186 ! 3) current argument the separator '--' 1158 1187 IF (isopt <= 0 .OR. TRIM(elt)=='--') CALL words_previous(cmd) 1159 IF (arg%nrec == -2 .AND. words_length(arg%values) == 0) & 1188 IF (arg%nrec == -2 .AND. words_length(arg%values) == 0) & 1160 1189 err = ac_fmt_val_err(arg,-17) 1161 1190 IF (arg%paction /= ap_append .AND. arg%nrec > 0 .AND. ca /= arg%nrec) & … … 1178 1207 END FUNCTION ap_fill_argument 1179 1208 1180 FUNCTION ap_split_cmd(this,string,new_cmd,rhelp) RESULT(err) 1209 FUNCTION ap_split_cmd(this,string,new_cmd,rhelp) RESULT(err) 1181 1210 !! Preprocess the command line 1182 !! The function reads and splits the given string so merged options/values 1211 !! The function reads and splits the given string so merged options/values 1183 1212 !! are splitted and saves the resulting string elements in a list of words. 1184 1213 !! @warning 1185 1214 !! For compilers that does not support allocatable strings in derived types, 1186 1215 !! computation are highly dependent of [[string_op(module):st_slen(variable): 1187 !! tokens length are limited by this parameter. 1216 !! tokens length are limited by this parameter. 1188 1217 IMPLICIT NONE 1189 1218 OBJECT(argparser), INTENT(in) :: this … … 1193 1222 TYPE(words), INTENT(out) :: new_cmd 1194 1223 !! An output [[string_op(module):words(type)]] object with the processed command line 1195 LOGICAL, INTENT(out) :: rhelp 1224 LOGICAL, INTENT(out) :: rhelp 1196 1225 !! An output boolean flag with `.true.` if help is requested, `.false.` otherwise 1197 1226 TYPE(error) :: err … … 1199 1228 INTEGER :: isopt,j,tl,res 1200 1229 CHARACTER(len=:), ALLOCATABLE :: elt 1201 TYPE(words) :: splitted 1230 TYPE(words) :: splitted 1202 1231 INTEGER :: arg_idx 1203 1232 err = noerror ; rhelp = .false. 1204 1233 IF (LEN_TRIM(string) == 0) THEN 1205 err = error('internal error (empty string)',-255) 1234 err = error('internal error (empty string)',-255) 1206 1235 RETURN 1207 1236 ENDIF … … 1218 1247 res = ap_check_string(this,"-"//elt(j:j),arg_idx) 1219 1248 ! great we have another short option flag 1220 IF (res == -1 .AND. arg_idx /= -1) THEN ! another short option ! 1249 IF (res == -1 .AND. arg_idx /= -1) THEN ! another short option ! 1221 1250 rhelp = (this%args(arg_idx)%paction == ap_help.OR.rhelp) 1222 1251 ! we must not set some argument's values here for help argument 1223 ! if auto is not set the parse method will disable the option 1252 ! if auto is not set the parse method will disable the option 1224 1253 ! during next! parsing process 1225 1254 CALL words_append(new_cmd,"-"//elt(j:j)) … … 1237 1266 rhelp = (this%args(arg_idx)%paction == ap_help.OR.rhelp) 1238 1267 ! we must not set some argument's values here for help argument 1239 ! if auto is not set the parse method will disable the option during 1268 ! if auto is not set the parse method will disable the option during 1240 1269 ! next parsing process 1241 1270 CALL words_append(new_cmd,TRIM(elt)) … … 1247 1276 RETURN 1248 1277 END FUNCTION ap_split_cmd 1249 1278 1250 1279 FUNCTION ap_check_string(this,string,idx) RESULT(ret) 1251 1280 !! Check if a string is an option flag … … 1259 1288 INTEGER, INTENT(out), OPTIONAL :: idx 1260 1289 !! An optional output intger with the index of the afferent argument in the parser (-1 if not found) 1261 INTEGER :: ret 1290 INTEGER :: ret 1262 1291 !! Return code with the following possible values: 1263 1292 !! - -1 if the string is a SHORT option flag … … 1272 1301 ! The combination of the (optional) output index and the return code 1273 1302 ! allows to check if an option is known or not 1274 ret = 1 1303 ret = 1 1275 1304 ! '--' is special : it is a separator that is seen as a value. 1276 1305 IF (TRIM(string) == '--') RETURN … … 1303 1332 CHARACTER(len=:), ALLOCATABLE :: copt,spc,opts,text 1304 1333 INTEGER :: i,j,ia,optmw,i1,io,n,zw,zh 1305 IF (this%width == 0) CALL fs_termsize(zh,this%width) 1334 IF (this%width == 0) CALL fs_termsize(zh,this%width) 1306 1335 zw = this%width 1307 1336 ! Sets usage … … 1312 1341 hlp = TRIM(this%usg)//NEW_LINE('A')//NEW_LINE('A') 1313 1342 ! Sets description 1314 IF (LEN_TRIM(this%descr) /= 0) & 1343 IF (LEN_TRIM(this%descr) /= 0) & 1315 1344 hlp=hlp//getpar(this%descr,zw,2)//NEW_LINE('A')//NEW_LINE('A') 1316 1345 ! Sets positionals … … 1380 1409 !! Format command line usage. 1381 1410 !! 1382 !! The subroutine creates and formats the command line usage of the 1383 !! given argparser object. If [[argparser(type):usg(variable)]] is already set (i.e. not empty) 1384 !! then the method only computes the maximum width of the option part of the usage command 1385 !! (see `optmw` argument description) if needed. In the other case, the method builds the usage 1411 !! The subroutine creates and formats the command line usage of the 1412 !! given argparser object. If [[argparser(type):usg(variable)]] is already set (i.e. not empty) 1413 !! then the method only computes the maximum width of the option part of the usage command 1414 !! (see `optmw` argument description) if needed. In the other case, the method builds the usage 1386 1415 !! line based on the arguments stored in the parser. 1387 1416 OBJECT(argparser), INTENT(inout) :: this 1388 1417 !! An argparser object reference 1389 1418 INTEGER, INTENT(out), OPTIONAL :: optmw 1390 !! An optional integer with the maximum length of the option part of the usage command. 1419 !! An optional integer with the maximum length of the option part of the usage command. 1391 1420 !! This variable is intended to set a fancy indentation while printing option in the helper. 1392 1421 CHARACTER(len=:), ALLOCATABLE :: usage, idts, copt,pgn … … 1428 1457 usage = usage(:)//TRIM(copt) 1429 1458 ENDIF 1430 ENDIF 1459 ENDIF 1431 1460 this%usg = usage 1432 1461 ELSE … … 1439 1468 optmw = omw 1440 1469 ENDIF 1441 ENDIF 1470 ENDIF 1442 1471 END SUBROUTINE ap_format_usage 1443 1472 … … 1462 1491 IF (ALLOCATED(other%descr)) this%descr = other%descr 1463 1492 IF (ALLOCATED(other%eplg)) this%eplg = other%eplg 1493 IF (ALLOCATED(other%vers)) this%vers = other%vers 1464 1494 #else 1465 1495 this%usg = other%usg 1466 1496 this%descr = other%descr 1467 1497 this%eplg = other%eplg 1498 this%vers = other%vers 1468 1499 #endif 1469 1500 this%mxhlpos = other%mxhlpos … … 1507 1538 FUNCTION ac_equals_arg(this,other) RESULT(ret) 1508 1539 !! Check if two arguments are identical 1509 !! The method checks if two arguments are equal based on their name, short option flag and long 1510 !! option flag.Two arguments are considered equals if at least one of these three members is equal 1540 !! The method checks if two arguments are equal based on their name, short option flag and long 1541 !! option flag.Two arguments are considered equals if at least one of these three members is equal 1511 1542 !! and not empty. 1512 1543 TYPE(argc), INTENT(in) :: this … … 1529 1560 tlf=TRIM(this%lflag) ; olf=TRIM(other%lflag) 1530 1561 #endif 1531 tn=LEN_TRIM(tna) ; on=LEN_TRIM(ona) 1562 tn=LEN_TRIM(tna) ; on=LEN_TRIM(ona) 1532 1563 tl=LEN_TRIM(tlf) ; ol=LEN_TRIM(olf) 1533 ts=LEN_TRIM(tsf) ; os=LEN_TRIM(osf) 1564 ts=LEN_TRIM(tsf) ; os=LEN_TRIM(osf) 1534 1565 ! check on name : 1535 1566 ! Returns True if at least of name, sflag and lflag is set to non-empty … … 1537 1568 ret = ((tn/=0 .AND. on==tn) .AND. tna == ona) .OR. & 1538 1569 ((tl/=0 .AND. ol==tl) .AND. tlf == olf) .OR. & 1539 ((ts/=0 .AND. os==ol) .AND. tsf == osf) 1570 ((ts/=0 .AND. os==ol) .AND. tsf == osf) 1540 1571 DEALLOCATE(tna,ona,tsf,osf,tlf,olf) 1541 1572 END FUNCTION ac_equals_arg … … 1543 1574 FUNCTION ac_differs_arg(this,other) RESULT(ret) 1544 1575 !! Check if two arguments are different 1545 !! The method checks if two arguments are different based on their names, short option flag 1576 !! The method checks if two arguments are different based on their names, short option flag 1546 1577 !! and long option flag. 1547 !! @note 1578 !! @note 1548 1579 !! This function is the extact contrary of [[argparse(module):ac_equals_arg(function)]] ! 1549 1580 TYPE(argc), INTENT(in) :: this … … 1558 1589 SUBROUTINE ac_clear_arg_sc(arg) 1559 1590 !! argc destructor (scalar) 1560 !! The subroutine frees all memory used by an argc object and resets its member to 1591 !! The subroutine frees all memory used by an argc object and resets its member to 1561 1592 !! default values. 1562 1593 TYPE(argc), INTENT(inout) :: arg … … 1606 1637 args(i)%lflag = "" 1607 1638 #endif 1608 ENDDO 1639 ENDDO 1609 1640 END SUBROUTINE ac_clear_arg_ve 1610 1641 … … 1622 1653 TYPE(argc), INTENT(in) :: this 1623 1654 !! An argc object 1624 INTEGER :: num 1655 INTEGER :: num 1625 1656 !! The number of values stored in the argument 1626 1657 num = words_length(this%values) … … 1629 1660 FUNCTION ac_get_usg_opt_str(arg) RESULT(line) 1630 1661 !! Build and format the option string for the usage part of the help message 1631 !! The function is private part of the help builder. It creates the 1662 !! The function is private part of the help builder. It creates the 1632 1663 !! option string part of a given argument. 1633 1664 TYPE(argc), INTENT(in), TARGET :: arg … … 1661 1692 FUNCTION ac_get_opt_str(arg) RESULT(line) 1662 1693 !! Build and format the option flag string for the option part of the help message 1663 !! The function is private part of the help builder. It creates the 1694 !! The function is private part of the help builder. It creates the 1664 1695 !! option string part of a given argument for the options part of the help. 1665 1696 TYPE(argc), INTENT(in), TARGET :: arg … … 1701 1732 !! The function formats argparse specific errors when extra (missing) values 1702 1733 !! are (not) set or when given values are not consistent with argument's type. 1703 !! For each of these errors, the basic error is updated with a more precise 1704 !! message. 1734 !! For each of these errors, the basic error is updated with a more precise 1735 !! message. 1705 1736 TYPE(argc), INTENT(in) :: arg 1706 1737 !! An argc object … … 1723 1754 IF (arg%nrec == -2) THEN 1724 1755 msg = msg//' takes at least '//nv//' value(s))' 1725 ELSE 1756 ELSE 1726 1757 msg = msg//' takes exactly '//nv//' value(s))' 1727 1758 ENDIF 1728 1759 CASE (-18) ! extra values -> -18 1729 IF (arg%nrec == -3) THEN 1760 IF (arg%nrec == -3) THEN 1730 1761 msg = msg//' takes at most '//nv//' value(s))' 1731 1762 ELSE … … 1740 1771 FUNCTION ac_check_and_set(this,sf,lf,ty,ac,de,na,meta,check_flag) RESULT(ret) 1741 1772 !! Interface to all argc's member tests 1742 !! The function calls all the tests to perform on argc members. Some of these tests can 1743 !! alter argc's member values to fit argparser's requirements. 1773 !! The function calls all the tests to perform on argc members. Some of these tests can 1774 !! alter argc's member values to fit argparser's requirements. 1744 1775 !! 1745 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1776 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1746 1777 !! produced by misuse of the function arguments. In such case, the program should be 1747 1778 !! stopped: note that such ezrror should not occur in _released_ programs. … … 1763 1794 !! An optional vector of strings with the Meta-name of the values 1764 1795 LOGICAL, INTENT(in), OPTIONAL :: check_flag 1765 !! An optional boolean flag hat instructs the method wether to check for option flag 1766 !! or not. By default this test is enabled but it should be disabled if one wants to 1796 !! An optional boolean flag hat instructs the method wether to check for option flag 1797 !! or not. By default this test is enabled but it should be disabled if one wants to 1767 1798 !! check for POSITIONAL arguments as they do not have option flags. 1768 1799 TYPE(error) :: ret … … 1789 1820 FUNCTION ac_check_ac_ty_de_na(this,ac,ty,de,na) RESULT(ret) 1790 1821 !! Check and set argc's action, type and default value 1791 !! The method checks if input argument's options are valid and update the argc object 1792 !! consequently. 1822 !! The method checks if input argument's options are valid and update the argc object 1823 !! consequently. 1793 1824 !! 1794 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1825 !! On success, `noerror` is returned. Otherwise -9 error code is returned. Errors are only 1795 1826 !! produced by misuse of the function arguments. 1796 1827 TYPE(argc), INTENT(inout) :: this 1797 1828 !! An argc object to update 1798 1829 INTEGER, INTENT(in) :: ac 1799 !! An integer with the action to set and check 1830 !! An integer with the action to set and check 1800 1831 INTEGER, INTENT(in) :: ty 1801 1832 !! An integer with the type to set and check 1802 1833 CHARACTER(len=*), INTENT(in) :: de 1803 1834 !! A string with the default value to set and check 1804 CHARACTER(len=*), INTENT(in) :: na 1835 CHARACTER(len=*), INTENT(in) :: na 1805 1836 !! A string with the expected number of value to set and check 1806 1837 TYPE(error) :: ret … … 1810 1841 ret = noerror 1811 1842 eprf = 'argparse: '//"Invalid argument `"//TRIM(this%name)//"'" 1812 uty = error(eprf//" (type)",-9) 1843 uty = error(eprf//" (type)",-9) 1813 1844 ina = error(eprf//" (inconsistent nargs)",-9) 1814 1845 zna = TRIM(na) 1815 1846 ! Checks action 1816 1847 IF(ANY(ap_actions == ac).OR.ac == ap_undef) THEN 1817 this%paction = ac 1848 this%paction = ac 1818 1849 ELSE 1819 ret = error(eprf//" (action)",-9) ; RETURN 1850 ret = error(eprf//" (action)",-9) ; RETURN 1820 1851 ENDIF 1821 1852 ! Checks and sets type and default as a function of the action 1822 1853 SELECT CASE(this%paction) 1823 ! HELP: fixed in any case: 1854 ! HELP: fixed in any case: 1824 1855 CASE(ap_help) 1825 1856 this%default = 'F' 1826 1857 this%ptype = ap_logical 1827 1858 this%nrec = 0 1859 ! VERSION: fixed in any case: 1860 CASE(ap_version) 1861 this%default = 'F' 1862 this%ptype = ap_logical 1863 this%nrec = 0 1828 1864 ! COUNT: 1829 ! we always use "hard-coded" stuff and do not warn if dev has made 1830 ! mistakes... 1865 ! we always use "hard-coded" stuff and do not warn if dev has made mistakes... 1831 1866 CASE(ap_count) 1832 1867 ! default settings of the count action … … 1835 1870 ret = set_def_val() 1836 1871 ! STORE, APPEND actions 1837 CASE (ap_store, ap_append) 1838 ! set type 1872 CASE (ap_store, ap_append) 1873 ! set type 1839 1874 IF (ty == ap_undef) THEN 1840 this%ptype= ap_integer 1875 this%ptype= ap_integer 1841 1876 ELSEIF (ANY(ap_types == ty)) THEN 1842 1877 this%ptype = ty … … 1847 1882 ret = set_def_val() ; IF (ret /= 0) RETURN 1848 1883 ! check for nargs (if na is empty then we set "*") 1849 ret = set_nrec("*") 1884 ret = set_nrec("*") 1850 1885 ! UNDEFINED: 1851 1886 ! -> 1) set to action to store … … 1855 1890 CASE (ap_undef) 1856 1891 ! 1) always define store action 1857 this%paction = ap_store 1892 this%paction = ap_store 1858 1893 ! 2) set type and nrec: 1859 1894 ! 2.1) If type is undef: 1860 1895 ! - to default value type if default is given 1861 ! - ap_logical otherwiset type 1896 ! - ap_logical otherwiset type 1862 1897 ! 2.2) set nrec 1863 1898 ! - if final type is ap_logical set nrec to 0 … … 1865 1900 If (ty == ap_undef) THEN 1866 1901 ! no explicit type : define logical trigger first 1867 this%ptype = ap_logical ; this%nrec = 0 1902 this%ptype = ap_logical ; this%nrec = 0 1868 1903 ! icheck the default value given 1869 1904 IF (LEN_TRIM(de) > 0) THEN … … 1873 1908 ENDIF 1874 1909 IF (this%ptype == ap_logical) THEN 1875 ret = set_nrec("0") 1910 ret = set_nrec("0") 1876 1911 ELSE 1877 ret = set_nrec("1") 1912 ret = set_nrec("1") 1878 1913 ENDIF 1879 ret = set_def_val() 1914 ret = set_def_val() 1880 1915 IF (ret /= 0) RETURN 1881 1916 ! type is given … … 1883 1918 ! known type given : 1884 1919 ! check default value and nrec: -> if na not given set "*" 1885 this%ptype = ty 1920 this%ptype = ty 1886 1921 ret = set_def_val() ; IF (ret /= 0) RETURN 1887 1922 IF (this%ptype == ap_logical) THEN 1888 ret = set_nrec("0") 1923 ret = set_nrec("0") 1889 1924 ELSE 1890 1925 ret = set_nrec("1") … … 1892 1927 ELSE 1893 1928 ! unknown type => bad end ! 1894 ret = uty ; RETURN 1929 ret = uty ; RETURN 1895 1930 ENDIF 1896 END SELECT 1931 END SELECT 1897 1932 ! set default value as first value if ret is noerror .... 1898 1933 IF (ret == 0) CALL words_append(this%values,this%default) … … 1904 1939 !! Check and set argument's expected number of records 1905 1940 !! The method compares `na` value with the expected and known flags and decides 1906 !! wether to raise an error or defines nrec member of the argument object. If `na` 1941 !! wether to raise an error or defines nrec member of the argument object. If `na` 1907 1942 !! is empty then `base` is used. 1908 1943 CHARACTER(len=1),INTENT(in) :: base … … 1921 1956 ! check numeric characters 1922 1957 IF (VERIFY(zna,"0123456789")==0) READ(zna,*) this%nrec 1923 IF (this%nrec == 0) terr = ina 1958 IF (this%nrec == 0) terr = ina 1924 1959 END SELECT 1925 1960 END FUNCTION set_nrec … … 1927 1962 FUNCTION set_def_val() RESULT(terr) 1928 1963 !! Check and set argument's default value 1929 !! The method compares `de` value with the type already stored in the argument and 1964 !! The method compares `de` value with the type already stored in the argument and 1930 1965 !! decides wether to raise an error or to save `de` as argument's default value. 1931 1966 !! If `de` is empty then it sets a default value according to argument's type. … … 1935 1970 terr = noerror 1936 1971 IF (LEN_TRIM(de) /= 0) THEN 1937 this%default = de ; t = string_is(de) 1972 this%default = de ; t = string_is(de) 1938 1973 IF (t /= this%ptype) THEN 1939 1974 terr = error(eprf//" (inconsistent default value: expected '"// & … … 1952 1987 ENDIF 1953 1988 RETURN 1954 END FUNCTION set_def_val 1989 END FUNCTION set_def_val 1955 1990 END FUNCTION ac_check_ac_ty_de_na 1956 1991 … … 1969 2004 CHARACTER(len=2), INTENT(in) :: sflag 1970 2005 !! A 2-characters wide string with the short option flag 1971 CHARACTER(len=*), INTENT(in) :: lflag 2006 CHARACTER(len=*), INTENT(in) :: lflag 1972 2007 !! A string (at least 3 characters wide) with the long option flag 1973 2008 TYPE(error) :: ret … … 2017 2052 !! Set meta-variable of the given argc object 2018 2053 !! The method set meta-variable in the argc object. If no `meta` are given, the method 2019 !! uses argument's name to set the values. 2054 !! uses argument's name to set the values. 2020 2055 !! @warning 2021 2056 !! To be effective, this subroutine must be called after argparse::chk_opt_nargs … … 2042 2077 j=j+1 ; IF (j>ms) j=1 2043 2078 zmeta = to_upper(meta(j)) 2044 blk=INDEX(TRIM(zmeta),CHAR(32))-1 2079 blk=INDEX(TRIM(zmeta),CHAR(32))-1 2045 2080 IF (blk <= 0) blk=LEN_TRIM(zmeta) 2046 2081 CALL words_append(this%meta,zmeta(1:blk)) … … 2063 2098 FUNCTION ac_check_value(this,str) RESULT(err) 2064 2099 !! Check if given string is a valid value for the argument 2065 TYPE(argc), INTENT(in) :: this 2100 TYPE(argc), INTENT(in) :: this 2066 2101 !! An argc object reference 2067 2102 CHARACTER(len=*), INTENT(in) :: str … … 2093 2128 FUNCTION ap_get_positional_sc(this,idx,value) RESULT(ret) 2094 2129 !! Get positional arguments value at given index 2095 !! @warning 2130 !! @warning 2096 2131 !! On error, the status of `value` is undefined. 2097 2132 OBJECT(argparser), INTENT(in) :: this … … 2100 2135 !! Subscript of the positional argument value to get 2101 2136 CHARACTER(len=:), INTENT(out), ALLOCATABLE :: value 2102 !! Output raw value of the positional. If `idx` is out of range, `value` is set to an 2137 !! Output raw value of the positional. If `idx` is out of range, `value` is set to an 2103 2138 !! empty string. 2104 2139 TYPE(error) :: ret … … 2117 2152 FUNCTION ap_get_positional_ve(this,values) RESULT(ret) 2118 2153 !! Get all positional arguments value 2119 !! @warning 2154 !! @warning 2120 2155 !! On error, the status of `values` is undefined. 2121 2156 OBJECT(argparser), INTENT(in) :: this 2122 2157 !! An argparser object reference 2123 2158 CHARACTER(len=st_slen), INTENT(out), ALLOCATABLE, DIMENSION(:) :: values 2124 !! An allocatable vector of **assumed length** strings with the value(s) of all 2125 !! positionals arguments found. 2159 !! An allocatable vector of **assumed length** strings with the value(s) of all 2160 !! positionals arguments found. 2126 2161 TYPE(error) :: ret 2127 2162 !! Error object with the first error encountered in the process … … 2532 2567 2533 2568 !> Gets a scalar @p REAL(kind=8) value from given argument 2534 !! @param[in,out] this An argc object 2569 !! @param[in,out] this An argc object 2535 2570 !! @param[out] output A scalar with the first value of the argument 2536 2571 !! @return An errors::error object with -21 if the destination variable's type … … 2538 2573 FUNCTION ac_get_dv_sc(this, output) RESULT(ret) 2539 2574 !! Get a scalar `REAL(kind=8)` value from given argument 2540 !! If no error occured, the function always returns at least a value (whatever the parser's 2575 !! If no error occured, the function always returns at least a value (whatever the parser's 2541 2576 !! state is) which is the default value if no specific values are set in the argument. 2542 2577 !! Otherwise, `output` value is undefined. … … 2563 2598 FUNCTION ac_get_rv_sc(this, output) RESULT(ret) 2564 2599 !! Get a scalar `REAL(kind=4)` value from given argument 2565 !! If no error occured, the function always returns at least a value (whatever the parser's 2600 !! If no error occured, the function always returns at least a value (whatever the parser's 2566 2601 !! state is) which is the default value if no specific values are set in the argument. 2567 2602 !! Otherwise, `output` value is undefined. … … 2588 2623 FUNCTION ac_get_iv_sc(this, output) RESULT(ret) 2589 2624 !! Get a scalar `INTEGER` value from given argument 2590 !! If no error occured, the function always returns at least a value (whatever the parser's 2625 !! If no error occured, the function always returns at least a value (whatever the parser's 2591 2626 !! state is) which is the default value if no specific values are set in the argument. 2592 2627 !! Otherwise, `output` value is undefined. … … 2613 2648 FUNCTION ac_get_lv_sc(this, output) RESULT(ret) 2614 2649 !! Get a scalar `INTEGER` value from given argument 2615 !! If no error occured, the function always returns at least a value (whatever the parser's 2650 !! If no error occured, the function always returns at least a value (whatever the parser's 2616 2651 !! state is) which is the default value if no specific values are set in the argument. 2617 2652 !! Otherwise, `output` value is undefined. … … 2638 2673 FUNCTION ac_get_cv_sc(this, output) RESULT(ret) 2639 2674 !! Get a scalar `COMPLEX` value from given argument 2640 !! If no error occured, the function always returns at least a value (whatever the parser's 2675 !! If no error occured, the function always returns at least a value (whatever the parser's 2641 2676 !! state is) which is the default value if no specific values are set in the argument. 2642 2677 !! Otherwise, `output` value is undefined. … … 2663 2698 FUNCTION ac_get_sv_sc(this, output) RESULT(ret) 2664 2699 !! Get a scalar `STRING` value from given argument 2665 !! If no error occured, the function always returns at least a value (whatever the parser's 2700 !! If no error occured, the function always returns at least a value (whatever the parser's 2666 2701 !! state is) which is the default value if no specific values are set in the argument. 2667 2702 !! Otherwise, `output` status is undefined. … … 2685 2720 FUNCTION ac_get_dv_ve(this, output) RESULT(ret) 2686 2721 !! Get a vector of `REAL(kind=8)` values from given argument 2687 !! If no error occured, the function always returns at least a value (whatever the parser's 2722 !! If no error occured, the function always returns at least a value (whatever the parser's 2688 2723 !! state is) which is the default value if no specific values are set in the argument. 2689 2724 !! Otherwise, `output` status is undefined. … … 2714 2749 FUNCTION ac_get_rv_ve(this, output) RESULT(ret) 2715 2750 !! Get a vector of `REAL(kind=4)` values from given argument 2716 !! If no error occured, the function always returns at least a value (whatever the parser's 2751 !! If no error occured, the function always returns at least a value (whatever the parser's 2717 2752 !! state is) which is the default value if no specific values are set in the argument. 2718 2753 !! Otherwise, `output` status is undefined. … … 2743 2778 FUNCTION ac_get_iv_ve(this, output) RESULT(ret) 2744 2779 !! Get a vector of `INTEGER` values from given argument 2745 !! If no error occured, the function always returns at least a value (whatever the parser's 2780 !! If no error occured, the function always returns at least a value (whatever the parser's 2746 2781 !! state is) which is the default value if no specific values are set in the argument. 2747 2782 !! Otherwise, `output` status is undefined. … … 2772 2807 FUNCTION ac_get_lv_ve(this, output) RESULT(ret) 2773 2808 !! Get a vector of `LOGICAL` values from given argument 2774 !! If no error occured, the function always returns at least a value (whatever the parser's 2809 !! If no error occured, the function always returns at least a value (whatever the parser's 2775 2810 !! state is) which is the default value if no specific values are set in the argument. 2776 2811 !! Otherwise, `output` status is undefined. … … 2801 2836 FUNCTION ac_get_cv_ve(this, output) RESULT(ret) 2802 2837 !! Get a vector of `COMPLEX` values from given argument 2803 !! If no error occured, the function always returns at least a value (whatever the parser's 2838 !! If no error occured, the function always returns at least a value (whatever the parser's 2804 2839 !! state is) which is the default value if no specific values are set in the argument. 2805 2840 !! Otherwise, `output` status is undefined. … … 2830 2865 FUNCTION ac_get_sv_ve(this, output) RESULT(ret) 2831 2866 !! Get a vector of `STRING` values from given argument 2832 !! If no error occured, the function always returns at least a value (whatever the parser's 2867 !! If no error occured, the function always returns at least a value (whatever the parser's 2833 2868 !! state is) which is the default value if no specific values are set in the argument. 2834 2869 !! Otherwise, `output` status is undefined. … … 2872 2907 2873 2908 FUNCTION apa2str(ap_a) RESULT(str) 2874 !! Get the string representation of argparse actions constants 2909 !! Get the string representation of argparse actions constants 2875 2910 INTEGER, INTENT(in) :: ap_a 2876 2911 !! One of ap_store, ap_append,cap_count or ap_help module constants … … 2882 2917 CASE(ap_count) ; str = 'count' 2883 2918 CASE(ap_help) ; str = 'help' 2919 CASE(ap_version); str = 'version' 2884 2920 CASE DEFAULT ; str = 'unknown' 2885 2921 END SELECT
Note: See TracChangeset
for help on using the changeset viewer.