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:

! cmdline.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
                call exit(0)
            case ('-h', '--help')
                call print_help()
                call exit(0)
            case default
                print '(2a, /)', 'Unrecognised command-line option: ', arg
                call print_help()
                call exit(0)
        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:

$ gfortran7 -Wl,-rpath=/usr/local/lib/gcc7/ -o cmdline cmdline.f90
$ ./cmdline --help
command-line options:

  -v, --version     print version information and exit
  -h, --help        print usage information and exit

This approach lacks functionality to read complex options with additional arguments. The Fortran Wiki lists several mature libraries for this purpose. For instance, the f90getopt module allows short and long arguments:

! cmdopt.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
                call exit(0)
        end select
    end do

    print '(a, i3)', 'number of attempts: ', attempts
end program main

Compile the example with:

$ gfortran7 -c f90getopt.f90
$ gfortran7 -Wl,-rpath=/usr/local/lib/gcc7/ -o cmdopt cmdopt.f90 f90getopt.o
$ ./cmdopt --version
version 1.0

References