add option to use posixly correct argument ordering
parent
bff30e3eb2
commit
a84c1e5d84
|
@ -332,8 +332,10 @@ protected:
|
||||||
|
|
||||||
template<typename HelpFormatter>
|
template<typename HelpFormatter>
|
||||||
struct basic_arg_parser: arg_description_container {
|
struct basic_arg_parser: arg_description_container {
|
||||||
basic_arg_parser(string_range progname = string_range{}):
|
basic_arg_parser(
|
||||||
arg_description_container(), p_progname(progname)
|
string_range progname = string_range{}, bool posix = false
|
||||||
|
):
|
||||||
|
arg_description_container(), p_progname(progname), p_posix(posix)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void parse(int argc, char **argv) {
|
void parse(int argc, char **argv) {
|
||||||
|
@ -346,7 +348,6 @@ struct basic_arg_parser: arg_description_container {
|
||||||
template<typename InputRange>
|
template<typename InputRange>
|
||||||
void parse(InputRange args) {
|
void parse(InputRange args) {
|
||||||
bool allow_optional = true;
|
bool allow_optional = true;
|
||||||
|
|
||||||
while (!args.empty()) {
|
while (!args.empty()) {
|
||||||
string_range s{args.front()};
|
string_range s{args.front()};
|
||||||
if (s == "--") {
|
if (s == "--") {
|
||||||
|
@ -354,14 +355,13 @@ struct basic_arg_parser: arg_description_container {
|
||||||
allow_optional = false;
|
allow_optional = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!allow_optional) {
|
if (allow_optional && is_optarg(s)) {
|
||||||
parse_pos(s);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_optarg(s)) {
|
|
||||||
parse_opt(s, args);
|
parse_opt(s, args);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (p_posix) {
|
||||||
|
allow_optional = false;
|
||||||
|
}
|
||||||
parse_pos(s);
|
parse_pos(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -405,6 +405,14 @@ struct basic_arg_parser: arg_description_container {
|
||||||
return p_progname;
|
return p_progname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool posix_ordering() const {
|
||||||
|
return p_posix;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool posix_ordering(bool v) {
|
||||||
|
return std::exchange(p_posix, v);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool is_optarg(string_range arg) {
|
static bool is_optarg(string_range arg) {
|
||||||
return (!arg.empty() && (arg[0] == '-') && (arg != "-"));
|
return (!arg.empty() && (arg[0] == '-') && (arg != "-"));
|
||||||
|
@ -470,6 +478,7 @@ private:
|
||||||
|
|
||||||
std::string p_progname;
|
std::string p_progname;
|
||||||
HelpFormatter p_helpfmt{*this};
|
HelpFormatter p_helpfmt{*this};
|
||||||
|
bool p_posix = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct default_help_formatter {
|
struct default_help_formatter {
|
||||||
|
|
Loading…
Reference in New Issue