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

Improve error handling for missing arguments #977

Merged
merged 2 commits into from
Oct 18, 2023

Conversation

evan-goode
Copy link
Member

  1. Print "It could be a command provided by a plugin..." hint to stderr, not stdout
  2. When dnf5 missingcommand --help is run, don't print the top-level help; instead print the "Unknown argument" error and exit with a non-zero code.

@jrohel let me know whether this looks good, if so I'll prepare some CI tests.

@evan-goode evan-goode requested a review from jrohel October 16, 2023 21:02
@evan-goode evan-goode self-assigned this Oct 16, 2023
@jrohel
Copy link
Contributor

jrohel commented Oct 17, 2023

$ dnf5 abc
Unknown argument "abc" for command "dnf5". Run "dnf5 --help" for a list of commands.
It could be a command provided by a plugin, try: dnf install dnf5-command(abc)

OK

$ dnf5/dnf5 --abc
Unknown argument "--abc" for command "dnf5". Add "--help" for more information about the arguments.

OK

$ dnf5/dnf5 --abc --help
Unknown argument "--abc" for command "dnf5". Add "--help" for more information about the arguments.

Fail. I added --help as advised. And I got again the same error message.

$ dnf5/dnf5 repo --abc
Unknown argument "--abc" for command "repo". Add "--help" for more information about the arguments.

OK

$ dnf5/dnf5 repo --abc --help
Usage:
  dnf5 [GLOBAL OPTIONS] repo [OPTIONS] <COMMAND> ...
                                
Query Commands:                 
  list                          List repositories
  info                          Print details about repositories
                                
Options:                        
  --forcearch=FORCEARCH         Force the use of a different architecture.

While dnf5/dnf5 --abc --help prints an error message without help, the subcommand prints help without an error message. What to unify it? For example, when there is an error and --help at the same time, print help and error message.

An example of code that prints what I have in mind:

            } catch (libdnf5::cli::ArgumentParserError & ex) {
                // Error during parsing arguments. Try to find "--help"/"-h".
                bool help_printed{false};
                for (int idx = 1; idx < argc; ++idx) {
                    if (strcmp(argv[idx], "-h") == 0 || strcmp(argv[idx], "--help") == 0) {
                        arg_parser.get_selected_command()->help();
                        help_printed = true;
                        break;
                    }
                }
                std::cerr << ex.what()
                          << (help_printed ? "." : _(". Add \"--help\" for more information about the arguments."))
                          << std::endl;
                // If the error is an unknown top-level command, suggest
                // installing a package that provides the command
                if (auto * unknown_arg_ex = dynamic_cast<libdnf5::cli::ArgumentParserUnknownArgumentError *>(&ex)) {
                    if (unknown_arg_ex->get_command() == "dnf5" && unknown_arg_ex->get_argument()[0] != '-') {
                        std::cerr
                            << fmt::format(
                                   "It could be a command provided by a plugin, try: dnf install dnf5-command({})",
                                   unknown_arg_ex->get_argument())
                            << std::endl;
                    }
                }
                return static_cast<int>(libdnf5::cli::ExitCode::ARGPARSER_ERROR);
            }

@jrohel jrohel assigned jrohel and unassigned evan-goode Oct 17, 2023
Copy link
Contributor

@jrohel jrohel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already described in previous the comment.

1. Print "It could be a command provided by a plugin..." hint to stderr,
   not stdout
2. When `dnf5 missingcommand --help` or `dnf5 --missingargument --help`
   is run, print the help followed by the "Unknown argument" error and
   exit with a non-zero code. The previous behavior was to only print
   the help and exit zero.
@evan-goode
Copy link
Member Author

Sure, I like that. I think the team was concerned that the error would be obscured by printing the help, but IMO it's alright if the error is printed last.

@evan-goode evan-goode marked this pull request as ready for review October 17, 2023 16:03
@evan-goode evan-goode requested a review from jrohel October 17, 2023 16:03
Copy link
Contributor

@jrohel jrohel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

@jrohel jrohel added this pull request to the merge queue Oct 18, 2023
Merged via the queue into rpm-software-management:main with commit c292df7 Oct 18, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants