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 Types

using fun_t = std::function<void(const vector<String>&)>

Public Functions

FlagInfo() = default
FlagInfo(const FlagInfo&) = default
inline FlagInfo(String name, String desc, size_t min_args, size_t max_args, fun_t fun, char shortcut = '\0')
FlagInfo &operator=(const FlagInfo&) = default
inline const String &GetName() const
inline const String &GetDesc() const
inline size_t GetMinArgs() const
inline size_t GetMaxArgs() const
inline fun_t GetFun() const
inline char GetShortcut() const
inline const String &GetGroup() const
inline FlagInfo &SetDesc(const String &in)
inline FlagInfo &SetMinArgs(size_t in)
inline FlagInfo &SetMaxArgs(size_t in)
inline FlagInfo &SetFun(fun_t in)
inline FlagInfo &SetShortcut(char in)
inline FlagInfo &SetGroup(String in)
template<typename ...Ts>
inline void Run(Ts&&... args)

Private Members

String name

Name used for this option.

String desc

Name to type to trigger option. E.g. “–help”.

size_t min_args = 0

Minimum number of arguments option needs to operate.

size_t max_args = 0

Maximum number of arguments option can handle.

fun_t fun

Function to run when this option is selected.

char shortcut = '\0'

Single-letter shortcut for this option. E.g., ‘h’ is for “-h”.

String group = "none"

Which option group does this belong to?

class FlagManager
#include <FlagManager.hpp>

Public Functions

FlagManager() = default
inline FlagManager(int argc, char *argv[])
inline String &operator[](size_t pos)
inline const String &operator[](size_t pos) const
inline const vector<String> &GetExtras() const
inline size_t Find(String pattern) const
inline bool Has(String pattern) const
inline bool Use(String pattern)
inline int AddGroup(String name, String desc = "")

Add a new option group.

inline int SetGroup(String name)

Change the option group back to a previously created group.

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(String name, size_t cur_pos = 0)
inline size_t ProcessArg(char c, size_t cur_pos = 0)
inline size_t ProcessOption(String name, size_t cur_pos)
inline size_t ProcessOption(char c, size_t cur_pos)
inline size_t ProcessFlagSet(String name, size_t cur_pos = 0)
inline size_t SkipOption(String name, size_t cur_pos)
inline size_t SkipOption(char c, size_t cur_pos)
inline size_t SkipFlagSet(String name, size_t cur_pos = 0)
inline void Process()
inline bool FindAndProcess(String option_name)

Process a specific argument, if available (return whether it was found).

inline const vector<String> &ProcessExtras()

ONLY process to collect the extras; don’t trigger any actual flags yet.

inline size_t GroupSize(String group_name) const
inline void PrintGroupOptions(String group, std::ostream &os = std::cout) const
inline void PrintOptions(std::ostream &os = std::cout) const

Public Static Attributes

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

Private Functions

inline int _FindGroupID(const String &name)
inline FlagInfo &_AddOption(String name, std::function<void(const vector<String>&)> fun, size_t min_args = 0, size_t max_args = npos, String desc = "")

Private Members

vector<String> args

Command-line arguments to be processed.

vector<String> extras

Set of command line arguments not handled by FlagManager.

std::map<String, FlagInfo> flag_options

Set of flags known by this manager.

std::map<char, String> shortcuts

Single-character shortcuts to particular flags.

vector<GroupInfo> groups

Info about flag groups to organize “help”.

int cur_group = -1

Which group are we currently adding to?

struct GroupInfo

Public Members

String name
String desc