0.6.2-dev0
FORCES
FORtran lib for Comp. Env. Sys.
Loading...
Searching...
No Matches
mo_cli::cli_parser Module Reference

This is a parser for command line arguments. More...

Public Member Functions

procedure add_option (self, name, s_name, help, has_value, value_name, default, required, blank, repeated)
 Add a new option to the cli_parser.
 
procedure get_option (self, name)
 Get an option from cli_parser by name.
 
procedure get_option_index (self, name, long, short, raise_error)
 Get the option index from cli_parser by name.
 
procedure cnt_options (self)
 Get option count from the cli_parser.
 
procedure option_was_read (self, name)
 Whether the option was read by the cli_parser given by name.
 
procedure option_read_count (self, name)
 Read count for the option in the cli_parser given by name.
 
procedure has_option (self, name)
 Whether the option is defined in cli_parser given by name.
 
procedure get_blank_option_index (self)
 Get the index of the blank option.
 
procedure option_value (self, name)
 Get the parsed value from an option by name from the cli_parser.
 
procedure print_help (self)
 Print help message for the cli_parser.
 
procedure parse (self)
 Parse the given command line arguments with the cli_parser.
 

Public Attributes

character(:), allocatable prog
 Program name (default will be arg(0)).
 
character(:), allocatable description
 help text for the cli
 
character(:), allocatable version
 Program version.
 
logical has_help = .true.
 whether the parser cares about the help text (–help / -h)
 
logical has_version = .false.
 whether the parser cares about the version text (–version / -V)
 
logical has_blank_option = .false.
 whether the parser has a blank option.
 
logical has_logger = .false.
 whether the parser should setup the logger.
 
type(option), dimension(:), allocatable options
 defined options
 

Detailed Description

This is a parser for command line arguments.

Version
0.1
Authors
Sebastian Mueller
Date
May 2021

A simple parser for command line arguments. You can define options and then parse the given command. Option can be with or without passed values and they can be set as required.

The following example demonstrates the functionality:

program main
use mo_cli, only: cli_parser
implicit none
type(cli_parser) :: parser
parser = cli_parser( &
description="This program has a CLI.", &
add_version_option=.true., version="1.3")
call parser%add_option( &
"cwd", &
blank=.true., &
required=.true., &
help="The working directory.")
call parser%add_option( &
name="file", &
s_name="f", &
has_value=.true., &
value_name="path", &
default="none", &
help="Your file path.")
call parser%add_option("opt", help="A switch")
call parser%parse()
print*, "file: ", parser%option_value("file")
print*, "dir: ", parser%option_value("cwd")
print*, "opt: ", parser%option_was_read("opt")
end program main
Module to parse command line arguments.
Definition mo_cli.f90:60
This is a parser for command line arguments.
Definition mo_cli.f90:100

You can call the program with:

$ ./prog --opt -f file.txt /dir/
file: file.txt
dir: /dir/
opt: T

As you see, you can automatically create help and version options:

$ ./prog -h
$ ./prog -V

Definition at line 100 of file mo_cli.f90.

Member Function/Subroutine Documentation

◆ add_option()

procedure mo_cli::cli_parser::add_option ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name,
character(1), intent(in), optional  s_name,
character(*), intent(in), optional  help,
logical, intent(in), optional  has_value,
character(*), intent(in), optional  value_name,
character(*), intent(in), optional  default,
logical, intent(in), optional  required,
logical, intent(in), optional  blank,
logical, intent(in), optional  repeated 
)

Add a new option to the cli_parser.

Parameters
[in]namelong name (will be double hyphenated: –opt)
[in]s_nameshort name (will be hyphenated: -o)
[in]helpdescription of the option
[in]has_valuewhether the option has a value
[in]value_namename of the value for the help text (default "value")
[in]defaultdefault value for this option
[in]requiredwhether the option is required
[in]blankwhether the option is passed blank without hyphenated name (only latter one possible)
[in]repeatedwhether the option can be read repeatedly
See also
mo_cli::add_option

Definition at line 111 of file mo_cli.f90.

◆ cnt_options()

procedure mo_cli::cli_parser::cnt_options ( class(cli_parser), intent(inout)  self)

Get option count from the cli_parser.

Returns
Option count.
See also
mo_cli::cnt_options

