File: arg_parser.h

package info (click to toggle)
spirv-reflect 1.4.328.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,260 kB
  • sloc: ansic: 13,975; cpp: 5,963; python: 140; sh: 76; makefile: 12
file content (65 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef __VERIFLECT_ARG_PARSER_H__
#define __VERIFLECT_ARG_PARSER_H__

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

class ArgParser {
 public:
  enum OptionType { OPTION_TYPE_UNDEFINED = 0, OPTION_TYPE_FLAG, OPTION_TYPE_STRING, OPTION_TYPE_INT, OPTION_TYPE_FLOAT };

  struct OptionValue {
    std::string str;
    union {
      int i32;
      float f32;
    };
  };

  struct Option {
    std::string short_name;
    std::string long_name;
    OptionType type;
    std::string desc;
    OptionValue value;
    OptionValue default_value;
    bool parsed;
  };

  ArgParser();
  ~ArgParser();

  bool AddFlag(const std::string& short_name, const std::string& long_name, const std::string& desc);
  bool AddOptionString(const std::string& short_name, const std::string& long_name, const std::string& desc,
                       const std::string& default_value = "");
  bool AddOptionInt(const std::string& short_name, const std::string& long_name, const std::string& desc, int default_value = 0);
  bool AddOptionFloat(const std::string& short_name, const std::string& long_name, const std::string& desc,
                      float default_value = 0);

  bool Parse(int argc, char** argv, std::ostream& os);

  size_t GetArgCount() const;
  bool GetArg(size_t i, std::string* p_value) const;
  const std::vector<std::string>& GetArgs() const;

  bool GetFlag(const std::string& short_name, const std::string& long_name) const;
  bool GetString(const std::string& short_name, const std::string& long_name, std::string* p_value) const;
  bool GetInt(const std::string& short_name, const std::string& long_name, int* p_value) const;
  bool GetFloat(const std::string& short_name, const std::string& long_name, float* p_value) const;

  void PrintHelp(std::ostream& os);

 private:
  ArgParser::Option* FindOptionByShortName(const std::string& short_name);
  const ArgParser::Option* FindOptionByShortName(const std::string& short_name) const;
  ArgParser::Option* FindOptionByLongName(const std::string& long_name);
  const ArgParser::Option* FindOptionByLongName(const std::string& long_name) const;

 private:
  std::vector<ArgParser::Option> m_options;
  std::vector<std::string> m_args;
};

#endif  // __VERIFLECT_ARG_PARSER_H__