fsc::ArgParser Class Reference

#include <ArgParser.hpp>

Description

an argument parser that does not require registration.

Usage:

For details and more examples, see the fsc::ArgParser member function documentation.

#include <iostream>
int main(int argc, char* argv[]) {
fsc::ArgParser ap(argc, argv);// creates an ArgParser and
// parses the command line
ap.parse_file("cline.txt"); // reads cline.txt and parses the content
// an already set argument will be overwritten
ap["n"]; // get the named argument "n" if set,
// otherwise a std::runtime_error is thrown
ap.def("n", 10); // set named argument "n" to 10
// if "n" is not already set
int n = ap.get("n", 10); // get named argument "n" if set, otherwise 10
std::cout << n << std::endl;
ap.is_set("f"); // true if "f" is a flag or named argument
ap[0]; // get free argument at position 0,
// or std::runtime_error if not set
ap.is_set(1); // true if there is a free argument at pos 1
ap.freeargc(); // returns the number of free arguments
ap.cwd(); // return the cwd
ap.pwd(); // return the pwd
ap.progname(); // return the name of the binary
std::cout << ap << std::endl; // print the ArgParser
return 0;
}
Examples:
basic_example.cpp, merge_example.cpp, mixed_example.cpp, and weak_type_example.cpp.

Definition at line 83 of file ArgParser.hpp.

Public Member Functions

 ArgParser () noexcept
 default constructor More...
 
 ArgParser (int const &argc, char *argv[])
 construct from command line More...
 
 ArgParser (std::string const &cline)
 string constructor More...
 
poly_type const & operator[] (std::string const &key) const
 get named argument if set More...
 
poly_type const & operator[] (size_type const &key) const
 get free argument if set More...
 
template<typename T >
get (std::string const &key, T const &def) const
 get named argument if set, else a default More...
 
std::string get (std::string const &key, char const *def) const
 special treatment of char const * More...
 
template<typename T >
get (size_type const &pos, T const &def) const
 get free argument if set, else a default More...
 
std::string get (size_type const &pos, char const *def) const
 special treatment of char const * More...
 
std::string const & cwd () const noexcept
 returns the current working directory More...
 
std::string const & pwd () const
 returns the program working directory More...
 
std::string const & progname () const
 returns the program name More...
 
size_type freeargc () const noexcept
 returns the number of free arguments
 
bool is_set (std::string const &key) const noexcept
 check if flag or named argument is set More...
 
bool is_set (size_type const &pos) const noexcept
 check if free argument is set More...
 
template<typename T >
void def (std::string const &key, T const &def)
 set a named argument if not already set More...
 
void def (std::string const &key)
 set a flag More...
 
void merge (ArgParser const &rhs, bool const &overwrite=true) noexcept
 merge two argument parsers More...
 
bool parse_file (std::string const &filename, bool const &overwrite=true)
 read a file, parse the content and merge into this More...
 
template<typename S >
void print (S &os) const noexcept
 prints the argument parser in a verbose form More...
 

Constructor & Destructor Documentation

fsc::ArgParser::ArgParser ( )
inlinenoexcept

default constructor

does not generate pwd and progname.

Definition at line 95 of file ArgParser.hpp.

fsc::ArgParser::ArgParser ( int const &  argc,
char *  argv[] 
)
inline

construct from command line

Parameters
argclength of argv
argvarray of arguments given via command line

the only constructor that can set pwd and progname since it can be extracted from argv[0]. Throws a std::runtime_error if the command line is ill-formed.

Definition at line 106 of file ArgParser.hpp.

fsc::ArgParser::ArgParser ( std::string const &  cline)
inlineexplicit

string constructor

Parameters
clinea string that simulates a command line

construct the ArgParser with a string instead of the command line to parse. Throws a std::runtime_error if the string is ill-formed. Does not generate pwd and progname.

Definition at line 121 of file ArgParser.hpp.

Member Function Documentation

std::string const& fsc::ArgParser::cwd ( ) const
inlinenoexcept

returns the current working directory

the cwd should always be set

Examples:
mixed_example.cpp.

Definition at line 202 of file ArgParser.hpp.

template<typename T >
void fsc::ArgParser::def ( std::string const &  key,
T const &  def 
)
inline

set a named argument if not already set

Parameters
keyname of the named argument
defis a default that is set if there is no named argument key

If there is already a flag with name key a std::runtime_error is thown.

Examples:
mixed_example.cpp.

Definition at line 257 of file ArgParser.hpp.

void fsc::ArgParser::def ( std::string const &  key)
inline

set a flag

Parameters
keyname of the flag

If there is already a named argument with name key a std::runtime_error is thown.

