set_args(3m_cli2)

πŸ“¦ version 3.2.0, in ubuntu resolute, ubuntu stonking

πŸ’– set_args β€” from the 3m_cli2 manual, bestie πŸ’–

okay so real talk: this is the page for set_args(3f), and it's the one that turns your chaotic little command line into actual values your Fortran program can read. Pull up a chair, grab a boba, we're doing this. πŸ§‹βœ¨


🌸 NAME 🌸

set_args(3f) β€” [ARGUMENTS:M_CLI2] command line argument parsing. (LICENSE:PD) β€” yeah, public domain, she's free, she's generous, she ate. πŸ’…


✨ SYNOPSIS ✨

subroutine set_args(prototype,help_text,version_text,ierr,errmsg)

     character(len=*),intent(in),optional              :: prototype
     character(len=*),intent(in),optional              :: help_text(:)
     character(len=*),intent(in),optional              :: version_text(:)
     integer,intent(out),optional                      :: ierr
     character(len=:),intent(out),allocatable,optional :: errmsg

look at her β€” five optional args and not one of them is required. main character energy, honestly. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

SET_ARGS(3f) needs a unix-like command prototype. That prototype is the thing that defines your command-line options and their default values. Then when your program actually gets executed, that prototype plus whatever the user typed on the command line get merged, and the resulting values get dropped into an internal table. You get them back out later with GET_ARGS(3f). no cap, that's the whole loop. πŸ”

The built-in --help and --version options are the reason the optional help_text and version_text values exist β€” like, they work without them, but they're only particularly useful when you actually hand those over. give her something to work with, bestie. 🫢


πŸŽ€ OPTIONS πŸŽ€

  • PROTOTYPE β€” composed of all command arguments concatenated into a Unix-like command prototype string. For example:
call set_args('-L F --ints 1,2,3 --title "my title" -R 10.3')

the following options are predefined for all commands:

see "DEFINING THE PROTOTYPE" in the next section for further details. πŸ‘€

  • HELP_TEXT β€” if present, this gets displayed when the program is called with a --help switch, and then the program terminates. If you don't supply help text, the command line initialization string gets echoed instead. (so like, it'll still say something, it's just gonna be your own prototype staring back at you πŸ’€)
  • VERSION_TEXT β€” if present, any version text you defined will be displayed when the program is called with a --version switch, and then the program terminates. πŸ”₯
  • IERR β€” if present, a non-zero option is returned when an error occurs, instead of the program terminating. your program lives to fight another day. 😌
  • ERRMSG β€” a description of the error if ierr is present.

πŸ’œ DEFINING THE PROTOTYPE πŸ’œ

  • Keywords start with a single dash for short single-character keywords, and with two dashes for longer keywords.
  • all keywords on the prototype MUST get a value. not optional. not vibes-based. all of them. 😀
    • logicals must be set to an unquoted F.
    • strings must be delimited with double-quotes. Since internal double-quotes are represented with two double-quotes, the string must be at least one space.
  • numeric keywords are not allowed; but this allows negative numbers to be used as values. (it's giving "tradeoff" but it's a good one πŸŽ€)
  • 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 get the value. consistency is the moment. ✨
  • to define a zero-length allocatable array, make the value a delimiter (usually a comma) or an empty set of braces ("[]").

🌈 LONG AND SHORT NAMES 🌈

Long keywords start with two dashes followed by more than one letter. Short keywords are a dash followed by a single letter. that's it, that's the rule.

  • It is recommended long names (--keyword) should be all lowercase, but are case-sensitive by default β€” unless set_mode('ignorecase') is in effect.
  • Long names should always be more than one character.
  • The recommended way to have short names is to suffix the long name with :LETTER in the definition.

If this syntax is used then logical shorts may be combined on the command line when set_mode('strict') is in effect. πŸŽ‰

⭐ SPECIAL BEHAVIORS ⭐

  • A special behavior occurs if a keyword name ends in ::. When the program is called, the next parameter is taken as a value even if it starts with -. This is not generally recommended but is useful in rare cases where non-numeric values starting with a dash are desired. (rare case icon, honestly πŸ™ˆ)
  • If the prototype ends with -- a special mode is turned on where anything after -- on input goes into the variable REMAINING with values double-quoted, and also into the array ARGS, instead of becoming elements in the UNNAMED array. This is not needed for normal processing, but was needed for a program that needed this behavior for its subcommands.

That is, for a normal call all unnamed values go into UNNAMED and ARGS and REMAINING are ignored. So for

