get_command_argument(3f) - [FORTRAN:INTRINSIC:SYSTEM ENVIRONMENT] Get command line arguments
Syntax
Description
Arguments
Return Value
Example
Standard
Class
See Also
call get_command_argument(number [, value, length, status])
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
There is not anything specifically stated about what an argument is but in practice the arguments are split on whitespace unless the arguments are quoted and IFS values (Internal Field Separators) used by common shells are ignored.
NUMBER - Shall be a scalar of type integer(4), NUMBER > 0. VALUE - Shall be a scalar of type CHARACTER and of default kind. LENGTH - (Optional) Shall be a scalar of type integer(4). STATUS - (Optional) Shall be a scalar of type integer(4).
After get_command_argument returns, the VALUE argument holds the NUMBER-th command line argument. If VALUE can not hold the argument, it is truncated to fit the length of VALUE. If there are less than NUMBER arguments specified at the command line, VALUE will be filled with blanks. If NUMBER = 0, VALUE is set to the name of the program (on systems that support this feature).
The LENGTH argument contains the length of the NUMBER-th command line argument.
If the argument retrieval fails, STATUS is a positive number; if VALUE contains a truncated command line argument, STATUS is -1; and otherwise the STATUS is zero.
Sample program:
program demo_get_command_argument implicit none integer :: count,i, longest, argument_length integer,allocatable :: istat(:), ilen(:) character(len=:),allocatable :: arguments(:)! get number of arguments count = command_argument_count()
! find longest argument longest=0 do i=0,count call get_command_argument(number=i,length=argument_length) longest=max(longest,argument_length) enddo
! allocate string array big enough to hold command line allocate(character(len=longest) :: arguments(0:count)) allocate(istat(0:count)) allocate(ilen(0:count))
! read the arguments into the array do i=0,count call get_command_argument(i, arguments(i),status=istat(i),length=ilen(i)) enddo
! show the results write (*,(i3.3,1x,i0.5,1x,i0.5,1x,"[",a,"]")) (i,istat(i),ilen(i),arguments(i)(:ilen(i)),i=0,count)
end program demo_get_command_argument
Sample output:
./test_get_command_argument a simple test of getting arguments " from the command" 000 00000 00003 [./test_get_command_argument] 001 00000 00001 [a] 002 00000 00006 [simple] 003 00000 00004 [test] 004 00000 00024 [of getting arguments ] 005 00000 00018 [ from the command]
[[Fortran 2003]] and later
Subroutine
[[get_command]], [[command_argument_count]]
get_command_argument (3) | March 18, 2019 |