DebugServer2
Loading...
Searching...
No Matches
OptParse.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include <map>
14#include <string>
15#include <vector>
16
17namespace ds2 {
18
19class OptParse {
20public:
21 enum OptionType {
22 boolOption,
23 stringOption,
24 vectorOption,
25 };
26
27public:
28 void addOption(OptionType type, std::string const &name, char shortName,
29 std::string const &help = std::string(), bool hidden = false);
30 void addPositional(std::string const &name,
31 std::string const &help = std::string(),
32 bool hidden = false);
33
34public:
35 int parse(int argc, char **argv);
36
37public:
38 bool getBool(std::string const &name) const;
39 std::string const &getString(std::string const &name) const;
40 std::vector<std::string> const &getVector(std::string const &name) const;
41 std::string const &getPositional(std::string const &name) const;
42
43public:
44 void usageDie(char const *format = nullptr, ...);
45
46private:
47 struct OptionStorage {
48 char shortName;
49 OptionType type;
50 struct {
51 bool boolValue;
52 std::string stringValue;
53 std::vector<std::string> vectorValue;
54 } values;
55 std::string help;
56 bool hidden;
57 };
58
59 struct PositionalStorage {
60 std::string value;
61 std::string help;
62 bool hidden;
63 };
64
65 typedef std::map<std::string, OptionStorage> OptionCollection;
66 typedef std::map<std::string, PositionalStorage> PositionalCollection;
67
68private:
69 OptionCollection _options;
70 PositionalCollection _positionals;
71 std::string _runMode;
72
73private:
74 OptionCollection::iterator findShortOpt(char shortOption);
75 OptionStorage const &get(std::string const &name, OptionType type) const;
76};
77} // namespace ds2
Definition OptParse.h:19