Пример #1
0
variables_map parse_command_line(int ac, char** av, const options_description & desc)
{
    variables_map variables;

    option_description const * option = nullptr;

    for (int i = 1; i < ac; ++i) {
        char const * arg = av[i];
        if (option) {
            if (!option->value().parse(arg)) {
                throw invalid_option_value(*option, arg);
            }
            variables.ref_descriptions.push_back(*option);
            option = nullptr;
        }
        else if (arg[0] == '-') {
            if (arg[1] == '-') {
                option = &get_option_description(desc, arg + 2);
                if (!option->has_value()) {
                    variables.ref_descriptions.push_back(*option);
                    option = nullptr;
                }
            }
            else {
                ++arg;
                do {
                    option = &get_option_description(desc, arg[0]);
                    if (option->has_value()) {
                        if (arg[1]) {
                            if (!option->value().parse(arg+1)) {
                                throw invalid_option_value(*option, arg);
                            }
                            variables.ref_descriptions.push_back(*option);
                            option = nullptr;
                        }
                        break;
                    }

                    variables.ref_descriptions.push_back(*option);
                    ++arg;
                    option = nullptr;
                } while (*arg);
            }
        }
        else {
            throw invalid_command_line_syntax();
        }
    }

    if (option) {
        throw invalid_command_line_syntax(*option);
    }

    return variables;
}
Пример #2
0
void validate(any& v
              ,vector<std::string> const& values
              ,Coloring* target_type
              ,int
             )
{   // {{{
    // Make sure no previous assignment to 'a' was made.
    validators::check_first_occurrence(v);

    // Extract the first string from 'values'. If there is more than
    // one string, it's an error, and exception will be thrown.
    string s = validators::get_single_string(values);
    to_lower(s);

    static string const plain("plain"), ansi("ansi");
    if(equals(s,plain)) {
        v = any(PLAIN_COLORING);
    } else if(equals(s,ansi)) {
        v = any(ANSI_COLORING);
    } else {
        throw invalid_option_value(s);
    }
} // }}}