Skip to content

Conversation

@sfinktah
Copy link

Because sometimes you don't have access to the command line arguments in their raw form...

std::vector<std::string> args;
int main(int argc, char** argv) {
   for (size_t i=0; i < argc; ++i)
      args.emplace_back(argv[0]);
   run_application();
}

void application() {
   auto cmdl = argh::parser(args);
   // ...
}

argh.h Outdated

void parse(const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION);
void parse(int argc, const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION);
void parser::parse(const std::vector<std::string>& argv, int mode);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why void parser::parse(....?

Copy link
Author

@sfinktah sfinktah Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, copy and paste from implementation... mode should also have a default. lemme fix.

btw also allows argh to parse any old array of strings, can be quite interesting. i'll fix that pr.

i also added this to my personal build, but as i haven't tested it yet, i won't push it.

it's basically for a use-case where you might have two same-meaning multiple-argument parameters, eg --exclude-file --exclude-files --exclude-path --exclude-path

i also thought it might be able to be written better with an iterator, but it was beyond me.

    inline std::vector<std::pair<std::string, std::string>> parser::params(std::initializer_list<char const* const> init_list) const {
        std::vector<std::string> names;
        for (auto name : init_list)
            names.emplace_back(trim_leading_dashes(name));

        std::vector<std::pair<std::string, std::string>> results;
        for (const auto& param : params_)
            if (std::any_of(names.begin(), names.end(), [&](const std::string& name) { return name == param.first; }))
                results.emplace_back(param.first, param.second);
        return results;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants