blob: e9a9088fc48a88c0d05a423691767187ec0b3dff [file] [log] [blame]
Patrick Williams3667cf32015-10-20 22:39:11 -05001#include <iostream>
2#include <iterator>
3#include <algorithm>
4#include <cassert>
5#include "argument.H"
6
7ArgumentParser::ArgumentParser(int argc, char** argv)
8{
9 int option = 0;
10 while(-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
11 {
12 if ((option == '?') || (option == 'h'))
13 {
14 usage(argv);
15 exit(-1);
16 }
17
18 auto i = &options[0];
19 while ((i->val != option) && (i->val != 0)) ++i;
20
21 if (i->val)
22 arguments[i->name] = (i->has_arg ? optarg : true_string);
23 }
24}
25
26const std::string& ArgumentParser::operator[](const std::string& opt)
27{
28 auto i = arguments.find(opt);
29 if (i == arguments.end())
30 {
31 return empty_string;
32 }
33 else
34 {
35 return i->second;
36 }
37}
38
39void ArgumentParser::usage(char** argv)
40{
41 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
42 std::cerr << "Options:" << std::endl;
Adriana Kobylakf261fa02016-06-12 15:45:42 -050043 std::cerr << " --help print this menu" << std::endl;
Patrick Williams3667cf32015-10-20 22:39:11 -050044 std::cerr << " --path=<path> sysfs location to monitor"
45 << std::endl;
46}
47
48const option ArgumentParser::options[] =
49{
50 { "path", required_argument, NULL, 'p' },
51 { "help", no_argument, NULL, 'h' },
52 { 0, 0, 0, 0},
53};
54
55const char* ArgumentParser::optionstr = "p:?h";
56
57const std::string ArgumentParser::true_string = "true";
58const std::string ArgumentParser::empty_string = "";