Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • FORCES FORCES
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 11
    • Issues 11
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 3
    • Merge requests 3
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • CHSCHS
  • FORCESFORCES
  • Merge requests
  • !30

mo_cli: an argument parser to create command line interfaces

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Sebastian Müller requested to merge add_mo_cli into develop May 26, 2021
  • Overview 1
  • Commits 1
  • Pipelines 2
  • Changes 2

This module provides a class cli_parser to create a command line argument parser for a fortran executable. One can define options with a name (--name) and a short name (-n) and set them to have a value or not (-n ). Also default values can be passed and options can be set to "required". A single "blank" option can bedefined, that is not indicated by a hyphenated switch.

See the following example:

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( &
    name="cwd", &
    blank=.true., &
    help="The working directory.")

  call parser%add_option( &
    name="file", &
    s_name="f", &
    has_value=.true., &
    value_name="path", &
    default="nope", &
    help="Your file path.")

  call parser%add_option( &
    name="file2", &
    has_value=.true., &
    value_name="path", &
    required=.true., &
    help="Your 2nd 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

This can be called like:

$ ./prog --file2 f2 -f f1 --opt a/path
 file: f1
 dir: a/path
 opt:  T

Or to display the auto generated help:

$ ./prog -h
This program has a CLI.

  Usage: PROJ [options] <cwd>

Options:
  <cwd>
      Description: The working directory.

  --file2 <path>
      Description: Your 2nd file path.
      (required)

  --help / -h
      Description: Print this help message.

  --version / -v
      Description: Print the version of the program.

  --file / -f <path>
      Description: Your file path.
      Default: nope

  --opt
      Description: A switch
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: add_mo_cli