dm_arg Module

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().


Uses

  • module~~dm_arg~~UsesGraph module~dm_arg dm_arg module~dm_ascii dm_ascii module~dm_arg->module~dm_ascii module~dm_error dm_error module~dm_arg->module~dm_error module~dm_file dm_file module~dm_arg->module~dm_file module~dm_kind dm_kind module~dm_arg->module~dm_kind module~dm_util dm_util module~dm_arg->module~dm_util module~dm_error->module~dm_ascii module~dm_error->module~dm_kind module~dm_file->module~dm_error module~dm_file->module~dm_kind iso_fortran_env iso_fortran_env module~dm_kind->iso_fortran_env module~dm_util->module~dm_error module~dm_util->module~dm_kind

Used by

  • module~~dm_arg~~UsedByGraph module~dm_arg dm_arg module~dmpack dmpack module~dmpack->module~dm_arg

Variables

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_DB = 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.


Interfaces

public interface dm_arg_get

Returns argument value.

  • private subroutine arg_get_int32(arg, value, default, passed, error)

    Returns argument value as 4-byte integer.

    Arguments

    Type IntentOptional 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.

  • private subroutine arg_get_logical(arg, value, default, passed, error)

    Returns .true. if argument has been passed.

    Arguments

    Type IntentOptional 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.

  • private subroutine arg_get_real64(arg, value, default, passed, error)

    Returns argument value as 8-byte real.

    Arguments

    Type IntentOptional 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.

  • private subroutine arg_get_string(arg, value, default, passed, error)

    Returns argument value as character string.

    Arguments

    Type IntentOptional 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.


Derived Types

type, public ::  arg_type

Argument description type.

Components

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.


Functions

public function dm_arg_has(name, short) result(has)

Returns .true. if argument of given name is passed without value.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Name of command-line argument.

character(len=1), intent(in), optional :: short

Short name.

Return Value logical

public function dm_arg_parse(args, ignore_unknown, verbose) result(rc)

Parses command-line for given arguments. Error messages are printed to standard error by default, unless verbose is .false..

Read more…

Arguments

Type IntentOptional 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.

Return Value integer

public function dm_arg_read(args, app, major, minor, patch, version) result(rc)

Reads all arguments from command-line and prints error message if one is missing. Returns the error code of the first invalid argument.

Read more…

Arguments

Type IntentOptional Attributes Name
type(arg_type), intent(inout) :: args(:)

Arguments to match.

character(len=*), intent(in), optional :: app

App name (for -v).

integer, intent(in), optional :: major

Major version number (for -v).

integer, intent(in), optional :: minor

Minor version number (for -v).

integer, intent(in), optional :: patch

Patch level (for -v).

character(len=*), intent(in), optional :: version

Additional version string.

Return Value integer

public pure elemental function dm_arg_type_is_valid(type) result(valid)

Returns .true. if passed type is a valid argument type.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: type

Argument type (ARG_TYPE_*).

Return Value logical

public function dm_arg_validate(arg) result(rc)

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.

Arguments

Type IntentOptional Attributes Name
type(arg_type), intent(inout) :: arg

Argument to validate.

Return Value integer


Subroutines

public subroutine dm_arg_help(args)

Prints command-line arguments to standard output if --help or -h is passed.

Arguments

Type IntentOptional Attributes Name
type(arg_type), intent(inout) :: args(:)

Arguments array.