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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions argh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
    }


std::multiset<std::string> const& flags() const { return flags_; }
std::multimap<std::string, std::string> const& params() const { return params_; }
Expand Down Expand Up @@ -193,15 +194,26 @@ namespace argh
//////////////////////////////////////////////////////////////////////////

inline void parser::parse(int argc, const char* const argv[], int mode /*= PREFER_FLAG_FOR_UNREG_OPTION*/)
{
std::vector<std::string> args;

// convert to strings
args.resize(static_cast<decltype(args_)::size_type>(argc));
std::transform(argv, argv + argc, args.begin(), [](const char* const arg) { return arg; });

parse(args, mode);
}

//////////////////////////////////////////////////////////////////////////

inline void parser::parse(const std::vector<std::string>& argv, int mode)
{
// clear out possible previous parsing remnants
flags_.clear();
params_.clear();
pos_args_.clear();

// convert to strings
args_.resize(static_cast<decltype(args_)::size_type>(argc));
std::transform(argv, argv + argc, args_.begin(), [](const char* const arg) { return arg; });
args_ = argv;

// parse line
for (auto i = 0u; i < args_.size(); ++i)
Expand Down