Definition at line 117 of file mo_cli.f90.

◆ get_blank_option_index()

procedure mo_cli::cli_parser::get_blank_option_index ( class(cli_parser), intent(inout)  self)

Get the index of the blank option.

Returns
The desired option index.
See also
mo_cli::get_blank_option_index

Definition at line 125 of file mo_cli.f90.

◆ get_option()

procedure mo_cli::cli_parser::get_option ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name 
)

Get an option from cli_parser by name.

Returns
The desired option.
Parameters
[in]namename (long or short) of the desired option
See also
mo_cli::get_option

Definition at line 113 of file mo_cli.f90.

◆ get_option_index()

procedure mo_cli::cli_parser::get_option_index ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name,
logical, intent(in), optional  long,
logical, intent(in), optional  short,
logical, intent(in), optional  raise_error 
)

Get the option index from cli_parser by name.

Returns
The desired option index.
Parameters
[in]namename of the desired option
[in]longwhether to check long name (default: .true.)
[in]shortwhether to check short name (default: .true.)
[in]raise_errorwhether to raise an error if option is not found (default: .true.)
See also
mo_cli::get_option_index

Definition at line 115 of file mo_cli.f90.

◆ has_option()

procedure mo_cli::cli_parser::has_option ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name 
)

Whether the option is defined in cli_parser given by name.

Returns
Truth value if the given option was defined.
Parameters
[in]namename of the desired option
See also
mo_cli::has_option

Definition at line 123 of file mo_cli.f90.

◆ option_read_count()

procedure mo_cli::cli_parser::option_read_count ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name 
)

Read count for the option in the cli_parser given by name.

Returns
Number of reads for the option.
Parameters
[in]namename of the desired option
See also
mo_cli::option_read_count

Definition at line 121 of file mo_cli.f90.

◆ option_value()

procedure mo_cli::cli_parser::option_value ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name 
)

Get the parsed value from an option by name from the cli_parser.

Returns
Value of the given option.
Parameters
[in]namename of the desired option
See also
mo_cli::option_value

Definition at line 127 of file mo_cli.f90.

◆ option_was_read()

procedure mo_cli::cli_parser::option_was_read ( class(cli_parser), intent(inout)  self,
character(*), intent(in)  name 
)

Whether the option was read by the cli_parser given by name.

Returns
Truth value if the given option was read.
Parameters
[in]namename of the desired option
See also
mo_cli::option_was_read

Definition at line 119 of file mo_cli.f90.

◆ parse()

procedure mo_cli::cli_parser::parse ( class(cli_parser), intent(inout)  self)

Parse the given command line arguments with the cli_parser.

See also
mo_cli::parse

Definition at line 131 of file mo_cli.f90.

◆ print_help()

procedure mo_cli::cli_parser::print_help ( class(cli_parser), intent(inout)  self)

Print help message for the cli_parser.

See also
mo_cli::print_help

Definition at line 129 of file mo_cli.f90.

Member Data Documentation

◆ description

character(:), allocatable mo_cli::cli_parser::description

help text for the cli

Definition at line 102 of file mo_cli.f90.

◆ has_blank_option

logical mo_cli::cli_parser::has_blank_option = .false.

whether the parser has a blank option.

Definition at line 106 of file mo_cli.f90.

◆ has_help

logical mo_cli::cli_parser::has_help = .true.

whether the parser cares about the help text (–help / -h)

Definition at line 104 of file mo_cli.f90.

◆ has_logger

logical mo_cli::cli_parser::has_logger = .false.

whether the parser should setup the logger.

Definition at line 107 of file mo_cli.f90.

◆ has_version

logical mo_cli::cli_parser::has_version = .false.

whether the parser cares about the version text (–version / -V)

Definition at line 105 of file mo_cli.f90.

◆ options

type(option), dimension(:), allocatable mo_cli::cli_parser::options

defined options

Definition at line 108 of file mo_cli.f90.

◆ prog

character(:), allocatable mo_cli::cli_parser::prog

Program name (default will be arg(0)).

Definition at line 101 of file mo_cli.f90.

◆ version

character(:), allocatable mo_cli::cli_parser::version

Program version.

Definition at line 103 of file mo_cli.f90.


The documentation for this module was generated from the following file: