Please change member properties from private to protected
This will make Inherit the clase and set command name by method after init.
Why not simply add a public member function to set the command name for a parser? Does this not meet your needs?
It also make sense. I am trying build a argument parser as a command in runtime, so inherit and change the inner maps is easy solution.
I digged it and found change from private to protected won't solve all the requirements. ArgumentParser as a friend class will not inheried by its child class. Asume a requirement like: print all user given argument from "xxx -n 1.0" which "-n"="-number" is by add_args("-n","number"). I want to get a string just like "-n 1.0" not "-number 1.0" and no other argument which is not user provided. I can add the child class as a friend class to fill it,but do you have a better solution?
@lingerer Could you post a short pseudocode of what you are doing and how you want it to work? I'm not clear about what you want from argparse.
The main purpose of this issue is to let me has some way to access the private inner maps of ArgumentParse and Argument without modified the source code. We are passing ArgumentParse as a command instead passing the orginal string argument.Without the original string argument,it's hard to modify/delete argument.Here's some example: 1.reproduce the original arguments or argument string make ArgumentParser private member to protected , I can access m_argument_map or m_optional_arguments to reproduce it like:
class FCommand :public argparse::ArgumentParser{
FCommand(const string& cmd);
string getCommand();
}
FCommand::FCommand(const string& cmd) :ArgumentParser(cmd, "1.0", default_arguments::none){}
string FCommand::getCommand(){
string result;
for(const auto& arg:m_optional_arguments){
result+=result.empty()?"":" "+arg.m_used_name; //<--m_used_name is not accessable cause its friend class is ArgumentParser
result+=" "+arg.get<string>(arg.m_used_name);
}
}
FCommand program("program_name");
program.add_argument("--stop","-stop","-s")
.implicit_value(true)
.default_value(false);
std::cout<<program.getCommand()<<std::endl;
2.change the argument value Just like above code, No easy way to remove one of the m_argument_map and m_optional_arguments then re-add it or just clear the value and re-parse it.
I hope this code will help you to understand the scenario.
I don't believe argparse will work for your purposes at this time.
For case 1, argparse assumes you know the acceptable arguments and can ask for their values. There is presently no way to ask for all processed arguments. I can see the benefit, but this feature is not yet available.
In case 2, argparse is not as dynamic as you may want. Parsed values are read-only. (argparse can re-parse, but it will only add new values, not replace old ones.)
I don't know if changing either of these is planned for the future.
I quite understand the design purpose, that's why I'am asking to make private to protected so I can do it by myself.
In case 2 ,I believe offer a clear() method to reset the value then we can reuse the parser .
I quite understand the design purpose, that's why I'am asking to make private to protected so I can do it by myself.
I'll leave that to @p-ranav as I don't know the original thoughts for deciding ArgumentParser should be private.
In case 2 ,I believe offer a clear() method to reset the value then we can reuse the parser .
Rather than trying to re-architect argparse, could you further process the values it gives you?
parser.add_argument("-n", "-number").scan<'i', int>();
auto arg_number = parser.get("-number");
while ( ! value_between(arg_number, 1, 100) ) {
arg_number = interactive_get_value();
}
But, if you want to offer a PR that adds Argument::clear and ArgumentParser::clear, I would be happy to review it and give feedback.
The clear() method is mean to be reused under heavy request to reduce the construction cost.Although I can just keep a original parser and copy it before use every time by now.