E-MailRelay

Public Member Functions | Static Public Member Functions | List of all members
G::Arg Class Reference

A class which holds a represention of the argc/argv command line array, and supports simple command-line parsing. More...

`#include <garg.h>`

## Public Member Functions
| Arg (int argc, char **argv)
| Constructor taking argc/argv. More...
| Arg (const G::StringArray &)
| Constructor taking an array of command-line arguments. More...
| Arg ()
| Default constructor. Initialise with parse().
void | parse (HINSTANCE hinstance, const std::string &command_line_tail)
| Parses the given command-line tail, splitting it up into an array of tokens. More...
void | parse (const std::string &command_line)
| Parses the given command line, splitting it up into an array of tokens. More...
void | reparse (const std::string &command_line_tail)
| Reinitialises the object with the given command-line tail. More...
std::size_t | c () const
| Returns the number of tokens in the command line, including the program name. More...
std::string | v (std::size_t i) const
| Returns the i'th argument. More...
std::string | v (std::size_t i, const std::string &default_) const
| Returns the i'th argument or the default if out of range. More...
std::string | prefix () const
| Returns the basename of v(0) without any extension. More...
bool | contains (const std::string &option, std::size_t option_args=0U, bool case_sensitive=true) const
| Returns true if the command line contains the given option with enough command line arguments left to satisfy the required number of option arguments. More...
std::size_t | count (const std::string &option)
| Returns the number of times the given string appears in the list of arguments. More...
std::size_t | index (const std::string &option, std::size_t option_args=0U, std::size_t default_=0U) const
| Returns the index of the given option. More...
std::size_t | match (const std::string &prefix) const
| Returns the index of the first argument that matches the given prefix. More...
bool | remove (const std::string &option, std::size_t option_args=0U)
| Removes the given option and its arguments. More...
std::string | removeAt (std::size_t option_index, std::size_t option_args=0U)
| Removes the given argument and the following 'option_args' ones. More...
StringArray | array (unsigned int shift=0U) const
| Returns the arguments as a string array, with an optional shift. More...

## Static Public Member Functions
static std::string | v0 ()
| Returns a copy of argv[0] from the first call to the constructor that takes argc/argv. More...
static std::string | exe (bool do_throw=true)
| Returns Process::exe() or an absolute path constructed from v0() and possibly using the cwd. More...
static const char * | prefix (char **argv) noexcept
| An exception-free version of prefix() which can be used in main() outside of the outermost try block. More...

## Detailed Description

A class which holds a represention of the argc/argv command line array, and supports simple command-line parsing.

A copy of argv[0] is squirrelled away and made accessible via a static method.

See also
  G::GetOpt

Definition at line 43 of file garg.h.

## Constructor & Destructor Documentation

## ◆ Arg() [1/2]

G::Arg::Arg | ( | int | argc,
|  | char ** | argv
| ) |  |

Constructor taking argc/argv.

Should not be used in a shared object or dll.

Definition at line 34 of file garg.cpp.

## ◆ Arg() [2/2]

G::Arg::Arg | ( | const G::StringArray & | args | ) |
| explicit

Constructor taking an array of command-line arguments.

The program name in the first position is expected but may be ignored.

Definition at line 49 of file garg.cpp.

## Member Function Documentation

## ◆ array()

G::StringArray G::Arg::array | ( | unsigned int | shift = `0U` | ) | const

Returns the arguments as a string array, with an optional shift.

A shift of one will remove the program name.

Definition at line 85 of file garg.cpp.

## ◆ c()

std::size_t G::Arg::c | ( |  | ) | const

Returns the number of tokens in the command line, including the program name.

Definition at line 169 of file garg.cpp.

## ◆ contains()

bool G::Arg::contains | ( | const std::string & | option,
|  | std::size_t | option_args = `0U`,
|  | bool | case_sensitive = `true`
| ) |  | const

Returns true if the command line contains the given option with enough command line arguments left to satisfy the required number of option arguments.