call set_args('-x 10 -y 20 ')

A program invocation such as

xx a b c -- A B C " dd "

results in

UNNAMED= ['a','b','c','A','B','C',' dd']
REMAINING= ''
ARGS= [character(len=0) :: ] ! ie, an empty character array

Whereas

call set_args('-x 10 -y 20 --')

generates the following output from the same program execution:

UNNAMED= ['a','b','c']
REMAINING= '"A" "B" "C" " dd "'
ARGS= ['A','B','C,' dd']

see the difference?? the -- at the end of the prototype literally changes the whole vibe of what lands where. πŸ¦‹


πŸ‘‘ USAGE NOTES πŸ‘‘

When invoking the program line, note the (subject to change) following restrictions β€” which, btw, often differ between various command-line parsers, so don't @ me:

  • values for duplicate keywords are appended together with a space separator when a command line is executed by default.
  • shuffling is not supported. Values immediately follow their keywords. no wandering off, values. stay next to your keyword. πŸ™…
  • Only short Boolean keywords can be bundled together. If allowing bundling is desired, call set_mode('strict'). This will require prefixing long names with -- and short names with -. Otherwise M_CLI2 relaxes that requirement and mostly does not care what prefix is used for a keyword. But this would make it unclear what was meant by -ox if allowed options were -o F -x F --ox F for example, so strict mode is required to remove the ambiguity.
real talk: -ox in relaxed mode is a genuine mystery meat situation. if you have -o F -x F --ox F sitting around, turn on strict mode or accept that you're living dangerously. πŸ’€
  • if a parameter value of just - is supplied it is converted to the string "stdin".
  • values not needed for a keyword value go into the character array UNNAMED.

In addition if the keyword -- is encountered on the command line the rest of the command line goes into the character array UNNAMED.


πŸ“ EXAMPLE πŸ“

Sample program:

program demo_set_args
use M_CLI2,  only : filenames=>unnamed, set_args, get_args
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" &
   ! string should be a single character at a minimum
   & --label " ", &
   ! set all logical values to F
   & -l F -L F &
   ! set allocatable size to zero if you like by using a delimiter
   & --ints , &
   & ')
! 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) > 0)then
   write(*,'(i6.6,3a)')(i,'[',filenames(i),']',i=1,size(filenames))
endif
end program demo_set_args

and that's part 1 bestie, we're not done yet β€” keep scrolling, there's more page to devour. πŸ’–

πŸ’Œ RESPONSE FILES πŸ’Œ

okay so if you genuinely have zero interest in using external files as abbreviations, you can literally just skip this whole section bestie, no hard feelings πŸ’–. But if you do want it, then before you call set_args(3f) you gotta add:

      use M_CLI2, only : set_mode
      call set_mode('response_file')

M_CLI2 Response files are these lil baby files that hold CLI (Command Line Interface) arguments, and they end in .rsp ✨. You use them when your command lines are soooo long they'd blow past line length limits, or when they're so complex that having a platform-independent way to make an abbreviation is just chef's kiss.

Shell aliases and scripts do kind of the same job (and honestly they allow for way more complex conditional execution, of course πŸ’…), but they generally cannot be used to overcome line length limits and they're typically platform-specific. πŸ™ƒ

Commands that support similar response files include the Clang and Intel compilers β€” although ngl there is no standard format for the files.

They get read if you add options with the syntax "@NAME" as the FIRST parameters on your program command line calls. They are not recursive β€” meaning an option inside a response file cannot be given the value "@NAME2" to call another response file. 🚫

More than one response name may appear on a command line. ✨

They are case-sensitive names.

Note: "@" is a special character in Powershell, and requires being escaped with a grave character. 😭

πŸ” LOCATING RESPONSE FILES πŸ”

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.

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. πŸ’€


πŸ“‘ RESPONSE FILE SECTIONS πŸ“‘

A simple response file just has options for calling the program in it prefixed with the word "options". But they can also contain section headers to denote selections that are only executed when a specific OS is being used, print messages, and execute system commands. πŸŽ€


🐧 SEARCHING FOR OSTYPE IN REGULAR FILES 🐧

So assuming the name @NAME was specified on the command line, a file named NAME.rsp will be searched for in all the search directories, and then in that file a string that starts with the string @OSTYPE (if the environment variables $OS and $OSTYPE are not blank β€” $OSTYPE takes precedence over $OS). πŸ‘‘


🌸 SEARCHING FOR UNLABELED DIRECTIVES IN REGULAR FILES 🌸

