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