FlagManager.hpp
This file contains tools for dealing with command-line flags (from argv and argc).
This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2023-2025 Michigan State University MIT Software license; see doc/LICENSE.md
The FlagManager class will take command line arguments (either in its constructor or with the AddFlags() function) and process all options appropriately.
For setup, a FlagManager instance must be configured by calling AddOption once for each flag option, providing a the function to call when the option is triggered. The functions 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 ‘—’ followed by at least one non dash, non whitespace character. If a shortcut character is provided, it must be called with a single ‘-‘.
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 saved as a vector of strings called “extras” and must be processed manually. They can be retrieved with GetExtras().
Note
Status: ALPHA
Defines
-
INCLUDE_EMP_CONFIG_FLAG_MANAGER_HPP_GUARD
-
class FlagInfo
- #include <FlagManager.hpp>
Public Functions
-
FlagInfo() = default
-
inline FlagInfo(String name, String desc, size_t min_args, size_t max_args, fun_t fun, char shortcut = '\0')
-
inline size_t GetMinArgs() const
-
inline size_t GetMaxArgs() const
-
inline char GetShortcut() const
-
FlagInfo() = default
-
class FlagManager
- #include <FlagManager.hpp>
Public Functions
-
FlagManager() = default
-
inline FlagManager(int argc, char *argv[])
-
inline FlagInfo &AddOption(String name, String desc = "")
Add a new option that doesn’t do anything when triggered.
-
inline FlagInfo &AddOption(String name, std::function<void()> fun, String desc = "")
Add an option that doesn’t take any arguments and runs a function when triggered.
-
inline FlagInfo &AddOption(String name, std::function<void(String)> fun, String desc = "")
Add an option that takes one argument that it passes to a function when triggered.
-
inline FlagInfo &AddOption(String name, std::function<void(String, String)> fun, String desc = "")
Add an option that takes two arguments that it passes to a function when triggered.
-
inline FlagInfo &AddOption(String name, std::function<void(const vector<String>&)> fun, size_t min_args = 0, size_t max_args = npos, String desc = "")
Add an option that takes a vector of arguments that it passes to a function when triggered.
-
template<typename FUN_T>
inline FlagInfo &AddOption(char shortcut, String name, FUN_T fun, String desc = "")
-
inline void AddFlags(int argc, char *argv[])
-
inline size_t ProcessArg(char c, size_t cur_pos = 0)
-
inline size_t ProcessOption(char c, size_t cur_pos)
-
inline size_t SkipOption(char c, size_t cur_pos)
-
inline void Process()
-
inline bool FindAndProcess(String option_name)
Process a specific argument, if available (return whether it was found).
Public Static Attributes
-
static constexpr size_t npos = static_cast<size_t>(-1)
Private Functions
Private Members
-
int cur_group = -1
Which group are we currently adding to?
-
struct GroupInfo
-
FlagManager() = default