comparison modules/options/options.cpp @ 500:d8ed4da7688c

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