comparison common/options.cpp @ 23:03068f5ed79d

Misc: various style issues, #419
author David Demelier <markand@malikania.fr>
date Sun, 14 Feb 2016 16:15:48 +0100
parents 1158cffe5a5e
children
comparison
equal deleted inserted replaced
22:23d59afec277 23:03068f5ed79d
52 void parseLongOption(Result &result, Args &args, Iterator &it, Iterator &end, const Options &definition) 52 void parseLongOption(Result &result, Args &args, Iterator &it, Iterator &end, const Options &definition)
53 { 53 {
54 auto arg = *it++; 54 auto arg = *it++;
55 auto opt = definition.find(arg); 55 auto opt = definition.find(arg);
56 56
57 if (opt == definition.end()) { 57 if (opt == definition.end())
58 throw InvalidOption{arg}; 58 throw InvalidOption(arg);
59 }
60 59
61 /* Need argument? */ 60 /* Need argument? */
62 if (opt->second) { 61 if (opt->second) {
63 if (it == end || isOption(*it)) { 62 if (it == end || isOption(*it))
64 throw MissingValue{arg}; 63 throw MissingValue(arg);
65 }
66 64
67 result.insert(std::make_pair(arg, *it++)); 65 result.insert(std::make_pair(arg, *it++));
68 it = args.erase(args.begin(), it); 66 it = args.erase(args.begin(), it);
69 end = args.end(); 67 end = args.end();
70 } else { 68 } else {
84 * -c value 82 * -c value
85 */ 83 */
86 auto arg = *it++; 84 auto arg = *it++;
87 auto opt = definition.find(arg); 85 auto opt = definition.find(arg);
88 86
89 if (opt == definition.end()) { 87 if (opt == definition.end())
90 throw InvalidOption{arg}; 88 throw InvalidOption(arg);
91 }
92 89
93 /* Need argument? */ 90 /* Need argument? */
94 if (opt->second) { 91 if (opt->second) {
95 if (it == end || isOption(*it)) { 92 if (it == end || isOption(*it))
96 throw MissingValue{arg}; 93 throw MissingValue(arg);
97 }
98 94
99 result.insert(std::make_pair(arg, *it++)); 95 result.insert(std::make_pair(arg, *it++));
100 it = args.erase(args.begin(), it); 96 it = args.erase(args.begin(), it);
101 end = args.end(); 97 end = args.end();
102 } else { 98 } else {
118 114
119 for (decltype(len) i = 0; i < len; ++i) { 115 for (decltype(len) i = 0; i < len; ++i) {
120 auto arg = std::string{'-'} + value[i]; 116 auto arg = std::string{'-'} + value[i];
121 auto opt = definition.find(arg); 117 auto opt = definition.find(arg);
122 118
123 if (opt == definition.end()) { 119 if (opt == definition.end())
124 throw InvalidOption{arg}; 120 throw InvalidOption(arg);
125 }
126 121
127 if (opt->second) { 122 if (opt->second) {
128 if (i == (len - 1)) { 123 if (i == (len - 1)) {
129 /* End of string, get the next argument (see 2.) */ 124 /* End of string, get the next argument (see 2.) */
130 if (++it == end || isOption(*it)) { 125 if (++it == end || isOption(*it))
131 throw MissingValue{arg}; 126 throw MissingValue(arg);
132 }
133 127
134 result.insert(std::make_pair(arg, *it)); 128 result.insert(std::make_pair(arg, *it));
135 toremove += 1; 129 toremove += 1;
136 } else { 130 } else {
137 result.insert(std::make_pair(arg, value.substr(i + 1))); 131 result.insert(std::make_pair(arg, value.substr(i + 1)));
155 149
156 auto it = args.begin(); 150 auto it = args.begin();
157 auto end = args.end(); 151 auto end = args.end();
158 152
159 while (it != end) { 153 while (it != end) {
160 if (!isOption(*it)) { 154 if (!isOption(*it))
161 break; 155 break;
162 }
163 156
164 if (isLongOption(*it)) { 157 if (isLongOption(*it))
165 parseLongOption(result, args, it, end, definition); 158 parseLongOption(result, args, it, end, definition);
166 } else { 159 else
167 parseShortOption(result, args, it, end, definition); 160 parseShortOption(result, args, it, end, definition);
168 }
169 } 161 }
170 162
171 return result; 163 return result;
172 } 164 }
173 165
174 Result read(int &argc, char **&argv, const Options &definition) 166 Result read(int &argc, char **&argv, const Options &definition)
175 { 167 {
176 std::vector<std::string> args; 168 std::vector<std::string> args;
177 169
178 for (int i = 0; i < argc; ++i) { 170 for (int i = 0; i < argc; ++i)
179 args.push_back(argv[i]); 171 args.push_back(argv[i]);
180 }
181 172
182 auto before = args.size(); 173 auto before = args.size();
183 auto result = read(args, definition); 174 auto result = read(args, definition);
184 175
185 argc -= before - args.size(); 176 argc -= before - args.size();