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

Proper way to get path to the config file that was parsed #236

Closed
pwm1234 opened this issue Feb 22, 2019 · 8 comments
Closed

Proper way to get path to the config file that was parsed #236

pwm1234 opened this issue Feb 22, 2019 · 8 comments

Comments

@pwm1234
Copy link

pwm1234 commented Feb 22, 2019

What is the right way to get the path to the config file that was parsed? For debugging purposes, I want to log the path to the config file that was loaded. Since the default path could have been overridden during argument parsing, I need the Option returned by the app->get_config_ptr() call to tell me. At least this is how I have done it:

CLI::Option* config = app.get_config_ptr();
std::vector<std::string> results = config->results();
std::string config_file = config->get_defaultval();
if (results.size() > 0)
{
    config_file = results[0];
}

Is there a better way? If not, should there be a better way? If this is not obvious, should this be added to the documentation?

@phlptp
Copy link
Collaborator

phlptp commented Feb 22, 2019

After #235 is merged you should be able to do

config_file = app.get_config_ptr()->as<std::string>();

or

std::string config_file;
app.get_config_ptr()->results(config_file);

until then I think what you did might be the only way. though @henryiii might know something else.

@pwm1234
Copy link
Author

pwm1234 commented Feb 22, 2019

Wow--you did this and more before I even asked for it!!! Thank you.

By the way, what is the process (and delay) for updating CLI11 in vcpkg? I have kind of settled upon vcpkg for getting almost all of my 3rd party dependencies, so I would like to understand how and when CLI11 changes make it into the vcpkg head. (Should I make this question a separate issue?)

@pwm1234
Copy link
Author

pwm1234 commented Feb 22, 2019

I just discovered another way to get the config value using check with a lambda function. This seems better and more concise than my first attempt:

auto config = app.set_config("--config", config_val);
config->check([&](const std::string &config_arg) { 
    config_val = config_arg;
    return config_arg;
});

This is my first attempt using a custom validator. While your PR changes provide exactly what I need, this (ab)use of check seems to provide an alternative to poking into the results.

@phlptp
Copy link
Collaborator

phlptp commented Feb 22, 2019

I needed it for some options where I don't have any place to store the result until after the parsing, so this made it much easier to deal with that in a nice way, and make converting from boost::program_option somewhat more straightforward.

@phlptp
Copy link
Collaborator

phlptp commented Feb 22, 2019

I think in your check function you probably need to return an empty string otherwise it will throw an error of some kind since the return string usually goes into an error message about the validation failing.

@phlptp
Copy link
Collaborator

phlptp commented Feb 22, 2019

Or you could put that into the translate instead of the check

@henryiii
Copy link
Collaborator

Yes, @phlptp is correct. You'll want to return an empty string for "no error". However, what about running each? That sounds pretty close to what you want.

auto config = app.set_config("--config", config_val);
config->each([&config_val](const std::string &config_arg) { 
    config_val = config_arg;
});

@pwm1234
Copy link
Author

pwm1234 commented Feb 22, 2019

Yes, each sounds even better. It was, however, good for me to learn how to use a validator.
Thanks!

@pwm1234 pwm1234 closed this as completed Feb 22, 2019
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

3 participants