Fortran Command Line Arguments Parser

发布于:2025-07-05 ⋅ 阅读:(13) ⋅ 点赞:(0)

Fortran Command Line Arguments Parser

Fortran provides intrinsic ways to handle command line arguments, though it’s not as fully featured as some other languages. Here are the key methods:

Basic Intrinsic Routines

Fortran has three main intrinsic procedures for command line argument handling:

  1. COMMAND_ARGUMENT_COUNT() - Returns the number of arguments
  2. GET_COMMAND_ARGUMENT(NUMBER, VALUE, LENGTH, STATUS) - Gets a specific argument
  3. GET_COMMAND(COMMAND, LENGTH, STATUS) - Gets the entire command line

Example Usage

program cli_example
  implicit none
  integer :: i, arg_count, arg_length, stat
  character(len=100) :: arg_value
  
  ! Get number of arguments
  arg_count = command_argument_count()
  print '(A,I2)', 'Number of arguments: ', arg_count
  
  ! Loop through arguments
  do i = 1, arg_count
    call get_command_argument(i, arg_value, arg_length, stat)
    if (stat == 0) then
      print '(A,I2,A,A)', 'Argument ', i, ': ', trim(arg_value)
    else
      print '(A,I2,A)', 'Error getting argument ', i
    end if
  end do
end program cli_example

More Advanced Parsing

For more complex argument parsing (like option flags), you’ll need to implement your own parser or use a library. Here’s a simple example:

program advanced_parser
  implicit none
  character(len=100) :: arg, filename
  logical :: verbose = .false., help = .false.
  integer :: i
  
  i = 1
  do while (i <= command_argument_count())
    call get_command_argument(i, arg)
    
    select case (arg)
    case ('-h', '--help')
      help = .true.
    case ('-v', '--verbose')
      verbose = .true.
    case ('-f', '--file')
      if (i < command_argument_count()) then
        call get_command_argument(i+1, filename)
        i = i + 1
      else
        print *, 'Error: Missing filename after -f'
        stop
      end if
    case default
      print *, 'Unknown option: ', trim(arg)
    end select
    
    i = i + 1
  end do
  
  ! Now use the parsed options
  if (help) then
    call print_help()
    stop
  end if
  
  if (verbose) print *, 'Verbose mode enabled'
  if (len_trim(filename) > 0) print *, 'Filename: ', trim(filename)
  
contains
  subroutine print_help()
    print *, 'Usage: program [options]'
    print *, 'Options:'
    print *, '  -h, --help     Show this help message'
    print *, '  -v, --verbose  Enable verbose output'
    print *, '  -f, --file     Specify input file'
  end subroutine print_help
end program advanced_parser

Fortran Libraries for Argument Parsing

For more robust parsing, consider these libraries:

  1. M_CLI2 - Modern command line argument parsing

    • GitHub: https://github.com/urbanjost/M_CLI2
  2. FLAP (Fortran command Line Arguments Parser)

    • GitHub: https://github.com/szaghi/FLAP
  3. f90getopt - A Fortran implementation of getopt

    • GitHub: https://github.com/haniibrahim/f90getopt

Example with M_CLI2

program mcli2_example
  use M_CLI2, only : set_args, get_args, unnamed
  implicit none
  character(len=:),allocatable :: file
  integer                      :: i, j
  logical                      :: verbose
  
  call set_args('--file "input.txt" --verbose F --i 10 --j 20')
  call get_args('file',file)
  call get_args('verbose',verbose)
  call get_args('i',i)
  call get_args('j',j)
  
  print *, 'file=',file
  print *, 'verbose=',verbose
  print *, 'i=',i
  print *, 'j=',j
  print *, 'unnamed arguments=',unnamed
end program mcli2_example

Best Practices

  1. Always check the status when getting arguments
  2. Provide meaningful error messages
  3. Include a help option that explains usage
  4. Validate argument values
  5. Consider case sensitivity in your parsing

Would you like more details on any particular aspect of Fortran command line argument parsing?


网站公告

今日签到

点亮在社区的每一天
去签到