Skip to content

clap exit code - #3457

Merged
sylvestre merged 5 commits into
uutils:mainfrom
tertsdiepraam:clap-exit-code
May 5, 2022
Merged

sylvestre merged 5 commits into
uutils:mainfrom
tertsdiepraam:clap-exit-code

Conversation

@tertsdiepraam

@tertsdiepraam tertsdiepraam commented Apr 30, 2022 •

Copy link
Copy Markdown
Collaborator

Step towards fixing:

To my surprise, clap::Error could already be used as a UResult due to excellent work by @shoriminimoe, which means that the exit code cal already be set to 1 by making this change in uumain:

let matches = uu_app().get_matches_from(args);
// to
let matches = uu_app().try_get_matches_from(args)?;

So, I added one additional bit of functionality to customize the error code:

use uucore::error::UClapError;
// -- snip --
let matches = uu_app().try_get_matches_from(args).with_exit_code(125)?;

With this change we should be able to go through all utils and fix up all the exit codes.

There is just one thing that I'm not sure about. The UResult implementation of clap::Error currently uses exit code 1, which is (intentionally) different from the standard clap exit code of 2. I'm thinking maybe the default should be 2 now that we can customize the exit code, so that the case where we differ from standard clap becomes explicit. @sylvestre what do you think?

Leaving this as a draft to wait for feedback and then I'll fix up the other utils.

@tertsdiepraam

Copy link
Copy Markdown
Collaborator Author

This PR now supersedes #3391

@sylvestre

Copy link
Copy Markdown
Contributor

Looks great, thanks :)

@tertsdiepraam

tertsdiepraam commented May 1, 2022 •

Copy link
Copy Markdown
Collaborator Author

Oh I just realized a drawback to this approach. It's very hard to create colored clap errors for this and it also always prefixes the error with the util name (sorry for using images, it is necessary to show the difference with the colors):

image
image

Maybe even more significantly, the help page is also rendered without colors.

@sylvestre

Copy link
Copy Markdown
Contributor

Oh, sad regression :/

@tertsdiepraam

Copy link
Copy Markdown
Collaborator Author

OK, I think I fixed it with a bit of an ugly workaround. Instead of doing what the Display trait expects us to do, we can instead print directly to stdout/err by calling the print method on clap's errors. In theory, this could lead to trouble if you don't want to write the error to stdout/err, but I can't think of any situations where that might be necessary.

@tertsdiepraam
tertsdiepraam force-pushed the clap-exit-code branch 2 times, most recently from 4218a30 to 9f143dd Compare May 4, 2022 17:10
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

Successfully merging this pull request may close these issues.

2 participants