• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef UTILS_OPTION_PARSER_EXPORT_OPTION_PARSER_H
17 #define UTILS_OPTION_PARSER_EXPORT_OPTION_PARSER_H
18 
19 #include <list>
20 #include <string>
21 #include <vector>
22 
23 class OptionParser {
24 public:
25     int32_t Parse(int32_t argc, const char **argv);
26     std::string GetErrorString();
27 
28     template<typename T>
AddOption(const std::string & shortOpt,const std::string & longOpt,T & result)29     int32_t AddOption(const std::string &shortOpt, const std::string &longOpt, T &result)
30     {
31         return 0;
32     }
33 
34     template<>
35     int32_t AddOption<int32_t>(const std::string &shortOpt, const std::string &longOpt, int32_t &result)
36     {
37         return AddOption(shortOpt, longOpt, &result, Option::ValueType::i32);
38     }
39 
40     template<>
41     int32_t AddOption<uint32_t>(const std::string &shortOpt, const std::string &longOpt, uint32_t &result)
42     {
43         return AddOption(shortOpt, longOpt, &result, Option::ValueType::u32);
44     }
45 
46     template<>
47     int32_t AddOption<int64_t>(const std::string &shortOpt, const std::string &longOpt, int64_t &result)
48     {
49         return AddOption(shortOpt, longOpt, &result, Option::ValueType::i64);
50     }
51 
52     template<>
53     int32_t AddOption<double>(const std::string &shortOpt, const std::string &longOpt, double &result)
54     {
55         return AddOption(shortOpt, longOpt, &result, Option::ValueType::f64);
56     }
57 
58     template<>
59     int32_t AddOption<std::string>(const std::string &shortOpt, const std::string &longOpt, std::string &result)
60     {
61         return AddOption(shortOpt, longOpt, &result, Option::ValueType::str);
62     }
63 
64     template<>
65     int32_t AddOption<bool>(const std::string &shortOpt, const std::string &longOpt, bool &result)
66     {
67         return AddOption(shortOpt, longOpt, &result, Option::ValueType::bol);
68     }
69 
70     template<typename T>
AddArguments(T & result)71     int32_t AddArguments(T &result)
72     {
73         return 0;
74     }
75 
76     template<>
77     int32_t AddArguments<int32_t>(int32_t &result)
78     {
79         return AddArguments(&result, Argument::ValueType::i32);
80     }
81 
82     template<>
83     int32_t AddArguments<uint32_t>(uint32_t &result)
84     {
85         return AddArguments(&result, Argument::ValueType::u32);
86     }
87 
88     template<>
89     int32_t AddArguments<int64_t>(int64_t &result)
90     {
91         return AddArguments(&result, Argument::ValueType::i64);
92     }
93 
94     template<>
95     int32_t AddArguments<double>(double &result)
96     {
97         return AddArguments(&result, Argument::ValueType::f64);
98     }
99 
100     template<>
101     int32_t AddArguments<std::string>(std::string &result)
102     {
103         return AddArguments(&result, Argument::ValueType::str);
104     }
105 
106     int32_t GetSkippedArgc();
107     const char **GetSkippedArgv();
108 
109 private:
110     int32_t ParseArgument(const char *arg, const char *arg2);
111     int32_t ParseArgc(const char *arg, const char *arg2);
112     int32_t ParseShortOption(const char *arg1, const char *arg2);
113     int32_t ParseLongEqualOption(const char *arg, const char *arg2);
114     int32_t ParseLongOption(const char *arg1, const char *arg2);
115     int32_t AddSkipped(const char *arg, const char *arg2);
116 
117     struct Option {
118         const std::string so;
119         const std::string lo;
120         union Value {
121             int32_t i32;
122             uint32_t u32;
123             int64_t i64;
124             double f64;
125             std::string str;
126             bool bl;
127         } *result;
128         enum class ValueType {
129             i32,
130             u32,
131             i64,
132             f64,
133             str,
134             bol,
135         } type;
136     };
137 
138     int32_t AddOption(const std::string &shortOpt,
139                       const std::string &longOpt, void *result, Option::ValueType type);
140 
141     struct Argument {
142         union Value {
143             int32_t i32;
144             uint32_t u32;
145             int64_t i64;
146             double f64;
147             std::string str;
148         } *result;
149         enum class ValueType {
150             i32,
151             u32,
152             i64,
153             f64,
154             str,
155         } type;
156     };
157 
158     int32_t AddArguments(void *result, Argument::ValueType type);
159 
160     std::list<struct Argument> arguments;
161     std::vector<struct Option> options;
162     std::vector<const char *> skipped;
163     std::string error = "";
164 };
165 
166 #endif // UTILS_OPTION_PARSER_EXPORT_OPTION_PARSER_H
167