Tag Archives: arguments

Fortran get_command_argument

Fortran 2003 is a major extension to Fortran 90/95 including many useful features, one significant feature is access to command arguments; this allows a program to take data from the execution command line and use this arguments as input information.

program cmdline
  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)', 'cmdline version ', version
        stop
     case ('-h', '--help')
        call print_help()
        stop
     case default
        print '(a,a,/)', 'Unrecognized command-line option: ', arg
        call print_help()
        stop
     end select
  end do

contains

  subroutine print_help()
    print '(a)', 'usage: cmdline [OPTIONS]'
    print '(a)', 'cmdline 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 cmdline