ArgManager.hpp

A tool for sythesizing command-line arguments, URL query params, and config files.

Note

Status: BETA

Functions

bool operator==(const ArgSpec &lhs, const ArgSpec &rhs)
struct ArgSpec
#include <ArgManager.hpp>

A helper struct for ArgManager that specifies a single argument type. Note that the primary argument name is specified as the keys of the specs map constructor argument for ArgManager.

Public Functions

inline ArgSpec(const size_t quota_ = 0, const std::string description_ = "No description provided.", const std::unordered_set<std::string> aliases_ = std::unordered_set<std::string>(), const std::function<void(optional<vector<std::string>>)> callback_ = nullptr, const bool gobble_flags_ = false, const bool flatten_ = false)
inline ArgSpec(const size_t most_quota_, const size_t least_quota_, const std::string description_ = "No description provided.", const std::unordered_set<std::string> aliases_ = std::unordered_set<std::string>(), const std::function<void(optional<vector<std::string>>)> callback_ = nullptr, const bool gobble_flags_ = false, const bool flatten_ = false)

Public Members

size_t most_quota

Max words after flag to be used (enforced during parsing and on UseArg request)

size_t least_quota

Minimum words after flag required (enforced during parsing and on UseArg request)

std::string description

User-level description of this flag.

std::unordered_set<std::string> aliases

Set of alternate flag names that will trigger this flag.

std::function<void(optional<vector<std::string>>)> callback

Function that can process this flag and its arguments.

bool gobble_flags

Should this flag collect subsequent flags (that begin with -) as arguments?

bool flatten

If this flag is used multiple times, should we combine all argument packs? (e.g., a single list of words instead of a list of lists of words).

class ArgManager
#include <ArgManager.hpp>

Manager for command line arguments and URL query params.

Public Types

using pack_t = vector<std::string>
using pack_map_t = std::multimap<std::string, pack_t>
using spec_map_t = std::unordered_map<std::string, ArgSpec>

Public Functions

inline ArgManager(int argc, char *argv[], const spec_map_t &specs_ = make_builtin_specs())

Constructor for raw command line arguments. This constructor is first in the constructor daisy chain.

inline ArgManager(const pack_t args, const spec_map_t &specs_ = make_builtin_specs())

This constructor is second in the constructor daisy chain.

inline ArgManager(const pack_map_t &packs_, const spec_map_t &specs_ = make_builtin_specs())

Constructor that bypasses command line argument parsing where argument packs are provided directly, e.g., for use with URL query params. This constructor is last in the constructor daisy chain.

inline ~ArgManager()
inline bool CallbackArg(const std::string &name)

Trigger the callback (if specified) for an argument, consuming an argument pack if available and callback present. If no argpack is available, the callback is triggered with nullopt. Return true if an argument pack was consumed, otherwise false.

inline void UseCallbacks()

Trigger all arguments with callbacks until all pertinent argument packs are consumed.

inline optional<pack_t> UseArg(const std::string &name)

UseArg consumes an argument pack accessed by a certain name.

inline vector<pack_t> ViewArg(const std::string &name) const

ViewArg provides, but doesn’t comsume, all argument packs under a certain name.

inline bool ProcessBuiltin(const Ptr<Config> config = nullptr, std::ostream &os = std::cout)

Process builtin commands. Return bool for “should program proceed” (i.e., true=continue, false=exit).

inline void PrintDiagnostic(std::ostream &os = std::cout) const

Print the current state of the ArgManager; provide diagnostic hints about argument packs remaining.

inline void PrintHelp(std::ostream &os = std::cerr) const

Print information about all known argument types and what they’re for; make pretty.

inline bool HasUnused(std::ostream &os = std::cerr) const

Test if there are any unused argument packs, and if so, output an error message. Returns true if there are any unused argument packs, false otherwise.

Public Static Functions

static inline pack_t args_to_strings(int argc, char *argv[])

Convert input arguments to a vector of strings for easier processing.

static inline pack_map_t parse(pack_t args, const spec_map_t &specs = spec_map_t())

Use argument specifications to convert command line arguments to argument packs.

static inline spec_map_t make_builtin_specs(const Ptr<Config> config = nullptr)

Make specs for builtin commands, including any config adjustment args.

static inline spec_map_t DealiasSpecs(spec_map_t inSpecsMap)

Private Members

pack_map_t packs
const spec_map_t specs
class ArgManager : public vector<std::string>
#include <ArgManager.hpp>

A simple class to manage command-line arguments that were passed in. Derived from vector<std::string>, but with added functionality for argument handling.

Public Functions

inline ArgManager()
inline ArgManager(int argc, char *argv[])
inline ~ArgManager()
template<typename T>
inline int UseArg(const std::string &name, T &var, const std::string &desc = "")

UseArg takes a name, a variable and an optional description. If the name exists, it uses the next argument to change the value of the variable. Return 1 if found, 0 if not found, and -1 if error (no value provided)

inline int UseArg(const std::string &name, Config &config, const std::string &cfg_name, const std::string &desc = "")

UseArg can also take a config object and a name, and use the argument to set the config object.

inline bool UseFlag(const std::string &name, const std::string &desc = "")

UseFlag takes a name and an optional description. If the name exists, return true, otherwise return false.

inline void PrintHelp(std::ostream &os) const

Print information about all known argument types and what they’re for; make pretty.

inline bool HasUnknown(std::ostream &os = std::cerr) const

Test if there are any unprocessed arguments, and if so, output an error.

inline bool TestUnknown(std::ostream &os = std::cerr) const

Leaving TestUnknown for backward compatability; returns opposite of HasUnknown().

inline bool ProcessConfigOptions(Config &config, std::ostream &os, const std::string &cfg_file = "", const std::string &macro_file = "")

Convert settings from a configure object to command-line arguments. Return bool for “should program proceed” (i.e., true=continue, false=exit).

Private Types

using parent_t = vector<std::string>

Private Members

vector<std::string> arg_names
vector<std::string> arg_descs