HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArgParse Class Reference

#include <argparse.h>

Classes

class  Arg
 

Public Types

using Action = std::function< void(cspan< const char * > myargs)>
 Holder for a callback that takes a span of C strings as arguments. More...
 
using ArgAction = std::function< void(Arg &arg, cspan< const char * > myargs)>
 
typedef std::function< void(const
ArgParse &ap, std::ostream &)> 
callback_t
 

Public Member Functions

 ArgParse ()
 Construct an ArgParse. More...
 
 ~ArgParse ()
 Destroy an ArgParse. More...
 
 ArgParse (int argc, const char **argv)
 
 ArgParse (const ArgParse &)=delete
 
const ArgParseoperator= (const ArgParse &)=delete
 
 ArgParse (ArgParse &&other)
 Move constructor. More...
 
ArgParseintro (string_view str)
 
ArgParseusage (string_view str)
 
ArgParsedescription (string_view str)
 
ArgParseepilog (string_view str)
 
ArgParseprog (string_view str)
 
ArgParseprint_defaults (bool print)
 
ArgParseadd_help (bool add_help)
 
ArgParseexit_on_error (bool exit_on_error)
 
void abort (bool aborted=true)
 
bool aborted () const
 Reveal whether the current state is aborted. More...
 
int parse_args (int argc, const char **argv)
 
bool has_error () const
 Is there a pending error message waiting to be retrieved? More...
 
std::string geterror (bool clear=true) const
 
std::string prog_name () const
 
void print_help () const
 
void briefusage () const
 
std::string command_line () const
 Return the entire command-line as one string. More...
 
Argadd_argument (const char *argname)
 
template<typename... T>
Argadd_argument (const char *argname, T...args)
 
Argarg (const char *argname)
 Shorter synonym for add_argument(). More...
 
template<typename... T>
Argarg (const char *argname, T...args)
 Shorter synonym for add_argument(). More...
 
Argseparator (string_view text)
 
AttrDelegate< const
ParamValueList
operator[] (string_view name) const
 Access a single argument result by name. More...
 
AttrDelegate< ParamValueListoperator[] (string_view name)
 Access a single argument result by name. More...
 
ParamValueListparams ()
 Directly access the ParamValueList that holds the argument results. More...
 
const ParamValueListcparams () const
 
int options (const char *intro,...)
 
int parse (int argc, const char **argv)
 
void set_preoption_help (callback_t callback)
 
void set_postoption_help (callback_t callback)
 
void usage () const
 

Static Public Member Functions

static ArgAction store_true ()
 Return an action that stores 1 into its destination attribute. More...
 
static ArgAction store_false ()
 Return an action that stores 0 into its destination attribute. More...
 
template<typename T >
static ArgAction store_const (const T &value)
 
static ArgAction store_const (const char *value)
 
template<typename T = ustring>
static ArgAction store ()
 
template<typename T = ustring>
static ArgAction append ()
 
static Action do_nothing ()
 

Friends

class ArgOption
 

Detailed Description

Argument Parsing. Kind of resembles Python argparse library.

Set up argument parser:

ArgParse ap;
ap.intro("myapp does good things")
  .usage("myapp [options] filename...");
ap.arg("filename")
  .hidden()
  .action([&](cspan<const char*> argv){ filenames.emplace_back(argv[0]); });

Declare arguments. Some examples of common idioms:

// Boolean option (no arguments)
ap.arg("-v")
  .help("verbose mode")
  .action(ArgParse::store_true());

// integer option
ap.arg("-passes NPASSES")
   .help("number of passes")
   .defaultval(1)
   .action(ArgParse::store<int>);

// An option that takes 3 float arguments, like a V3f
ap.arg("-camera X Y Z")
  .help("set the camera position")
  .defaultval(Imath::V3f(0.0f, 0.0f, -1.0f))
  .action(ArgParse::store<float>());

// Positional argument -- append strings
ap.arg("filename")
  .action(ArgParse::append())
  .hidden();

Parse the command line:

ap.parse (argc, argv);

Extract the values like they are attributes in a ParamValueList:

int passes = ap["passes"].get<int>();
bool verbose = ap["verbose"].get<int>();
Imath::V3f camera = ap["camera"].get<Imath::V3f>();

Definition at line 174 of file argparse.h.

Member Typedef Documentation

typedef std::function<void(const ArgParse& ap, std::ostream&)> ArgParse::callback_t

Definition at line 751 of file argparse.h.

Member Function Documentation

int ArgParse::options ( const char *  intro,
  ... 
)
int ArgParse::parse ( int  argc,
const char **  argv 
)
inline

Definition at line 748 of file argparse.h.

void ArgParse::set_postoption_help ( callback_t  callback)
void ArgParse::set_preoption_help ( callback_t  callback)
void ArgParse::usage ( ) const
inline

Definition at line 758 of file argparse.h.

Friends And Related Function Documentation

friend class ArgOption
friend

Definition at line 731 of file argparse.h.


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