Programming in Modern Fortran
Command-Line Arguments
Modern Fortran compilers provide the intrinsic
get_command_argument()
routine to read command-line arguments:
call get_command_argument(number[, value][, length][, status]) |
||
---|---|---|
Argument | Type | Description |
number |
integer | Argument to return. |
value |
character | String that contains the value of the requested argument (optional). |
length |
integer | Length of the requested argument on return (optional). |
status |
integer | Returns 0 on success, -1 if value was
too short to contain the actual argument, and 1 if
argument could not be returned (optional). |
The function command_argument_count()
returns the number of
given arguments. The whole command-line can be accessed with
get_command()
:
call get_command(command[, length][, status]) |
||
---|---|---|
Argument | Type | Description |
command |
character | Command-line to return. |
length |
integer | Length of the command-line or 0 if the length
cannot be determined (optional). |
status |
integer | Returns 0 on success, -1 if value was
too short to contain the actual argument, and 1 if
argument could not be returned (optional). |
The following example utilises command_argument_count()
and
get_command_argument()
:
! example.f90
program main
implicit none
character(len=*), parameter :: VERSION = '1.0'
character(len=32) :: arg
integer :: i
do i = 1, command_argument_count()
call get_command_argument(i, arg)
select case (arg)
case ('-v', '--version')
print '(2a)', 'version ', VERSION
stop
case ('-h', '--help')
call print_help()
stop
case default
print '(2a, /)', 'unrecognised command-line option: ', arg
call print_help()
stop
end select
end do
print '(a)', 'Hello, World!'
contains
subroutine print_help()
print '(a, /)', 'command-line options:'
print '(a)', ' -v, --version print version information and exit'
print '(a, /)', ' -h, --help print usage information and exit'
end subroutine print_help
end program main
Run the program with one of the implemented command-line arguments to output the wanted information:
$ gfortran13 -o example example.f90
$ ./example --help
command-line options:
-v, --version print version information and exit
-h, --help print usage information and exit
$ ./example --version
version 1.0
This basic approach lacks functionality to read complex options with additional arguments.
f90getopt
The Fortran Wiki lists several mature libraries for command-line input. For instance, the f90getopt (GNU GPL, v. 2) module allows short and long arguments:
! example.f90
program main
use :: f90getopt
implicit none
character(len=*), parameter :: VERSION = '1.0'
integer :: attempts = 1
type(option_s) :: opts(2)
opts(1) = option_s('attempts', .true., 'a')
opts(2) = option_s('version', .false., 'v')
do
select case (getopt('a:v', opts))
case (char(0))
exit
case ('g')
read (optarg, '(i3)') attempts
case ('v')
print '(a, f3.1)', 'version ', VERSION
stop
end select
end do
print '(a, i3)', 'number of attempts: ', attempts
end program main
Compile and run the example with:
$ gfortran13 -c f90getopt.f90
$ gfortran13 -o example example.f90 f90getopt.o
$ ./example --version
version 1.0
Fortran Libraries
- FLAP: Fortran 2003 command-line arguments parser “for poor people”
- fArgParse: Command-line argument parser for Fortran, part of the Goddard Fortran Ecosystem
- General-Purpose Fortran (GPF): Library that contains a command-line parser
- M_CLI: Unix-like command-line parsing
References
- Fortran Wiki: Command-line arguments
- J. Blevins: Parsing command-line options in Fortran 2003, 2009
< File System Directories | [Index] | Environment Variables > |