Skip to content

Instantly share code, notes, and snippets.

@fspaolo
Last active December 19, 2015 10:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fspaolo/5942135 to your computer and use it in GitHub Desktop.

Command line args in Fortran 90/95

Although I'm not a big fan of Fortran, here is a simple example of parsing Unix-like command line arguments to a program following the modern standard of F2003. Note that the following example also works for any F90/F95 code compiled with gfortran or g95.

Example:

$ ./thisprog -h 

usage: ./thisprog [-h] [-a ARG_A] [-b ARG_B] [-c]

options:
  -h, --help  print the help message and exit
  -a ARG_A    print the argument ARG_A
  -b ARG_B    print the argument ARG_B
  -c          print the default argument for -c

Code:

program main
   implicit none
 
   character(100) :: arg, arg_a, arg_b
   character(*), parameter :: arg_c = 'Hello!'
   logical :: jump = .false.
   integer :: i
 
   call get_arguments()
 
contains
 
   subroutine get_arguments()
      if (command_argument_count() < 1) call print_help()
 
      do i = 1, command_argument_count()  ! iterate over arguments
         if (jump) then                   ! jump one iteration
             jump = .false.
             cycle
         endif
         call get_command_argument(i, arg)
 
         select case (arg)
            case ('-h', '--help')
               call print_help()
            case ('-a')
               call get_command_argument(i+1, arg_a)
               if (arg_a(:1) == '-' .or. arg_a == '') call print_help()  ! check the arg is correct
               print '(a)', trim(arg_a)
               jump = .true.
            case ('-b')
               call get_command_argument(i+1, arg_b)
               if (arg_b(:1) == '-' .or. arg_b == '') call print_help()
               print '(a)', trim(arg_b)
               jump = .true.
            case ('-c')
               print '(a)', arg_c
            case default
               print '(a,a,/)', 'unrecognized command line option: ', trim(arg)
               call print_help()
         endselect
      enddo
   end subroutine get_arguments
 
   subroutine print_help()
      print '(a)', 'usage: ./thisprog [-h] [-a ARG_A] [-b ARG_B] [-c]'
      print '(a)', ''
      print '(a)', 'options:'
      print '(a)', '  -h, --help  print the help message and exit'
      print '(a)', '  -a ARG_A    print the argument ARG_A'
      print '(a)', '  -b ARG_B    print the argument ARG_B'
      print '(a)', '  -c          print the default argument for -c'
      stop
   end subroutine print_help
 
end program main

Read more about command line arguments.
This example is modified from: http://jblevins.org/log/cmdline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment