@@ -57,22 +57,22 @@ namespace cargo {
5757
5858// / @brief Description of an argument used with `cargo::argument_parser`.
5959class argument {
60- public:
60+ public:
6161 // / @brief Parse result, used in `cargo::argument_parser::parse_args`.
6262 enum class parse : uint32_t {
63- NOT_FOUND, // /< Given argument was not found.
64- INVALID, // /< Given argument is invalid.
65- INCOMPLETE, // /< Given argument requires further parsing.
66- COMPLETE, // /< Given argument parsing completed.
63+ NOT_FOUND, // /< Given argument was not found.
64+ INVALID, // /< Given argument is invalid.
65+ INCOMPLETE, // /< Given argument requires further parsing.
66+ COMPLETE, // /< Given argument parsing completed.
6767 };
6868
6969 // / @brief Type to store the bitset of parsing options.
7070 using option_bitset = uint8_t ;
7171 // / @brief Enumeration of argument parsing options.
7272 enum option : uint8_t {
73- NONE = 0 , // /< No optional behavior enabled.
74- STORE_TRUE = 0x1 << 1 , // /< Store `bool` argument as `true`.
75- STORE_FALSE = 0x1 << 2 , // /< Store `bool` argument as `false`.
73+ NONE = 0 , // /< No optional behavior enabled.
74+ STORE_TRUE = 0x1 << 1 , // /< Store `bool` argument as `true`.
75+ STORE_FALSE = 0x1 << 2 , // /< Store `bool` argument as `false`.
7676 };
7777
7878 // / @brief Function type for custom handlers.
@@ -107,8 +107,7 @@ class argument {
107107 cargo::array_view<cargo::string_view> choices,
108108 cargo::string_view &storage,
109109 cargo::argument::option_bitset options = NONE)
110- : Name(name),
111- Variant(ChoiceT{std::addressof (storage), choices}),
110+ : Name(name), Variant(ChoiceT{std::addressof (storage), choices}),
112111 Options(options) {}
113112
114113 // / @brief Construct an append value argument.
@@ -132,9 +131,8 @@ class argument {
132131 argument (cargo::string_view name, custom_handler_function &&parse_argument,
133132 custom_handler_function &&parse_value,
134133 cargo::argument::option_bitset options = NONE)
135- : Name(name),
136- Variant(
137- CustomHandlerT{std::move (parse_argument), std::move (parse_value)}),
134+ : Name(name), Variant(CustomHandlerT{std::move (parse_argument),
135+ std::move (parse_value)}),
138136 Options(options) {}
139137
140138 // / @brief Parse a given argument, used by `cargo::argument_parser`.
@@ -146,9 +144,9 @@ class argument {
146144 // / @retval `INCOMPLETE` argument was found, requires a value.
147145 // / @retval `NOT_FOUND` argument was not found.
148146 // / @retval `INVALID` invalid argument, value not found.
149- [[nodiscard]] cargo::error_or<cargo::argument::parse> parse_arg (
150- cargo::string_view arg) {
151- if (Name == arg) { // "<name> <value>"
147+ [[nodiscard]] cargo::error_or<cargo::argument::parse>
148+ parse_arg ( cargo::string_view arg) {
149+ if (Name == arg) { // "<name> <value>"
152150 if (auto *Bool = isType<BOOL>()) {
153151 **Bool = (STORE_FALSE & Options) ? false : true ;
154152 return cargo::argument::parse::COMPLETE;
@@ -160,7 +158,7 @@ class argument {
160158 return CustomHandler->ParseArgument (arg);
161159 }
162160 }
163- if (arg.starts_with (Name)) { // "<name><value>"
161+ if (arg.starts_with (Name)) { // "<name><value>"
164162 auto value = arg.substr (Name.size (), cargo::string_view::npos);
165163 if (!value) {
166164 return cargo::argument::parse::NOT_FOUND;
@@ -180,8 +178,8 @@ class argument {
180178 // / @return Returns an error from `cargo::small_vector` or the parse result.
181179 // / @retval `COMPLETE` argument was found with no further action required.
182180 // / @retval `INVALID` invalid argument, value not found.
183- [[nodiscard]] cargo::error_or<cargo::argument::parse> parse_value (
184- cargo::string_view value) {
181+ [[nodiscard]] cargo::error_or<cargo::argument::parse>
182+ parse_value ( cargo::string_view value) {
185183 if (isType<BOOL>()) {
186184 return cargo::argument::parse::INVALID;
187185 }
@@ -210,7 +208,7 @@ class argument {
210208 return cargo::argument::parse::COMPLETE;
211209 }
212210
213- private:
211+ private:
214212 enum TypeT : uint8_t {
215213 BOOL,
216214 VALUE,
@@ -234,8 +232,7 @@ class argument {
234232 std::variant<BoolT, ValueT, ChoiceT, ValuesT, CustomHandlerT> Variant;
235233 option_bitset Options;
236234
237- template <TypeT Type>
238- auto isType () -> decltype(std::get_if<Type>(&Variant)) {
235+ template <TypeT Type> auto isType () -> decltype(std::get_if<Type>(&Variant)) {
239236 return std::get_if<Type>(&Variant);
240237 }
241238
@@ -287,9 +284,8 @@ enum argument_parser_option : uint8_t {
287284// / return error;
288285// / }
289286// / ```
290- template <size_t N, size_t NP = 1 , size_t NU = 1 >
291- class argument_parser {
292- public:
287+ template <size_t N, size_t NP = 1 , size_t NU = 1 > class argument_parser {
288+ public:
293289 // / @brief Construct the argument parser.
294290 // /
295291 // / @param options Bitset of cargo::argument_parser_option values.
@@ -313,8 +309,8 @@ class argument_parser {
313309 // / @retval `cargo::success` parsing was successful.
314310 // / @retval `cargo::bad_argument` an invalid argument was found.
315311 // / @retval `cargo::bad_alloc` allocation failure.
316- [[nodiscard]] cargo::result parse_args (
317- cargo::array_view<cargo::string_view> args) {
312+ [[nodiscard]] cargo::result
313+ parse_args ( cargo::array_view<cargo::string_view> args) {
318314 if (0 == args.size ()) {
319315 return cargo::success;
320316 }
@@ -336,30 +332,30 @@ class argument_parser {
336332 return result.error ();
337333 }
338334 switch (*result) {
335+ case cargo::argument::parse::COMPLETE:
336+ goto next_argument;
337+ case cargo::argument::parse::INCOMPLETE: {
338+ if (end == iter + 1 ) {
339+ return cargo::bad_argument;
340+ }
341+ iter++;
342+ auto result = Arg.parse_value (*iter);
343+ if (!result) {
344+ return result.error ();
345+ }
346+ switch (*result) {
339347 case cargo::argument::parse::COMPLETE:
340348 goto next_argument;
341- case cargo::argument::parse::INCOMPLETE: {
342- if (end == iter + 1 ) {
343- return cargo::bad_argument;
344- }
345- iter++;
346- auto result = Arg.parse_value (*iter);
347- if (!result) {
348- return result.error ();
349- }
350- switch (*result) {
351- case cargo::argument::parse::COMPLETE:
352- goto next_argument;
353- case cargo::argument::parse::INCOMPLETE:
354- case cargo::argument::parse::INVALID:
355- case cargo::argument::parse::NOT_FOUND:
356- return cargo::bad_argument;
357- }
358- } break ;
349+ case cargo::argument::parse::INCOMPLETE:
359350 case cargo::argument::parse::INVALID:
360- return cargo::bad_argument;
361351 case cargo::argument::parse::NOT_FOUND:
362- continue ;
352+ return cargo::bad_argument;
353+ }
354+ } break ;
355+ case cargo::argument::parse::INVALID:
356+ return cargo::bad_argument;
357+ case cargo::argument::parse::NOT_FOUND:
358+ continue ;
363359 }
364360 }
365361 if (AcceptPositionalArgs && arg == " --" ) {
@@ -440,14 +436,14 @@ class argument_parser {
440436 return UnrecognizedArgs;
441437 }
442438
443- private:
439+ private:
444440 cargo::small_vector<argument, N> Args;
445441 cargo::small_vector<cargo::string_view, NP> PositionalArgs;
446442 cargo::small_vector<cargo::string_view, NU> UnrecognizedArgs;
447443 argument_parser_option_bitset Options;
448444};
449445
450446// / @}
451- } // namespace cargo
447+ } // namespace cargo
452448
453- #endif // CARGO_ARGUMENT_PARSER_H_INCLUDED
449+ #endif // CARGO_ARGUMENT_PARSER_H_INCLUDED
0 commit comments