command_line.hpp

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

Functions here include:

vector<std::string> args_to_strings(int argc, char* argv[]);

  • Convert the standard command-line args to a more managable vector of strings.

int find_arg(const vector<std::string> & args, const std::string & pattern);

  • Return index where a specified argument can be found (or -1 if it doesn’t exist).

bool has_arg(const vector<std::string> & args, const std::string & pattern);

  • Return true if a particular argument was set on the command line

bool use_arg(vector<std::string> & args, const std::string & pattern);

  • Same as has_arg(), but remove the argument from the set of available args.

Development notes:

  • Add has_flag() and use_flag() functions to more gracefully handle flags. For example, if -a and -b are legal flags, -ab should trigger both of them.

  • Process arguments from left to right, rather than out of order?

  • Identify errors if arguments to a flag begin with a ‘-’ and should be a flag themselves? Or, more generally, recognize if an argument is clearly

namespace cl

Functions

vector<std::string> args_to_strings(int argc, char *argv[])
template<typename T>
T read_arg_pos(const vector<std::string> &args, size_t pos, T default_val = T())
int find_arg(const vector<std::string> &args, const std::string &pattern)
bool has_arg(const vector<std::string> &args, const std::string &pattern)
bool use_arg(vector<std::string> &args, const std::string &pattern)
template<typename T>
int get_arg_value(vector<std::string> &args, const std::string &pattern, T &var)
int get_arg_value(vector<std::string> &args, const std::string &pattern, std::string &var1, std::string &var2)
template<typename ...Ts>
int use_arg_value(vector<std::string> &args, const std::string &pattern, Ts&... vars)