FlagManager.hpp

This file contains tools for dealing with command-line flags (from argv and argc).

The FlagManager class will take command line arguments (either in its constructor or with the AddFlags() function) and process them appropriately.

For setup, the user must run AddOption with the function to call. Functions to call can take zero, one, or two Strings as arguments OR they can take a vector of Strings and the range of allowed arguments should be specified. When Process() is run, the appropriate function will be called on each and any invalid arguments will trigger an error.

Flags are expected to begin with a ‘-’ and non-flags are expected to NOT begin with a ‘-‘.

If a single dash is followed by multiple characters, each will be processed independently. So, “-abc” will be the same as “-a -b -c”.

Extra command line arguments will be saves as a vector of strings called “extras” and must be processed manually. They can be retrieved with GetExtras().

Todo:

: Make variable numbers of flag arguments work.

Note

Status: ALPHA

class FlagManager
#include <FlagManager.hpp>

Public Functions

inline FlagManager()
inline FlagManager(int argc, char *argv[])
inline String &operator[](size_t pos)
inline const String &operator[](size_t pos) const
inline vector<String> GetExtras() const
inline size_t Find(String pattern) const
inline bool Has(String pattern) const
inline bool Use(String pattern)
inline void AddOption(String name, String desc = "")
inline void AddOption(String name, std::function<void()> fun, String desc = "")
inline void AddOption(String name, std::function<void(String)> fun, String desc = "")
inline void AddOption(String name, std::function<void(String, String)> fun, String desc = "")
inline void AddOption(String name, std::function<void(const vector<String>&)> fun, size_t min_args = 0, size_t max_args = npos, String desc = "")
template<typename FUN_T>
inline void AddOption(char shortcut, String name, FUN_T fun, String desc = "")
inline void AddFlags(int argc, char *argv[])
inline size_t ProcessArg(String name, size_t cur_pos = 0)
inline size_t ProcessArg(char c, size_t cur_pos = 0)
inline size_t ProcessFlagSet(String name, size_t cur_pos = 0)
inline void Process()
inline void PrintOptions(std::ostream &os = std::cout) const

Public Static Attributes

static constexpr size_t npos = static_cast<size_t>(-1)

Private Members

vector<String> args
vector<String> extras
std::map<String, FlagInfo> flag_options
std::map<char, String> shortcuts
struct FlagInfo

Public Members

String desc
size_t min_args = 0
size_t max_args = 0
std::function<void(const vector<String>&)> fun
char shortcut = '\0'