0.6.2-dev0
FORCES
FORtran lib for Comp. Env. Sys.
Loading...
Searching...
No Matches
mo_cli.f90 File Reference

Module to parse command line arguments. More...

Go to the source code of this file.

Data Types

module  mo_cli::option
 This is a container for a single command line option. More...
 
module  mo_cli::cli_parser
 This is a parser for command line arguments. More...
 

Modules

module  mo_cli
 Module to parse command line arguments.
 

Functions/Subroutines

type(cli_parser) function mo_cli::new_cli_parser (prog, description, add_help_option, add_version_option, version, add_logger_options)
 Create a new cli_parser.
 
type(option) function mo_cli::new_option (name, s_name, help, has_value, value_name, default, required, blank, repeated)
 Create a new option.
 
subroutine mo_cli::add_option (self, name, s_name, help, has_value, value_name, default, required, blank, repeated)
 Add a new option to the cli_parser.
 
integer(i4) function mo_cli::cnt_options (self)
 Get option count from the cli_parser.
 
logical function mo_cli::is_given_arg (self, arg)
 check if this option is the given argument.
 
integer(i4) function mo_cli::get_option_index (self, name, long, short, raise_error)
 Get the option index from cli_parser by name.
 
type(option) function mo_cli::get_option (self, name)
 Get an option from cli_parser by name.
 
logical function mo_cli::option_was_read (self, name)
 Whether the option was read by the cli_parser given by name.
 
integer(i4) function mo_cli::option_read_count (self, name)
 Read count for the option in the cli_parser given by name.
 
logical function mo_cli::has_option (self, name)
 Whether the option is defined in cli_parser given by name.
 
integer(i4) function mo_cli::get_blank_option_index (self)
 Get the index of the blank option.
 
character(:) function, allocatable mo_cli::option_value (self, name)
 Get the parsed value from an option by name from the cli_parser.
 
subroutine mo_cli::print_info (self)
 Print info for an option.
 
subroutine mo_cli::print_help (self)
 Print help message for the cli_parser.
 
subroutine mo_cli::parse (self)
 Parse the given command line arguments with the cli_parser.
 
subroutine mo_cli::parse_arg (arg, names, counts)
 Parse given argument.
 
integer(i4) function mo_cli::findchar (array, chr)
 

Detailed Description

Module to parse 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 in file mo_cli.f90.