Skip to content

Commit

Permalink
[dnf5] Fix help in case argument parser detect error
Browse files Browse the repository at this point in the history
- if the user wants help (--help/-h argument is present) -> print help
  and only help without errors and the application ends with the return
  code libdnf::cli::ExitCode::SUCCESS.
- if user doesn't want help (--help/-h argument is not present) and
  argument parser detects missing/bad argument -> print an error message
  with a note about '--help' and application exits with return code
  libdnf::cli::ExitCode::ARGPARSER_ERROR
  • Loading branch information
jrohel committed Apr 12, 2023
1 parent 99f6bc4 commit e5e1865
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,29 +670,31 @@ int main(int argc, char * argv[]) try {

// Parse command line arguments
{
auto & arg_parser = context.get_argument_parser();
try {
context.get_argument_parser().parse(argc, argv);
} catch (libdnf::cli::ArgumentParserUnknownArgumentError & ex) {
// print help if an unknown command is provided
std::cerr << ex.what() << std::endl;
context.get_argument_parser().get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::ARGPARSER_ERROR);
} catch (const std::exception & ex) {
std::cout << ex.what() << std::endl;
arg_parser.parse(argc, argv);
} catch (libdnf::cli::ArgumentParserError & ex) {
// Error during parsing arguments. Try to find "--help"/"-h".
for (int idx = 1; idx < argc; ++idx) {
if (strcmp(argv[idx], "-h") == 0 || strcmp(argv[idx], "--help") == 0) {
arg_parser.get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::SUCCESS);
}
}
std::cerr << ex.what() << ". Add \"--help\" for more information about the arguments." << std::endl;
return static_cast<int>(libdnf::cli::ExitCode::ARGPARSER_ERROR);
}

auto & arg_parser = context.get_argument_parser();
// print help of the selected command if --help was used
if (arg_parser.get_named_arg("help", false).get_parse_count() > 0) {
arg_parser.get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::SUCCESS);
}
// print version of program if --version was used
if (arg_parser.get_named_arg("version", false).get_parse_count() > 0) {
dnf5::print_versions(context);
return static_cast<int>(libdnf::cli::ExitCode::SUCCESS);
}
// print help of the selected command if --help was used
if (arg_parser.get_named_arg("help", false).get_parse_count() > 0) {
context.get_argument_parser().get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::SUCCESS);
}
}

auto command = context.get_selected_command();
Expand Down Expand Up @@ -784,18 +786,6 @@ int main(int argc, char * argv[]) try {
}
std::cerr << ex.what() << std::endl;
return static_cast<int>(libdnf::cli::ExitCode::ERROR);
} catch (libdnf::cli::ArgumentParserMissingCommandError & ex) {
// print help if no command is provided
std::cerr << ex.what() << std::endl;
context.get_argument_parser().get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::ARGPARSER_ERROR);
} catch (libdnf::cli::ArgumentParserMissingDependentArgumentError & ex) {
std::cerr << ex.what() << std::endl;
context.get_argument_parser().get_selected_command()->help();
return static_cast<int>(libdnf::cli::ExitCode::ARGPARSER_ERROR);
} catch (libdnf::cli::ArgumentParserError & ex) {
std::cerr << ex.what() << std::endl;
return static_cast<int>(libdnf::cli::ExitCode::ARGPARSER_ERROR);
} catch (libdnf::cli::CommandExitError & ex) {
std::cerr << ex.what() << std::endl;
return ex.get_exit_code();
Expand Down

0 comments on commit e5e1865

Please sign in to comment.