E-MailRelay
Classes | 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 directly from main(). More...
 
 Arg (const G::StringArray &)
 Constructor taking an array of command-line arguments. More...
 
 Arg (const std::string &full_command_line)
 Constructor taking a full command-line. More...
 
 Arg (const Path &exe, const std::string &command_line_tail)
 Constructor taking argv0 and a 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 (std::string_view 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 (std::string_view option) const
 Returns the number of times the given string appears in the list of arguments. More...
 
std::size_t index (std::string_view 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 (std::string_view option, std::size_t option_args=0U)
 Removes the given option and its arguments. More...
 
std::string removeValue (std::string_view option, const std::string &default_={})
 Removes the given single-valued option and its value. 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...
 
StringArray::const_iterator cbegin () const
 Returns a begin iterator, advanced to exclude argv0. More...
 
StringArray::const_iterator cend () const
 Returns the end iterator. More...
 

Static Public Member Functions

static Arg windows ()
 Factory function for Windows using GetCommandLineW(). More...
 
static Path v0 ()
 Returns a copy of argv[0] from the first call to the argc/argv constructor overload or windows(). More...
 
static Path exe ()
 Returns Process::exe() or in exceptional circumstances an absolute path constructed from v0() and the captured cwd. More...
 
static Path exe (std::nothrow_t)
 Returns Process::exe() or in exceptional circumstances an absolute path constructed from v0() and the captured 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 49 of file garg.h.

Constructor & Destructor Documentation

◆ Arg() [1/4]

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

Constructor taking argc/argv directly from main().

Sets the v0() path and captures the cwd.

Definition at line 34 of file garg.cpp.

◆ Arg() [2/4]

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.

◆ Arg() [3/4]

G::Arg::Arg ( const std::string &  full_command_line)
explicit

Constructor taking a full command-line.

Definition at line 64 of file garg.cpp.

◆ Arg() [4/4]

G::Arg::Arg ( const Path exe,
const std::string &  command_line_tail 
)

Constructor taking argv0 and a command-line tail.

The first parameter is typically exe().

Definition at line 55 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 88 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 182 of file garg.cpp.

◆ cbegin()

G::StringArray::const_iterator G::Arg::cbegin ( ) const

Returns a begin iterator, advanced to exclude argv0.

Definition at line 267 of file garg.cpp.

◆ cend()

G::StringArray::const_iterator G::Arg::cend ( ) const

Returns the end iterator.

Definition at line 273 of file garg.cpp.

◆ contains()

bool G::Arg::contains ( std::string_view  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 96 of file garg.cpp.

◆ count()

std::size_t G::Arg::count ( std::string_view  option) const

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

Definition at line 102 of file garg.cpp.

◆ exe() [1/2]

G::Path G::Arg::exe ( )
static

Returns Process::exe() or in exceptional circumstances an absolute path constructed from v0() and the captured cwd.

Throws on error. See also v0().

Definition at line 228 of file garg.cpp.

◆ exe() [2/2]

G::Path G::Arg::exe ( std::nothrow_t  )
static

Returns Process::exe() or in exceptional circumstances an absolute path constructed from v0() and the captured cwd.

Returns the empty path on error. See also v0().

Definition at line 234 of file garg.cpp.

◆ index()

std::size_t G::Arg::index ( std::string_view  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 174 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 125 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 198 of file garg.cpp.

◆ prefix() [2/2]

const char * G::Arg::prefix ( char **  argv)
staticnoexcept

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

Definition at line 205 of file garg.cpp.

◆ remove()

bool G::Arg::remove ( std::string_view  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 142 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),""). Does nothing and returns the empty string if the index is zero or out of range.

Definition at line 159 of file garg.cpp.

◆ removeValue()

std::string G::Arg::removeValue ( std::string_view  option,
const std::string &  default_ = {} 
)

Removes the given single-valued option and its value.

Returns the option value or the default if the option does not exist.

Definition at line 151 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 187 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 193 of file garg.cpp.

◆ v0()

G::Path G::Arg::v0 ( )
static

Returns a copy of argv[0] from the first call to the argc/argv constructor overload or windows().

Returns the empty path if those methods have never been called successfully. See also exe().

Definition at line 82 of file garg.cpp.

◆ windows()

G::Arg G::Arg::windows ( )
static

Factory function for Windows using GetCommandLineW().

Also sets the v0() path and captures the cwd.

Definition at line 70 of file garg.cpp.


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