Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
main.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "Hazelnupp.h"
3 #include "IntValue.h"
4 
5 int main(int argc, char** argv)
6 {
7  while (1)
8  {
9  // Mock command-line params
10  std::vector<const char*> testArgv = {
11  "meinpfad",
12  "-w",
13  "-99",
14  "--alfred",
15  "apfel",
16  "banane",
17  "triangle",
18  "--numbers",
19  "1",
20  "2",
21  "3",
22  "4",
23  "5",
24  };
25 
26  argc = testArgv.size();
27  argv = const_cast<char**>(testArgv.data());
28 
29  // Prepare Hazelnupp parser
30  Hazelnupp args;
31 
32  ParamConstraint pc;
33  pc.key = "--alfredo";
34  pc.constrainType = true;
36  pc.required = true;
37  pc.defaultValue = { "coca cola", "fanta" };
38 
39  args.RegisterConstraints({
40  pc
41  });
42 
43  args.RegisterAbbreviation("-w", "--word");
44 
45  // Parse
46  args.Parse(argc, argv);
47 
48  // Use
49  if (args.HasParam("--alfredo"))
50  {
51  std::cout << args["--alfredo"].GetInt32() << std::endl;
52  }
53  else
54  {
55  std::cout << "No --alfredo!" << std::endl;
56  }
57  }
58 
59  return 0;
60 }
Hazelnupp::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: Hazelnupp.cpp:330
ParamConstraint::required
bool required
If set to true, and no default value set, an error will be produced if this parameter is not supplied...
Definition: ParamConstraint.h:64
Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:352
DATA_TYPE::LIST
@ LIST
IntValue.h
main
int main(int argc, char **argv)
Definition: main.cpp:5
ParamConstraint::defaultValue
std::vector< std::string > defaultValue
The default value for this parameter.
Definition: ParamConstraint.h:60
ParamConstraint::key
std::string key
The key of the parameter to constrain.
Definition: ParamConstraint.h:48
Hazelnupp.h
Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:151
ParamConstraint::constrainType
bool constrainType
Should this parameter be forced to be of a certain type? Remember to set constrainTo to the wanted ...
Definition: ParamConstraint.h:52
ParamConstraint::wantedType
DATA_TYPE wantedType
Constrain the parameter to this value. Requires constrainType to be set to true.
Definition: ParamConstraint.h:55
Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:9
ParamConstraint
Definition: ParamConstraint.h:6
Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:33