Clicky

Parsing Command-Line Options in Fortran 2003

September 17, 2009

For programs with only a few simple command-line options, it isn’t too difficult to parse them yourself, especially given Fortran 2003’s new intrinsic functions command_argument_count and get_command_argument. Below is a simple example program which, by default, prints the current date and exits. It also accepts options to print the version, usage, or the current time. An error message is displayed if an invalid option is given.

! cmdline.f90 -- simple command-line argument parsing example

program cmdline
  implicit none

  character(len=*), parameter :: version = '1.0'
  character(len=32) :: arg
  character(len=8) :: date
  character(len=10) :: time
  character(len=5) :: zone
  logical :: do_time = .false.
  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 ('-t', '--time')
        do_time = .true.
     case default
        print '(a,a,/)', 'Unrecognized command-line option: ', arg
        call print_help()
        stop
     end select
  end do

  ! Print the date and, optionally, the time
  call date_and_time(DATE=date, TIME=time, ZONE=zone)
  write (*, '(a,"-",a,"-",a)', advance='no') date(1:4), date(5:6), date(7:8)
  if (do_time) then
     write (*, '(x,a,":",a,x,a)') time(1:2), time(3:4), zone
  else
     write (*, '(a)') ''
  end if

contains

  subroutine print_help()
    print '(a)', 'usage: cmdline [OPTIONS]'
    print '(a)', ''
    print '(a)', 'Without further options, cmdline prints the date and exits.'
    print '(a)', ''
    print '(a)', 'cmdline options:'
    print '(a)', ''
    print '(a)', '  -v, --version     print version information and exit'
    print '(a)', '  -h, --help        print usage information and exit'
    print '(a)', '  -t, --time        print time'
  end subroutine print_help

end program cmdline

Source: cmdline.f90.

For more complex programs, several libraries are available for handling command-line arguments. See the command-line arguments page on the FortranWiki for details.