Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
CommandLineParser.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <iostream>
4#include <string>
5#include <variant>
6#include <vector>
7#include <misc/global.h>
8
9namespace sf
10{
11// This class is a simple and effective class to parse command line arguments.
12// For each possible argument it stores a pointer to a variable. When the
13// corresponding argument is set on the command line (given to the parse()
14// method) the variable is set to the given value. If the option is not set,
15// the variable is not touched. Hence, it should be initialized to a default
16// state.
17// For each argument, several names (aliases) can be defined. Thus, the same
18// boolean could be set via '--help' or '-h'. While not required, it is a good
19// practice to precede the argument names with either '--' or '-'. Except for
20// booleans, a value is expected to be given. Booleans are set to 'true' if no
21// value is provided (that means they can be used as simple flags as in the
22// '--help' case). Values can be given in two ways: Either the option name and
23// the value should be separated by a space or by a '='. Here are some valid
24// examples:
25// --string="Foo Bar"
26// --string "Foo Bar"
27// --help
28// --help=false
29// --help true
30
32{
33 public:
34 // These are the possible variables the options may point to. Bool and
35 // std::string are handled in a special way, all other values are parsed
36 // with a std::stringstream. This std::variant can be easily extended if
37 // the stream operator>> is overloaded. If not, you have to add a special
38 // case to the parse() method.
39 typedef std::variant<int32_t*, uint32_t*, int64_t*, uint64_t*, double*, float*, bool*, std::string*> Value;
40
41 // The description is printed as part of the help message.
42 explicit CommandLineParser(std::string description);
43
44 // Adds a possible option. A typical call would be like this:
45 // bool printHelp = false;
46 // cmd.addArgument({"--help", "-h"}, &printHelp, "Print this help message");
47 // Then, after parse() has been called, printHelp will be true if the user
48 // provided the flag.
49 void addArgument(std::vector<std::string> const& flags, Value const& value, std::string const& help);
50
51 // Prints the description given to the constructor and the help
52 // for each option.
53 void printHelp(std::ostream& os = std::cout) const;
54
55 // The command line arguments are traversed from start to end.
56 // That means, if an option is set multiple times, the last will be the one that is finally used.
57 // This call will throw a std::runtime_error if a value is missing for a given option.
58 // Unknown flags will cause a warning on std::cerr.
59 void parse(int argc, const char* argv[]) const;
60
61 private:
62 struct Argument
63 {
64 std::vector<std::string> _flags;
65 Value _value;
66 std::string _help;
67 };
68
69 std::string _description;
70 std::vector<Argument> _arguments;
71};
72
73}// namespace sf
void parse(int argc, const char *argv[]) const
void addArgument(std::vector< std::string > const &flags, Value const &value, std::string const &help)
CommandLineParser(std::string description)
void printHelp(std::ostream &os=std::cout) const
std::variant< int32_t *, uint32_t *, int64_t *, uint64_t *, double *, float *, bool *, std::string * > Value
Definition CommandLineParser.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10