get_args(3m_cli2)

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

πŸ’– get_args β€” the get_args(3f) page, bestie edition πŸ’–

okay so you're in the 3m_cli2 manual, section 3, and this whole page is about get_args(3f). yes the manual name is get_args(3m_cli2), i know, i know, it's giving "two names for one function" energy but that's just how Fortran rolls. πŸŽ€


✨ NAME ✨

get_args(3f) β€” [ARGUMENTS:M_CLI2] it returns your keyword values when you're parsing command line arguments. oh and btw, the license on this one is PD (public domain, slay, it's free).


πŸ¦‹ SYNOPSIS πŸ¦‹

so here's the whole squad β€” get_args(3f) plus its lil convenience besties:

use M_CLI2, only : get_args
! convenience functions
use M_CLI2, only : dget, iget, lget, rget, sget, cget
use M_CLI2, only : dgets, igets, lgets, rgets, sgets, cgets

subroutine get_args(name,value,delimiters)

 character(len=*),intent(in) :: name

 type(${TYPE}),allocatable,intent(out) :: value(:)
 ! or
 type(${TYPE}),allocatable,intent(out) :: value

 character(len=*),intent(in),optional :: delimiters

 where ${TYPE} may be from the set
         {real,doubleprecision,integer,logical,complex,character(len=:)}

and ${TYPE} can literally be any one of those six β€” real, doubleprecision, integer, logical, complex, or character(len=:). pick your fighter. πŸ‘‘


🌸 DESCRIPTION 🌸

GET_ARGS(3f) hands you back the value of keywords after SET_ARGS(3f) has already done the whole parse-the-command-line thing. so the order is: set it up, then get it out. periodt.

if you're working with fixed-length CHARACTER variables, go look at GET_ARGS_FIXED_LENGTH(3f) instead. and for fixed-size arrays? that's GET_ARGS_FIXED_SIZE(3f), bestie. different page, different vibe. πŸ’…

as a lil convenience moment, you can pass multiple pairs of keywords and variables in one call β€” but only if and only if every single value is a scalar and the CHARACTER variables are fixed-length or pre-allocated. that's the rule, no exceptions, i don't make them. 😭


πŸ’… OPTIONS πŸ’…

  • NAME β€” the name of the commandline argument you want the value of. that's it, that's the whole job.
  • VALUE β€” the variable that holds the returned value. the kind of that variable is what tells the routine what type to return, so it's the one doing the deciding here. it can be a scalar or an allocatable array. and heads up: if the type is CHARACTER, the scalar has to have an allocatable length, no cap. 🫢
  • DELIMITERS β€” by default, the delimiters for array values are comma, colon, and whitespace. you can pass a string with an alternate list of delimiter characters if you want to switch it up. your call, main character. ✨

🌈 CONVENIENCE FUNCTIONS 🌈

these are the shortcuts, the little helpers that stand in for calls to get_args(3f) β€” one for each supported default intrinsic type:

  • scalars β€” dget(3f), iget(3f), lget(3f), rget(3f), sget(3f), cget(3f)
  • vectors β€” dgets(3f), igets(3f), lgets(3f), rgets(3f), sgets(3f), cgets(3f)

and the letter decoding, because i know you're wondering: D is for DOUBLEPRECISION, I for INTEGER, L for LOGICAL, R for REAL, S for string (CHARACTER), and C for COMPLEX. iykyk. 🧚

also, if you call these functions with no argument at all, they return the UNNAMED array, converted to the specified type. cute, right?


πŸ”₯ EXAMPLE πŸ”₯

here's the sample program, straight up:

program demo_get_args
use M_CLI2,  only : filenames=>unnamed, set_args, get_args
implicit none
integer                      :: i
 ! Define ARGS
real                         :: x, y, z
real,allocatable             :: p(:)
character(len=:),allocatable :: title
logical                      :: l, lbig
 ! Define and parse (to set initial values) command line
 !   o only quote strings and use double-quotes
 !   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,'y',y,'z',z,'l',l,'L',lbig)
 ! Allocatable string
call get_args('title',title)
 ! Allocatable arrays
call get_args('p',p)
 ! Use values
write(*,'(1x,g0,"=",g0)')'x',x, 'y',y, 'z',z
write(*,*)'p=',p
write(*,*)'title=',title
write(*,*)'l=',l
write(*,*)'L=',lbig
if(size(filenames) > 0)then
   write(*,'(i6.6,3a)')(i,'[',filenames(i),']',i=1,size(filenames))
endif
end program demo_get_args

like, look at it β€” scalars in one call, allocatable string, allocatable array, all vibing. that's the whole point of the multiple-pairs thing. 🌟


πŸ‘‘ AUTHOR πŸ‘‘

John S. Urban, 2019. legend behavior, honestly. 🫑


πŸ“ LICENSE πŸ“

Public Domain. do whatever, it's yours. πŸ’–


and that's the whole page, bestie β€” this came to you from the get_args(3m_cli2) manual, February 10, 2023, and yes the footer says the exact same thing as the title because manpages love symmetry. go parse something iconic. πŸ’…βœ¨

πŸ“¦ where this page comes from