-
Notifications
You must be signed in to change notification settings - Fork 380
Deprecated retired options #358
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
Changes from all commits
fa086ec
1568e94
5a37076
0037ca5
39021de
884540e
9b33653
a649b8d
a80d909
937dfe2
c326f73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "CLI/CLI.hpp" | ||
#include <vector> | ||
|
||
// This example shows the usage of the retired and deprecated option helper methods | ||
int main(int argc, char **argv) { | ||
|
||
CLI::App app("example for retired/deprecated options"); | ||
std::vector<int> x; | ||
auto opt1 = app.add_option("--retired_option2", x); | ||
|
||
std::pair<int, int> y; | ||
auto opt2 = app.add_option("--deprecate", y); | ||
|
||
app.add_option("--not_deprecated", x); | ||
|
||
// specify that a non-existing option is retired | ||
CLI::retire_option(app, "--retired_option"); | ||
|
||
// specify that an existing option is retired and non-functional: this will replace the option with another that | ||
// behaves the same but does nothing | ||
CLI::retire_option(app, opt1); | ||
|
||
// deprecate an existing option and specify the recommended replacement | ||
CLI::deprecate_option(opt2, "--not_deprecated"); | ||
|
||
CLI11_PARSE(app, argc, argv); | ||
|
||
if(!x.empty()) { | ||
std::cout << "Retired option example: got --not_deprecated values:"; | ||
for(auto &xval : x) { | ||
std::cout << xval << " "; | ||
} | ||
std::cout << '\n'; | ||
} else if(app.count_all() == 1) { | ||
std::cout << "Retired option example: no arguments received\n"; | ||
} | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3029,6 +3029,85 @@ inline void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable) { | |
}); | ||
} | ||
|
||
/// Helper function to mark an option as deprecated | ||
inline void deprecate_option(Option *opt, const std::string &replacement = "") { | ||
Validator deprecate_warning{[opt, replacement](std::string &) { | ||
std::cout << opt->get_name() << " is deprecated please use '" << replacement | ||
<< "' instead\n"; | ||
return std::string(); | ||
}, | ||
"DEPRECATED"}; | ||
deprecate_warning.application_index(0); | ||
opt->check(deprecate_warning); | ||
if(!replacement.empty()) { | ||
opt->description(opt->get_description() + " DEPRECATED: please use '" + replacement + "' instead"); | ||
} | ||
} | ||
|
||
/// Helper function to mark an option as deprecated | ||
inline void deprecate_option(App *app, const std::string &option_name, const std::string &replacement = "") { | ||
auto opt = app->get_option(option_name); | ||
deprecate_option(opt, replacement); | ||
} | ||
|
||
/// Helper function to mark an option as deprecated | ||
inline void deprecate_option(App &app, const std::string &option_name, const std::string &replacement = "") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My IDE complains that this function is never used. Is my IDE wrong? Manually, I also can't find usages and wonder how you get 100% coverage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I was using it one of the tests but I will double check that and make sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
auto opt = app.get_option(option_name); | ||
deprecate_option(opt, replacement); | ||
} | ||
|
||
/// Helper function to mark an option as retired | ||
inline void retire_option(App *app, Option *opt) { | ||
App temp; | ||
auto option_copy = temp.add_option(opt->get_name(false, true)) | ||
->type_size(opt->get_type_size_min(), opt->get_type_size_max()) | ||
->expected(opt->get_expected_min(), opt->get_expected_max()) | ||
->allow_extra_args(opt->get_allow_extra_args()); | ||
|
||
app->remove_option(opt); | ||
auto opt2 = app->add_option(option_copy->get_name(false, true), "option has been retired and has no effect") | ||
->type_name("RETIRED") | ||
->default_str("RETIRED") | ||
->type_size(option_copy->get_type_size_min(), option_copy->get_type_size_max()) | ||
->expected(option_copy->get_expected_min(), option_copy->get_expected_max()) | ||
->allow_extra_args(option_copy->get_allow_extra_args()); | ||
|
||
Validator retired_warning{[opt2](std::string &) { | ||
std::cout << "WARNING " << opt2->get_name() << " is retired and has no effect\n"; | ||
return std::string(); | ||
}, | ||
""}; | ||
retired_warning.application_index(0); | ||
opt2->check(retired_warning); | ||
} | ||
|
||
/// Helper function to mark an option as retired | ||
inline void retire_option(App &app, Option *opt) { retire_option(&app, opt); } | ||
|
||
/// Helper function to mark an option as retired | ||
inline void retire_option(App *app, const std::string &option_name) { | ||
|
||
auto opt = app->get_option_no_throw(option_name); | ||
if(opt != nullptr) { | ||
retire_option(app, opt); | ||
return; | ||
} | ||
auto opt2 = app->add_option(option_name, "option has been retired and has no effect") | ||
->type_name("RETIRED") | ||
->expected(0, 1) | ||
->default_str("RETIRED"); | ||
Validator retired_warning{[opt2](std::string &) { | ||
std::cout << "WARNING " << opt2->get_name() << " is retired and has no effect\n"; | ||
return std::string(); | ||
}, | ||
""}; | ||
retired_warning.application_index(0); | ||
opt2->check(retired_warning); | ||
} | ||
|
||
/// Helper function to mark an option as retired | ||
inline void retire_option(App &app, const std::string &option_name) { retire_option(&app, option_name); } | ||
|
||
namespace FailureMessage { | ||
|
||
/// Printout a clean, simple message on error (the default in CLI11 1.5+) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many other examples do a std::cout at the end, to show what is going on. Does that make sense here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something that prints out if the '--not_deprecated` or no options were passed, just so there is always guaranteed to be some output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, I like it!