Then, the same files will be searched for lines above 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. πŸ¦‹


🧚 SEARCHING FOR OSTYPE AND NAME IN THE COMPOUND FILE 🧚

In addition or instead of files with the same name as the @NAME option on the command line, you can have one file named after the executable name that contains multiple abbreviation names. πŸ’–

So if your program executable is named EXEC you create a single file called EXEC.rsp and can append all the simple files described above, separating them with lines of the form @OSTYPE@NAME or just @NAME.

So if no specific file for the abbreviation is found, a file called "EXEC.rsp" is searched for, where "EXEC" is the name of the executable. This file is always a "compound" response file that uses the following format:

Any compound EXEC.rsp file found in the current or searched directories will be searched for the string @OSTYPE@NAME first. πŸ₯‡

Then if nothing is found, the less specific line @NAME is searched for.


🏁 THE SEARCH IS OVER 🏁

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 no match is found. ✨

If you end up using a lot of files like this you can combine them all together and put them into a file called "program_name".rsp and just put lines like @NAME or @OSTYPE@NAME at that top of each selection. πŸ’…

Now, back to the details on just what you can put in the files. πŸ‘€


πŸ“œ SPECIFICATION FOR RESPONSE FILES πŸ“œ

πŸ“ SIMPLE 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:
    • It is preferred to specify a value for all options.
    • double-quote strings.
    • give a blank string value as " ".
    • use F|T for lists of logicals,
    • lists of numbers should be comma-delimited.
    • --usage, --help, --version, --verbose, and unknown options are ignored.
  • 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) BEFORE the command being processed.
  • print|> β€” Message to screen
  • stop β€” display message and stop program.
NOTE: system commands are executed when encountered, but options are gathered from multiple option lines and passed together at the end of processing of the block; so all commands will be executed BEFORE the command for which options are being supplied no matter where they occur. ⚠️

So if a program that does nothing but echos its parameters:

     program testit
     use M_CLI2, only : set_args, rget, sget, lget, set_mode
     implicit none
        real :: x,y                           ; namelist/args/ x,y
        character(len=:),allocatable :: title ; namelist/args/ title
        logical :: big                        ; namelist/args/ big
        call set_mode('response_file')
        call set_args('-x 10.0 -y 20.0 --title "my title" --big F')
        x=rget('x')
        y=rget('y')
        title=sget('title')
        big=lget('big')
        write(*,nml=args)
     end program testit

And a file in the current directory called "a.rsp" contains:

      # defaults for project A
      options -x 1000 -y 9999
      options --title " "
      options --big T

The program could be called with:

      $myprog     # normal call
       X=10.0 Y=20.0 TITLE="my title"

      $myprog @a  # change defaults as specified in "a.rsp"
      X=1000.0 Y=9999.0 TITLE=" "

      # change defaults but use any option as normal to override defaults
      $myprog @a -y 1234
       X=1000.0 Y=1234.0 TITLE=" "

πŸŽ€ COMPOUND RESPONSE FILES πŸŽ€

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 last leaf 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.

Specifically, in a compound file, if the environment variable $OSTYPE (first) or $OS is set the first search will be for a line of the form (no leading spaces should be used):

     @OSTYPE@alias_name

If 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_name

is searched for in simple or compound files. If found, 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. 🌈


πŸ‘‘ COMPOUND RESPONSE FILE EXAMPLE πŸ‘‘

An example compound file:

     #################
     @if
     > RUNNING TESTS USING RELEASE VERSION AND ifort
     options test --release --compiler ifort
     #################
     @gf
     > RUNNING TESTS USING RELEASE VERSION AND gfortran
     options test --release --compiler gfortran
     #################
     @nv
     > RUNNING TESTS USING RELEASE VERSION AND nvfortran
     options test --release --compiler nvfortran
     #################
     @nag
     > RUNNING TESTS USING RELEASE VERSION AND nagfor
     options test --release --compiler nagfor
     #
     #################
     # OS-specific example:
     @Linux@install
     #
     # install executables in directory (assuming install(1) exists)
     #
     system mkdir -p ~/.local/bin
     options run --release T --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

πŸ’€ NOTES πŸ’€

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


🌟 AUTHOR 🌟

John S. Urban, 2019


πŸ’– LICENSE πŸ’–

Public Domain


and that's the whole page bestie, we ate and left no crumbs 🫢 the original was dated February 10, 2023 and shipped as set_args(3m_cli2) β€” go forth and slay those command lines πŸ’…βœ¨

πŸ“¦ where this page comes from