set_args(3f) - [ARGUMENTS:M_CLI] command line argument parsing (LICENSE:PD)
Synopsis
Description
Options
Usage
Example
Author
License
subroutine set_args(definition,help_text,version_text)
character(len=*),intent(in),optional :: definition character(len=:),intent(in),allocatable,intent(in) :: help_text character(len=:),intent(in),allocatable,intent(in) :: version_text
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).
DESCRIPTION composed of all command arguments concatenated into a Unix-like command prototype string. DESCRIPTION is pre-defined to act as if started with the reserved options --usage F --help F --version F.
o all keywords 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 lists of values should be comma-delimited. o long names (--keyword) should be all lowercase The --help and --version options require the optional help_text and version_text values to be provided.
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 initialized 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.
When using one of the Unix-like command line forms note that (subject to change) the following variations from other common command-line parsers:
o long names do not take the --KEY=VALUE form, just --KEY VALUE; and long names should be all lowercase and always more than one character. o 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 mapping of short names to long names is via an EQUIVALENCE. Note that allocatable arrays cannot be EQUIVANENCEd. Specifying both names of an equivalenced keyword on a command line will have undefined results (currently, their alphabetical order will define what the Fortran variable values become).
o short keywords cannot be combined. -a -b -c is required, not -abc even for 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 if the keyword "--" is encountered the rest of the command arguments go into the character array "UNUSED". o values not matching a keyword go into the character array "UNUSED".
Sample program:
program demo_set_args use M_CLI, only : filenames=>unnamed, set_args, get_args, unnamed implicit none integer :: i integer,parameter :: dp=kind(0.0d0) ! DEFINE ARGS real :: x, y, z real :: p(3) character(len=:),allocatable :: title logical :: l, lbig ! DEFINE AND PARSE (TO SET INITIAL VALUES) COMMAND LINE ! o only quote strings ! o set all logical values to F or T. call set_args( -x 1 -y 2 -z 3 -p -1,-2,-3 --title "my title" & & -l F -L F & & --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) ! ALLOCATABLE STRING call get_args(title,title) ! NON-ALLOCATABLE ARRAYS ! for non-allocatable arrays pass size call get_args(p,p,size(p)) ! USE VALUES write(*,*)x=,x write(*,*)y=,y write(*,*)z=,z write(*,*)p=,p write(*,*)title=,title write(*,*)l=,l write(*,*)L=,lbig if(size(filenames).gt.0)then write(*,(i6.6,3a))(i,[,filenames(i),],i=1,size(filenames)) endif end program demo_set_args
John S. Urban, 2019
Public Domain
set_args (3) | July 05, 2020 |