(By convention an option starts with a dash, but that is not required here; it's just a string that is matched against command-line arguments.)

Definition at line 93 of file garg.cpp.

## ◆ count()

std::size_t G::Arg::count | ( | const std::string & | option | ) |

Returns the number of times the given string appears in the list of arguments.

Definition at line 98 of file garg.cpp.

## ◆ exe()

std::string G::Arg::exe | ( | bool | do_throw = `true` | ) |
| static

Returns Process::exe() or an absolute path constructed from v0() and possibly using the cwd.

Throws on error by default, or optionally returns the empty string. See also v0().

Definition at line 212 of file garg.cpp.

## ◆ index()

std::size_t G::Arg::index | ( | const std::string & | option,
|  | std::size_t | option_args = `0U`,
|  | std::size_t | default_ = `0U`
| ) |  | const

Returns the index of the given option.

Returns zero (or the given default) if not present.

Definition at line 161 of file garg.cpp.

## ◆ match()

std::size_t G::Arg::match | ( | const std::string & | prefix | ) | const

Returns the index of the first argument that matches the given prefix.

Returns zero if none.

Definition at line 120 of file garg.cpp.

## ◆ parse() [1/2]

void G::Arg::parse | ( | const std::string & | command_line | ) |

Parses the given command line, splitting it up into an array of tokens.

The command-line should start with the v(0) value.

Definition at line 66 of file garg.cpp.

## ◆ parse() [2/2]

void G::Arg::parse | ( | HINSTANCE | hinstance,
|  | const std::string & | command_line_tail
| ) |  |

Parses the given command-line tail, splitting it up into an array of tokens.

The v(0) value comes from the supplied instance handle.

Definition at line 57 of file garg.cpp.

## ◆ prefix() [1/2]

std::string G::Arg::prefix | ( |  | ) | const

Returns the basename of v(0) without any extension.

Typically used as a prefix in error messages.

Definition at line 185 of file garg.cpp.

## ◆ prefix() [2/2]

const char * G::Arg::prefix | ( | char ** | argv | ) |
| static noexcept

An exception-free version of prefix() which can be used in main() outside of the outermost try block.

Definition at line 192 of file garg.cpp.

## ◆ remove()

bool G::Arg::remove | ( | const std::string & | option,
|  | std::size_t | option_args = `0U`
| ) |  |

Removes the given option and its arguments.

Returns false if the option does not exist.

Definition at line 137 of file garg.cpp.

## ◆ removeAt()

std::string G::Arg::removeAt | ( | std::size_t | option_index,
|  | std::size_t | option_args = `0U`
| ) |  |

Removes the given argument and the following 'option_args' ones.

Returns v(option_index+(option_args?1:0),"").

Definition at line 146 of file garg.cpp.

## ◆ reparse()

void G::Arg::reparse | ( | const std::string & | command_line_tail | ) |

Reinitialises the object with the given command-line tail.

The command-line should not contain the program name: the v(0) value and prefix() are unchanged.

Definition at line 73 of file garg.cpp.

## ◆ v() [1/2]

std::string G::Arg::v | ( | std::size_t | i | ) | const

Returns the i'th argument.

Precondition: i < c()

Definition at line 174 of file garg.cpp.

## ◆ v() [2/2]

std::string G::Arg::v | ( | std::size_t | i,
|  | const std::string & | default_
| ) |  | const

Returns the i'th argument or the default if out of range.

Definition at line 180 of file garg.cpp.

## ◆ v0()

std::string G::Arg::v0 | ( |  | ) |
| static

Returns a copy of argv[0] from the first call to the constructor that takes argc/argv.

Returns the empty string if that constructor overload has never been used. See also exe().

Definition at line 80 of file garg.cpp.

The documentation for this class was generated from the following files:

- /usr/src/slapt-src-x86_64/network/emailrelay/emailrelay-2.3/src/glib/garg.h
- /usr/src/slapt-src-x86_64/network/emailrelay/emailrelay-2.3/src/glib/garg.cpp