set_args(3f) - [ARGUMENTS:M_CLI2] command line argument parsing (LICENSE:PD)
Synopsis
Description
Options
Defining The Prototype
Usage
Example
Response Files
Specification For Response Files
Author
License
subroutine set_args(definition,help_text,version_text,ierr,errmsg)
character(len=*),intent(in),optional :: definition character(len=:),intent(in),allocatable,optional :: help_text character(len=:),intent(in),allocatable,optional :: version_text integer,intent(out),optional :: ierr character(len=:),intent(out),allocatable,optional :: errmsg
SET_ARGS(3f) requires a unix-like command prototype for defining arguments and default command-line options. Argument values are then read using GET_ARGS(3f).
The --help and --version options require the optional help_text and version_text values to be provided.
DESCRIPTION composed of all command arguments concatenated into a Unix-like command prototype string. For example: call set_args(-L F -ints 10,20,30 -title "my title" -R 10.3)
DESCRIPTION is pre-defined to act as if started with the reserved options --verbose F --usage F --help F --version F. The --usage option is processed when the set_args(3f) routine is called. The same is true for --help and --version if the optional help_text and version_text options are provided. see "DEFINING THE PROTOTYPE" in the next section for further details.
HELP_TEXT if present, will be displayed if program is called with --help switch, and then the program will terminate. If not supplied, the command line initialization string will be shown when --help is used on the commandline. VERSION_TEXT if present, will be displayed if program is called with --version switch, and then the program will terminate. IERR if present a non-zero option is returned when an error occurs instead of program execution being terminated ERRMSG a description of the error if ierr is present
o all keywords on the prototype MUST get a value. o logicals MUST be set to F or T. o strings MUST be delimited with double-quotes and must be at least one space. Internal double-quotes are represented with two double-quotes. o numeric keywords are not allowed; but this allows negative numbers to be used as values. o lists of values should be comma-delimited unless a user-specified delimiter is used. The prototype must use the same array delimiters as the call to the family of get_args*(3f) called. o long names (--keyword) should be all lowercase o The simplest way to have short names is to suffix the long name with :LETTER If this syntax is used then logical shorts may be combined on the command line and -- and - prefixes are strictly enforced. mapping of short names to long names not using the --LONGNAME:SHORTNAME syntax is demonstrated in the manpage for SPECIFIED(3f).
o A very special behavior occurs if the keyword name ends in ::. The next parameter is taken as a value even if it starts with -. This is not generally recommended but is noted here for completeness. o to define a zero-length allocatable array make the value a delimiter (usually a comma). o all unused values go into the character array UNNAMED o If the prototype ends with "--" a special mode is turned on where anything after "--" on input goes into the variable REMAINING and the array ARGS instead of becoming elements in the UNNAMED array. This is not needed for normal processing.
When invoking the program line note that (subject to change) the following variations from other common command-line parsers:
o Long names should be all lowercase and always more than one character. o values for duplicate keywords are appended together with a space separator when a command line is executed. o numeric keywords are not allowed; but this allows negative numbers to be used as values. o Although not generally recommended you can equivalence keywords (usually for multi-lingual support). Be aware that specifying both names of an equivalenced keyword on a command line will have undefined results (currently, their ASCII alphabetical order will define what the Fortran variable values become). The second of the names should only be called with a GET_ARGS*(3f) routine if the SPECIFIED(3f) function is .TRUE. for that name.
Note that allocatable arrays cannot be EQUIVALENCEd in Fortran.
o short keywords cannot be combined unless they were defined using the --LONGNAME:SHORTNAME syntax. Even then -a -b -c is required not -abc unless all the keywords are logicals (Boolean keys). o shuffling is not supported. Values should follow their keywords. o if a parameter value of just "-" is supplied it is converted to the string "stdin". o values not matching a keyword go into the character array "UNUSED". o if the keyword "--" is encountered the rest of the command arguments go into the character array "UNUSED".
Sample program:
program demo_set_args use M_CLI2, only : filenames=>unnamed, set_args, get_args, unnamed use M_CLI2, only : get_args_fixed_size implicit none integer :: i ! ! DEFINE ARGS real :: x, y, z real :: p(3) character(len=:),allocatable :: title logical :: l, lbig integer,allocatable :: ints(:) ! ! DEFINE COMMAND (TO SET INITIAL VALUES AND ALLOWED KEYWORDS) ! AND READ COMMAND LINE call set_args( & ! reals & -x 1 -y 2.3 -z 3.4e2 & ! integer array & -p -1,-2,-3 & ! always double-quote strings & --title "my title" & ! set all logical values to F or T. & -l F -L F & ! set allocatable size to zero if you like by using a delimiter & -ints , & ! string should be a single character at a minimum & --label " " & & ) ! ASSIGN VALUES TO ELEMENTS ! SCALARS call get_args(x,x) call get_args(y,y) call get_args(z,z) call get_args(l,l) call get_args(L,lbig) call get_args(ints,ints) ! ALLOCATABLE ARRAY call get_args(title,title) ! ALLOCATABLE STRING call get_args_fixed_size(p,p) ! NON-ALLOCATABLE ARRAY ! USE VALUES write(*,*)x=,x write(*,*)y=,y write(*,*)z=,z write(*,*)p=,p write(*,*)title=,title write(*,*)ints=,ints write(*,*)l=,l write(*,*)L=,lbig ! UNNAMED VALUES if(size(filenames).gt.0)then write(*,(i6.6,3a))(i,[,filenames(i),],i=1,size(filenames)) endif end program demo_set_args
If you have no interest in using external files as abbreviations you can ignore this section. Otherwise, before calling set_args(3f) add:
use M_CLI2, only : CLI_response_file CLI_response_file=.true.M_CLI2 Response files are small files containing CLI (Command Line Interface) arguments that are used when command lines are so long that they would exceed line length limits or so complex that it is useful to have a platform-independent method of creating an abbreviation.
Examples of commands that support similar response files are the Clang and Intel compilers, although there is no standard format for the files.
Shell aliases and scripts are often used for similar purposes (and allow for much more complex conditional execution, of course), but they generally cannot be used to overcome line length limits and are typically platform-specific.
The file names must end with .rsp . They are read if you add options of the syntax "@NAME" as the FIRST parameters on your program command line calls.
The first resource file found that results in lines being processed will be used and processing stops after that first match is found. If no match is found an error occurs and the program is stopped.
A search for the response file always starts with the current directory. The search then proceeds to look in any additional directories specified with the colon-delimited environment variable CLI_RESPONSE_PATH.
A simple response file just has options for calling the program in it. But they can also contain section headers to denote sections that are only executed when a specific OS is being used. In addition a special response file named PROGRAM.rsp can contain multiple abbreviations.
SEARCH FOR @OSTYPE IN REGULAR FILES (NAME.rsp)Assuming the name @NAME was specified on the command line a file named NAME.rsp will be searched for in all those search locations for a string that starts with the string @OSTYPE if the environment variables $OS and $OSTYPE are not blank.
If $OSTYPE is unset, the value of the variable OS will be used.
SEARCH FOR UNLABELED DIRECTIVES IN REGULAR FILES (NAME.rsp)Then, the same files will be searched for lines before any line starting with "@". That is, if there is no special section for the current OS it just looks at the top of the file for unlabeled options.
SEARCH FOR @OSTYPE@NAME IN THE COMPLEX FILE (EXECUTABLE.rsp)Then, if nothing was found a file name EXECUTABLE.rsp will be searched for in the same locations where EXECUTABLE is the basename of the program being executed. This file is always a "complex" response file that uses the format described below to allow for multiple entries.
Any complex EXECUTABLE.rsp file found in the current or searched directories will be searched for the string @OSTYPE@NAME first.
SEARCH FOR @NAME IN THE COMPLEX FILE (EXECUTABLE.rsp)The last search is to search all the EXECUTABLE.rsp files for the string @NAME.
Sounds complicated but actually works quite intuitively. Make a file in the current directory and put options in it and it will be used. If that file ends up needing different cases for different platforms add a line like "@Linux" to the file and some more lines and that will only be executed if the environment variable OSTYPE or OS is "Linux". If no match is found for named sections the lines at the top before any "@" lines will be used as a default if not match is found.
If you end up using a lot of files like this you can combine them all together and put them info a file called "program_name.rsp" and just put lines like @NAME or @OSTYPE@NAME at that top of each section.
Note that more than one response name may appear on a command line.
They are case-sensitive names.
As mentioned, they must be the first options on the command line.
SPECIFICATION FOR RESPONSE FILES
The first word of a line is special and has the following meanings:
options|- Command options following the rules of the SET_ARGS(3f) prototype. So o It is preferred to specify a value for all options. o double-quote strings. o give a blank string value as " ". o use F|T for lists of logicals, o lists of numbers should be comma-delimited. comment|# Line is a comment line system|! System command. System commands are executed as a simple call to system (so a cd(1) or setting a shell variable would not effect subsequent lines, for example) print|> Message to screen stop display message and stop program.So if a program that echoed its parameters has a call of the form
set_args(-x 10.0 -y 20.0 --title "my title")And a file in the current directory called "a.rsp" contained
# defaults for project A options -x 1000 -y 9999 options --title "my new default title"The program could be called with
# normal $myprog X=10.0 Y=20.0 TITLE="my title"# change defaults as specified in "a.rsp" $myprog @a X=1000.0 Y=9999.0 TITLE="my new default title"
# change defaults but use any option as normal to override defaults $myprog @a -y 1234 X=1000.0 Y=1234.0 TITLE="my new default title"
A compound response file has the same basename as the executable with a ".rsp" suffix added. So if your program is named "myprg" the filename must be "myprg.rsp".
Note that here basename means the basename of the name of the program as returned by the Fortran intrinsic GET_COMMAND_ARGUMENT(0,...) trimmed of anything after a period ("."), so it is a good idea not to use hidden files.Unlike simple response files compound response files can contain multiple setting names.
If the environment variable $OSTYPE (first) or $OS is set the search will first be for a line of the form (no leading spaces should be used):
@OSTYPE@alias_nameIf no match or if the environment variables $OSTYPE and $OS were not set or a match is not found then a line of the form
@alias_nameis searched for. Subsequent lines will be ignored that start with "@"
until a line not starting with @ is encountered. Lines will then be processed until another line starting with "@" is found or end-of-file is encountered.
An example compound file
################# @if > RUNNING TESTS USING RELEASE VERSION AND ifort - test --release --compiler ifort ################# @gf > RUNNING TESTS USING RELEASE VERSION AND gfortran - test --release --compiler gfortran ################# @nv > RUNNING TESTS USING RELEASE VERSION AND nvfortran - test --release --compiler nvfortran ################# @nag > RUNNING TESTS USING RELEASE VERSION AND nagfor - test --release --compiler nagfor # ################# # OS-specific example: @Linux@install # # install executables in directory (assuming install(1) exists) # !mkdir -p ~/.local/bin - run --release T --compiler gfortran --runner "install -vbp -m 0711 -t ~/.local/bin" @install @STOP INSTALL NOT SUPPORTED ON THIS PLATFORM OR $OSTYPE NOT SET # ################# @fpm@testall # !fpm test --compiler nvfortran !fpm test --compiler ifort !fpm test --compiler gfortran !fpm test --compiler nagfor STOP tests complete. Any additional parameters were ignored #################Would be used like
fpm @install fpm @nag -- fpm @testall
The intel Fortran compiler now calls the response files "indirect files" and does not add the implied suffix ".rsp" to the files anymore. It also allows the @NAME syntax anywhere on the command
line, not just at the beginning. -- 20201212
John S. Urban, 2019
Public Domain
set_args (3) | March 11, 2021 |