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

getter for registeredParams_? #42

Open
paleozogt opened this issue May 29, 2019 · 3 comments
Open

getter for registeredParams_? #42

paleozogt opened this issue May 29, 2019 · 3 comments

Comments

@paleozogt
Copy link

After calling add_param(s) I'd like to be able to read them back out. Can we add a registered params getter? For example:

std::set<std::string> const& registered_params() const { return registeredParams_; }

This would allow us to write our own "--help" flag handling:

argh::parser cmdl(argc, argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION);

cmdl.add_param("a");
cmdl.add_param("b");
cmdl.add_param("c");
cmdl.add_param("help");

if (cmdl.params().empty() || cmdl["help"]) {
    cout << "Help:\n";
    for (auto &param : cmdl.registered_params()) {
        cout << "--" << param << std::endl;
    }
    return 0;
}
@helmesjo
Copy link

IMO --help & -h should be built-in and handled automatically. A cli tool without a help print shouldn't be allowed. ;)

@adishavit
Copy link
Owner

@paleozogt: I somehow missed this issue when you posted.
That’s easy to add but I’m not sure I see the benefit. Since there is no support for help strings / usage, you’d have to write your own usage anyway, and only you know the supported options.
Argh kinda encourages you to be lazy and not preregister. Besides you’d still have to specify the flag arguments “manually”.

A future C++20 version will support automatic usage info, but that’s still a while away.

@ljluestc
Copy link

ljluestc commented Jan 2, 2024


#include <iostream>
#include <set>
#include <string>
#include <argh.h>

int main(int argc, char* argv[]) {
    argh::parser cmdl(argc, argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION);

    // Define a set to store registered parameters
    std::set<std::string> registeredParams;

    // Define and register parameters
    cmdl.add_param("a");
    cmdl.add_param("b");
    cmdl.add_param("c");
    cmdl.add_param("help");
    
    // Store registered parameters in the set
    for (const auto& param : cmdl.params()) {
        registeredParams.insert(param);
    }

    if (cmdl.params().empty() || cmdl["help"]) {
        std::cout << "Help:\n";
        for (const auto& param : registeredParams) {
            std::cout << "--" << param << std::endl;
        }
        return 0;
    }

    // Other command-line processing logic here...

    return 0;
}

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

No branches or pull requests

4 participants