Definition at line 267 of file ArgParser.hpp.

template<typename T >
T fsc::ArgParser::get ( std::string const &  key,
T const &  def 
) const
inline

get named argument if set, else a default

Parameters
keyname of the named argument
defis a default that is returned if key is not found

If key is found but not convertibly to decltype(def), a std::runtime_error is thrown.

Examples:
mixed_example.cpp.

Definition at line 156 of file ArgParser.hpp.

std::string fsc::ArgParser::get ( std::string const &  key,
char const *  def 
) const
inline

special treatment of char const *

Parameters
keyname of the named argument
defis a default that is returned if key is not found

If key is found but not convertibly to std::string, a std::runtime_error is thrown.

Definition at line 169 of file ArgParser.hpp.

template<typename T >
T fsc::ArgParser::get ( size_type const &  pos,
T const &  def 
) const
inline

get free argument if set, else a default

Parameters
posposition of the free argument
defis a default that is returned if there is no free argument at position pos

If the free argument at pos is found but not convertibly to decltype(def), a std::runtime_error is thrown.

Definition at line 181 of file ArgParser.hpp.

std::string fsc::ArgParser::get ( size_type const &  pos,
char const *  def 
) const
inline

special treatment of char const *

Parameters
posposition of the free argument
defis a default that is returned if there is no free argument at position pos

If pos is found but not convertibly to std::string, a std::runtime_error is thrown.

Definition at line 195 of file ArgParser.hpp.

bool fsc::ArgParser::is_set ( std::string const &  key) const
inlinenoexcept

check if flag or named argument is set

Parameters
keyis a named parameter or flag

returns true if key is a flag or named argument. Note that it is not allowed to have a flag and named argument with the same name.

Examples:
mixed_example.cpp.

Definition at line 236 of file ArgParser.hpp.

bool fsc::ArgParser::is_set ( size_type const &  pos) const
inlinenoexcept

check if free argument is set

Parameters
posis the position of a potential free argument

returns true if pos < freeargc().

Definition at line 244 of file ArgParser.hpp.

void fsc::ArgParser::merge ( ArgParser const &  rhs,
bool const &  overwrite = true 
)
inlinenoexcept

merge two argument parsers

Parameters
rhsis the argument parser that is merged into this one.
overwritedefines the merging-behavior on conflicts

The merging policy works as follows:

  • named arguments are merged, on conflict take rhs if overwrite == true (a std::runtime_error is thrown if there is a flag - named argument collision)
  • the flags are merged (a std::runtime_error is thrown if there is a flag - named argument collision)
  • free arguments are NOT merged in any way, since the position is important. If overwrite == true the free arguments are overwritten by the free arguments of rhs, otherwise nothing is done.
  • cwd, pwd and progname are also overwritten by rhs if overwrite == true, otherwise nothing is done.
Examples:
merge_example.cpp.

Definition at line 289 of file ArgParser.hpp.

poly_type const& fsc::ArgParser::operator[] ( std::string const &  key) const
inline

get named argument if set

Parameters
keyis the name of the desired named argument

If key is not found, a std::runtime_error is thrown.

Definition at line 132 of file ArgParser.hpp.

poly_type const& fsc::ArgParser::operator[] ( size_type const &  key) const
inline

get free argument if set

Parameters
keyposition of the free argument

If key >= freeargc() a std::runtime_error is thrown, i.e. there is no free argument at the position key

Definition at line 143 of file ArgParser.hpp.

bool fsc::ArgParser::parse_file ( std::string const &  filename,
bool const &  overwrite = true 
)
inline

read a file, parse the content and merge into this

Parameters
filenameis the name of the input file
overwritedefines the merging-behavior on conflicts

the file is loaded an parsed in a seperate ArgParser inastance an the merged into this. For the mergin behavior see merge .

Examples:
mixed_example.cpp.

Definition at line 319 of file ArgParser.hpp.

template<typename S >
void fsc::ArgParser::print ( S &  os) const
inlinenoexcept

prints the argument parser in a verbose form

Parameters
osan std::ostream like object

Definition at line 339 of file ArgParser.hpp.

std::string const& fsc::ArgParser::progname ( ) const
inline

returns the program name

Only works if the progname is set, which only happens in the command line constructor. If not set, a std::runtime_error is thrown.

Examples:
mixed_example.cpp.

Definition at line 220 of file ArgParser.hpp.

std::string const& fsc::ArgParser::pwd ( ) const
inline

returns the program working directory

Only works if the pwd is set, which only happens in the command line constructor. If not set, a std::runtime_error is thrown.

Examples:
mixed_example.cpp.

Definition at line 210 of file ArgParser.hpp.


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