Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept vector<string> for argv #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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
@@ -114,6 +114,7 @@ namespace argh

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