Procedures for command-line argument parsing.
Create an array of argument types, then read and parse the arguments:
character(len=72) :: input
integer :: delay, rc
logical :: verbose
type(arg_type) :: args(3)
args = [ &
arg_type('input', short='i', type=ARG_TYPE_STRING, required=.true.), &
arg_type('delay', short='x', type=ARG_TYPE_INTEGER), &
arg_type('verbose', short='V', type=ARG_TYPE_LOGICAL) &
]
rc = dm_arg_read(args, app='myapp', major=1, minor=0, patch=0)
call dm_error_out(rc)
call dm_arg_get(args(1), input)
call dm_arg_get(args(2), delay)
call dm_arg_get(args(3), verbose)
Each argument requires name and type. The default type is
ARG_TYPE_LOGICAL
. The command-line arguments --help
/-h
and
--version
/-v
are processed automatically by function dm_arg_read()
.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | parameter | :: | ARG_TYPE_NONE | = | 0 |
None type (invalid). |
integer, | public, | parameter | :: | ARG_TYPE_LOGICAL | = | 1 |
Logical argument. |
integer, | public, | parameter | :: | ARG_TYPE_INTEGER | = | 2 |
Integer value. |
integer, | public, | parameter | :: | ARG_TYPE_REAL | = | 3 |
Real value. |
integer, | public, | parameter | :: | ARG_TYPE_CHAR | = | 4 |
Single character value. |
integer, | public, | parameter | :: | ARG_TYPE_STRING | = | 5 |
Character string value. |
integer, | public, | parameter | :: | ARG_TYPE_ID | = | 6 |
Valid ID value. |
integer, | public, | parameter | :: | ARG_TYPE_UUID | = | 7 |
Valid UUIDv4 value. |
integer, | public, | parameter | :: | ARG_TYPE_TIME | = | 8 |
Valid ISO 8601 value. |
integer, | public, | parameter | :: | ARG_TYPE_LEVEL | = | 9 |
Log level (name string or integer value). |
integer, | public, | parameter | :: | ARG_TYPE_FILE | = | 10 |
Path to file on file system (must exist). |
integer, | public, | parameter | :: | ARG_TYPE_DATABASE | = | 11 |
Path to database on file system (must exist). |
integer, | public, | parameter | :: | ARG_TYPE_LAST | = | 11 |
Never use this. |
integer, | public, | parameter | :: | ARG_NAME_LEN | = | 32 |
Maximum length of argument name. |
integer, | public, | parameter | :: | ARG_VALUE_LEN | = | FILE_PATH_LEN |
Maximum length of argument value. |
Returns argument value.
Returns argument value as 4-byte integer.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | arg |
Arg type. |
||
integer, | intent(inout) | :: | value |
Argument value. |
||
integer, | intent(in), | optional | :: | default |
Default value. |
|
logical, | intent(out), | optional | :: | passed |
Passed or not. |
|
integer, | intent(out), | optional | :: | error |
Argument error. |
Returns .true.
if argument has been passed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | arg |
Arg type. |
||
logical, | intent(inout) | :: | value |
Argument value. |
||
logical, | intent(in), | optional | :: | default |
Default value. |
|
logical, | intent(out), | optional | :: | passed |
Passed or not. |
|
integer, | intent(out), | optional | :: | error |
Argument error. |
Returns argument value as 8-byte real.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | arg |
Arg type. |
||
real(kind=r8), | intent(inout) | :: | value |
Argument value. |
||
real(kind=r8), | intent(in), | optional | :: | default |
Default value. |
|
logical, | intent(out), | optional | :: | passed |
Passed or not. |
|
integer, | intent(out), | optional | :: | error |
Argument error. |
Returns argument value as character string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | arg |
Arg type. |
||
character(len=*), | intent(inout) | :: | value |
Argument value. |
||
character(len=*), | intent(in), | optional | :: | default |
Default value. |
|
logical, | intent(out), | optional | :: | passed |
Passed or not. |
|
integer, | intent(out), | optional | :: | error |
Argument error. |
Argument description type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=ARG_NAME_LEN), | public | :: | name | = | ' ' |
Identifier of the argument (without leading --). |
|
character(len=1), | public | :: | short | = | ASCII_NUL |
Short argument character. |
|
character(len=ARG_VALUE_LEN), | public | :: | value | = | ' ' |
Default and passed value (if any). |
|
integer, | public | :: | length | = | 0 |
Value length. |
|
integer, | public | :: | max_len | = | ARG_VALUE_LEN |
Maximum argument value length. |
|
integer, | public | :: | min_len | = | 0 |
Minimum argument value length. |
|
integer, | public | :: | type | = | ARG_TYPE_LOGICAL |
Value data type. |
|
logical, | public | :: | required | = | .false. |
Option is mandatory. |
|
logical, | public | :: | passed | = | .false. |
Option was passed. |
|
integer, | public | :: | error | = | E_NONE |
Occured error. |
Returns .true.
if argument of given name is passed without value.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | name |
Name of command-line argument. |
||
character(len=1), | intent(in), | optional | :: | short |
Short name. |
Parses command-line for given arguments. Error messages are printed
to standard error by default, unless verbose
is .false.
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | args(:) |
Arguments array. |
||
logical, | intent(in), | optional | :: | ignore_unknown |
Allow unknown arguments. |
|
logical, | intent(in), | optional | :: | verbose |
Print error messages to stderr. |
Reads all arguments from command-line and prints error message if one is missing. Returns the error code of the first invalid argument.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | args(:) |
Arguments to match. |
||
character(len=*), | intent(in), | optional | :: | app |
App name (for |
|
integer, | intent(in), | optional | :: | major |
Major version number (for |
|
integer, | intent(in), | optional | :: | minor |
Minor version number (for |
|
integer, | intent(in), | optional | :: | patch |
Patch level (for |
|
character(len=*), | intent(in), | optional | :: | version |
Additional version string. |
Returns .true.
if passed type is a valid argument type.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | type |
Argument type ( |
Validates given argument. Arguments of type ARG_TYPE_LEVEL
are
additionally converted to integer if the passed argument value is a
valid log level name. For example, the argument value warning
is
converted to integer 3
, to match log level LL_WARNING
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | arg |
Argument to validate. |
Prints command-line arguments to standard output if --help
or -h
is passed.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(arg_type), | intent(inout) | :: | args(:) |
Arguments array. |