文章目录
- [Fortran Command Line Arguments Parser](#Fortran Command Line Arguments Parser)
-
- [Basic Intrinsic Routines](#Basic Intrinsic Routines)
-
- [Example Usage](#Example Usage)
- [More Advanced Parsing](#More Advanced Parsing)
- [Fortran Libraries for Argument Parsing](#Fortran Libraries for Argument Parsing)
-
- [Example with M_CLI2](#Example with M_CLI2)
- [Best Practices](#Best Practices)
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:
COMMAND_ARGUMENT_COUNT()
- Returns the number of argumentsGET_COMMAND_ARGUMENT(NUMBER, VALUE, LENGTH, STATUS)
- Gets a specific argumentGET_COMMAND(COMMAND, LENGTH, STATUS)
- Gets the entire command line
Example Usage
fortran
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:
fortran
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:
-
M_CLI2 - Modern command line argument parsing
-
FLAP (Fortran command Line Arguments Parser)
- GitHub: https://github.com/szaghi/FLAP
-
f90getopt - A Fortran implementation of getopt
Example with M_CLI2
fortran
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
- Always check the status when getting arguments
- Provide meaningful error messages
- Include a help option that explains usage
- Validate argument values
- Consider case sensitivity in your parsing
Would you like more details on any particular aspect of Fortran command line argument parsing?