| specified(3m_cli2) | specified(3m_cli2) | 
NAME¶
specified(3f) - [ARGUMENTS:M_CLI2] return true if keyword was present on command line (LICENSE:PD)
SYNOPSIS¶
elemental impure function specified(name)
character(len=*),intent(in) :: name
logical :: specified
DESCRIPTION¶
specified(3f) returns .true. if the specified keyword was present on the command line.
M_CLI2 intentionally does not have validators except for SPECIFIED(3f) and of course a check whether the input conforms to the type when requesting a value (with get_args(3f) or the convenience functions like inum(3f)).
Fortran already has powerful validation capabilities. Logical expressions ANY(3f) and ALL(3f) are standard Fortran features which easily allow performing the common validations for command line arguments without having to learn any additional syntax or methods.
OPTIONS¶
- NAME
- name of commandline argument to query the presence of. Long names should always be used.
RETURNS¶
- SPECIFIED
- returns .TRUE. if specified NAME was present on the command line when the program was invoked.
EXAMPLE¶
Sample program:
Default output
program demo_specified
use, intrinsic :: iso_fortran_env, only : &
& stderr=>ERROR_UNIT, stdin=>INPUT_UNIT, stdout=>OUTPUT_UNIT
use M_CLI2, only : set_args, igets, rgets, specified, sget, lget
implicit none
! Define args
integer,allocatable :: ints(:)
real,allocatable :: floats(:)
logical :: flag
character(len=:),allocatable :: color
character(len=:),allocatable :: list(:)
integer :: i
call set_args('&
& --color:c "red" &
& --flag:f F &
& --ints:i 1,10,11 &
& --floats:T 12.3, 4.56 &
& ')
ints=igets('ints')
floats=rgets('floats')
flag=lget('flag')
color=sget('color')
write(*,*)'color=',color
write(*,*)'flag=',flag
write(*,*)'ints=',ints
write(*,*)'floats=',floats
write(*,*)'was -flag specified?',specified('flag')
! elemental
write(*,*)specified(['floats','ints '])
! If you want to know if groups of parameters were specified use
! ANY(3f) and ALL(3f)
write(*,*)'ANY:',any(specified(['floats','ints ']))
write(*,*)'ALL:',all(specified(['floats','ints ']))
! For mutually exclusive
if (all(specified(['floats','ints '])))then
write(*,*)'You specified both names --ints and --floats'
endif
! For required parameter
if (.not.any(specified(['floats','ints '])))then
write(*,*)'You must specify --ints or --floats'
endif
! check if all values are in range from 10 to 30 and even
write(*,*)'are all numbers good?',all([ints >= 10,ints <= 30,(ints/2)*2 == ints])
! perhaps you want to check one value at a time
do i=1,size(ints)
write(*,*)ints(i),[ints(i) >= 10,ints(i) <= 30,(ints(i)/2)*2 == ints(i)]
if(all([ints(i) >= 10,ints(i) <= 30,(ints(i)/2)*2 == ints(i)]) )then
write(*,*)ints(i),'is an even number from 10 to 30 inclusive'
else
write(*,*)ints(i),'is not an even number from 10 to 30 inclusive'
endif
enddo
list = [character(len=10) :: 'red','white','blue']
if( any(color == list) )then
write(*,*)color,'matches a value in the list'
else
write(*,*)color,'not in the list'
endif
if(size(ints).eq.3)then
write(*,*)'ints(:) has expected number of values'
else
write(*,*)'ints(:) does not have expected number of values'
endif
end program demo_specified
> color=red
> flag= F
> ints= 1 10 11
> floats= 12.3000002 4.55999994
> was -flag specified? F
> F F
> ANY: F
> ALL: F
> You must specify --ints or --floats
> 1 F T F
> 1 is not an even number from 10 to 30 inclusive
> 10 T T T
> 10 is an even number from 10 to 30 inclusive
> 11 T T F
> 11 is not an even number from 10 to 30 inclusive
> red matches a value in the list
> ints(:) has expected number of values
AUTHOR¶
John S. Urban, 2019
LICENSE¶
Public Domain
| February 10, 2023 |