![]() |
libSUFR
a LIBrary of Some Useful Fortran Routines
|
Procedures for a getopt and getopt_long implementation to parse command-line parameters in Fortran. More...
Data Types | |
type | getopt_t |
Struct to define short and long options for getopt_long() More... | |
Functions/Subroutines | |
character function | getopt (optstr) |
Parse a command-line parameter and return short options and their arguments. A warning is printed to stderr if an argument is required but not present. | |
character function | getopt_long (longopts) |
Parse a command-line parameter and return short and/or long options and their arguments. A warning is printed to stderr if an argument is required but not present or vice versa. | |
subroutine | getopt_get_command () |
Get the full command line in a string, split compound short options (e.g. "-abc" -> "-a -b -c") and set numberOfArguments. | |
integer function | getopt_command_argument_count () |
Return the number of arguments on the command line (excluding the command). | |
subroutine | getopt_get_command_argument (argnr, arg) |
Returns the argNr-th command-line argument or option. | |
integer function | getopt_optarg_to_int (minvalue, maxvalue) |
Extract and return an integer value from the argument optArg. On error, report and abort. | |
real function | getopt_optarg_to_real (minvalue, maxvalue) |
Extract and return a real value from the argument optArg. On error, report and abort. | |
real(double) function | getopt_optarg_to_dbl (minvalue, maxvalue) |
Extract and return a double value from the argument optArg. On error, report and abort. | |
subroutine | getopt_split_short_options (cloptstr) |
Split combined short command-line options into several individual ones, e.g. "... -abc ..." -> "... -a -b -c ...". | |
subroutine | getopt_help (optstr, linebef, lineaft) |
Print a help list of all short options and their required arguments. | |
subroutine | getopt_long_help (longopts, linebef, lineaft) |
Print a help list of all short/long options, their required arguments and their descriptions. | |
Variables | |
character, dimension(9999) | commandline |
The full command line as a single string. | |
integer | numberofarguments = -1 |
The number of arguments on the command line, excluding the command (= argc-1 in C) | |
character, dimension(999) | curarg |
The current argument or option (word) | |
character, dimension(999) | optarg |
The option's argument, if required and present. | |
character, dimension(longoptlen+2) | longoption |
The short or long option found, including leading dash(es) | |
integer, save | optcount = 0 |
The current option count. | |
character, dimension(999) | getopthelpheader = '' |
The header line for the message printed by getopt(_long)_help() | |
character, dimension(999) | getopthelpsyntax = '' |
The syntax line for the message printed by getopt(_long)_help() | |
character, dimension(999) | getopthelpfooter = '' |
The footer line for the message printed by getopt(_long)_help() | |
Procedures for a getopt and getopt_long implementation to parse command-line parameters in Fortran.
Getopt implementation in Fortran, similar but not identical to the GlibC implementation. Two possibilities are available:
Both functions return the letter of the option (e.g. 'a' for -a) and set the variable optArg which contains the argument if required and present. In addition, getopt_long() sets the variable longOption which contains the full option found (e.g. "-a" or "--all"). Both functions and variables can be accessed through the SUFR_getopt module.
In addition, a quick help output can be generated (e.g. when no option, a help option or a non-existing option is given) using:
The general idea comes from [1] and [2], while I followed [3] for the return values indication an issue (>!.). Unlike [3], I wanted both short and long options, allow the argument to be glued to the option (e.g. -ffile.txt) and get a warning if a required argument is missing. Unlike [2] I thought a non-OOP solution might be simpler. In addition, I wanted to allow an equal sign in e.g. –file=file.txt, and to provide a short description for each option, in order to simplify the generation of an explanatory list of options, which is provided through getopt_help() and getopt_long_help().
character function sufr_getopt::getopt | ( | character, dimension(*), intent(in) | optstr | ) |
Parse a command-line parameter and return short options and their arguments. A warning is printed to stderr if an argument is required but not present.
optStr | String defining the allowed short options. Characters followed by a colon have a required argument. Example: 'ab:c' for -a, -b <arg> and -c. |
getopt | Either:
|
The following 'global' variables are set (accessible through the module SUFR_getopt):
Definition at line 95 of file getopt.f90.
References curarg, getopt(), getopt_command_argument_count(), getopt_get_command(), getopt_get_command_argument(), optarg, and optcount.
Referenced by getopt().
integer function sufr_getopt::getopt_command_argument_count |
Return the number of arguments on the command line (excluding the command).
getopt_command_argument_count | Number of arguments excluding the command |
Definition at line 336 of file getopt.f90.
References getopt_command_argument_count(), getopt_get_command(), and numberofarguments.
Referenced by getopt(), getopt_command_argument_count(), and getopt_long().
subroutine sufr_getopt::getopt_get_command |
Get the full command line in a string, split compound short options (e.g. "-abc" -> "-a -b -c") and set numberOfArguments.
Definition at line 317 of file getopt.f90.
References commandline, sufr_text::count_substring(), getopt_split_short_options(), and numberofarguments.
Referenced by getopt(), getopt_command_argument_count(), getopt_get_command_argument(), and getopt_long().
subroutine sufr_getopt::getopt_get_command_argument | ( | integer, intent(in) | argnr, |
character, dimension(99), intent(out) | arg ) |
Returns the argNr-th command-line argument or option.
argNr | Number of the desired argument |
arg | Content of the desired argument (a word/string) |
Definition at line 354 of file getopt.f90.
References commandline, getopt_get_command(), sufr_text::int2str(), numberofarguments, and sufr_system::quit_program_error().
Referenced by getopt(), and getopt_long().
subroutine sufr_getopt::getopt_help | ( | character, dimension(*), intent(in) | optstr, |
integer, intent(in), optional | linebef, | ||
integer, intent(in), optional | lineaft ) |
Print a help list of all short options and their required arguments.
optStr | Short-options string used for getopt() |
lineBef | Number of lines to print before option list (default: 0) |
lineAft | Number of lines to print after option list (default: 0) |
Definition at line 573 of file getopt.f90.
References getopthelpfooter, getopthelpheader, getopthelpsyntax, and sufr_constants::program_name.
character function sufr_getopt::getopt_long | ( | type(getopt_t), dimension(:), intent(in) | longopts | ) |
Parse a command-line parameter and return short and/or long options and their arguments. A warning is printed to stderr if an argument is required but not present or vice versa.
longopts | Long-options used for getopt_long(). This is an array of getopt_t structs, each of which contains:
|
getopt_long | Either:
|
The following 'global' variables are set (accessible through the module SUFR_getopt):
Definition at line 194 of file getopt.f90.
References curarg, getopt_command_argument_count(), getopt_get_command(), getopt_get_command_argument(), getopt_long(), longoption, optarg, and optcount.
Referenced by getopt_long().
subroutine sufr_getopt::getopt_long_help | ( | type(getopt_t), dimension(:), intent(in) | longopts, |
integer, intent(in), optional | linebef, | ||
integer, intent(in), optional | lineaft ) |
Print a help list of all short/long options, their required arguments and their descriptions.
longopts | Long-options struct used for getopt_long() |
lineBef | Number of lines to print before option list (default: 0) |
lineAft | Number of lines to print after option list (default: 0) |
Definition at line 642 of file getopt.f90.
References getopthelpfooter, getopthelpheader, getopthelpsyntax, and sufr_constants::program_name.
real(double) function sufr_getopt::getopt_optarg_to_dbl | ( | real(double), intent(in), optional | minvalue, |
real(double), intent(in), optional | maxvalue ) |
Extract and return a double value from the argument optArg. On error, report and abort.
minValue | Minimum acceptable value for this argument (optional). |
maxValue | Maximum acceptable value for this argument (optional). |
getopt_optarg_to_dbl | The integer value of the argument |
Definition at line 462 of file getopt.f90.
References sufr_text::dbl2str(), sufr_kinds::double, getopt_optarg_to_dbl(), longoption, optarg, and sufr_system::quit_program_error().
Referenced by getopt_optarg_to_dbl().
integer function sufr_getopt::getopt_optarg_to_int | ( | integer, intent(in), optional | minvalue, |
integer, intent(in), optional | maxvalue ) |
Extract and return an integer value from the argument optArg. On error, report and abort.
minValue | Minimum acceptable value for this argument (optional). |
maxValue | Maximum acceptable value for this argument (optional). |
getopt_optarg_to_int | The integer value of the argument |
Definition at line 387 of file getopt.f90.
References getopt_optarg_to_int(), sufr_text::int2str(), longoption, optarg, and sufr_system::quit_program_error().
Referenced by getopt_optarg_to_int().
real function sufr_getopt::getopt_optarg_to_real | ( | real, intent(in), optional | minvalue, |
real, intent(in), optional | maxvalue ) |
Extract and return a real value from the argument optArg. On error, report and abort.
minValue | Minimum acceptable value for this argument (optional). |
maxValue | Maximum acceptable value for this argument (optional). |
getopt_optarg_to_real | The real value of the argument |
Definition at line 424 of file getopt.f90.
References getopt_optarg_to_real(), longoption, optarg, sufr_system::quit_program_error(), and sufr_text::real2str().
Referenced by getopt_optarg_to_real().
subroutine sufr_getopt::getopt_split_short_options | ( | character, dimension(*), intent(inout) | cloptstr | ) |
Split combined short command-line options into several individual ones, e.g. "... -abc ..." -> "... -a -b -c ...".
CLoptStr | String containing (all) command-line options (I/O). |
Definition at line 498 of file getopt.f90.
References sufr_dummy::dumdbl.
Referenced by getopt_get_command().
character, dimension(9999) sufr_getopt::commandline |
The full command line as a single string.
Definition at line 55 of file getopt.f90.
Referenced by getopt_get_command(), and getopt_get_command_argument().
character, dimension(999) sufr_getopt::curarg |
The current argument or option (word)
Definition at line 58 of file getopt.f90.
Referenced by getopt(), and getopt_long().
character, dimension(999) sufr_getopt::getopthelpfooter = '' |
The footer line for the message printed by getopt(_long)_help()
Definition at line 65 of file getopt.f90.
Referenced by getopt_help(), and getopt_long_help().
character, dimension(999) sufr_getopt::getopthelpheader = '' |
The header line for the message printed by getopt(_long)_help()
Definition at line 63 of file getopt.f90.
Referenced by getopt_help(), and getopt_long_help().
character, dimension(999) sufr_getopt::getopthelpsyntax = '' |
The syntax line for the message printed by getopt(_long)_help()
Definition at line 64 of file getopt.f90.
Referenced by getopt_help(), and getopt_long_help().
character, dimension(longoptlen+2) sufr_getopt::longoption |
The short or long option found, including leading dash(es)
Definition at line 60 of file getopt.f90.
Referenced by getopt_long(), getopt_optarg_to_dbl(), getopt_optarg_to_int(), and getopt_optarg_to_real().
integer sufr_getopt::numberofarguments = -1 |
The number of arguments on the command line, excluding the command (= argc-1 in C)
Definition at line 56 of file getopt.f90.
Referenced by getopt_command_argument_count(), getopt_get_command(), and getopt_get_command_argument().
character, dimension(999) sufr_getopt::optarg |
The option's argument, if required and present.
Definition at line 59 of file getopt.f90.
Referenced by getopt(), getopt_long(), getopt_optarg_to_dbl(), getopt_optarg_to_int(), and getopt_optarg_to_real().
integer, save sufr_getopt::optcount = 0 |
The current option count.
Definition at line 61 of file getopt.f90.
Referenced by getopt(), and getopt_long().