clap
clap copied to clipboard
clap::Error to colored string
Please complete the following tasks
- [X] I have searched the discussions
- [X] I have searched the open and rejected issues
Clap Version
master
Describe your use case
I make a bash cli tool(https://github.com/sigoden/argc), it use clap as cli parser.
A pr(https://github.com/sigoden/argc/issues/37) have been submitted for request colored help and colored error message.
So I need a way to covert clap::Error to colored string.
Describe the solution you'd like
The easiest way is to expose following two apis.
A better way is to add a to_color_string
function to clap::error::Error
impl Error {
#[cfg(feature = "color")]
pub fn to_colored_string(&self) -> String {
}
}
Alternatives, if applicable
No response
Additional Context
No response
Is there a reason you can't just use Error::print
or Error::exit
?
In clap 4.x, we are planning on changing how we handle terminal styling which will lead to use exposing something like what is proposed in here.
Argc use bash eval $(argc --argc-eval "$0" "$@")
to retrive passed variables from stdout, so the help text or error message must print to stderr.
Error::print
or Error::exit
will print to stdout in some situation(ErrorKind::DisplayHelp | ErrorKind::DisplayVersion
). So I can't use them.
Error::print or Error::exit will print to stdout in some situation(ErrorKind::DisplayHelp | ErrorKind::DisplayVersion). So I can't use them.
In the interim, you can check the error kind and conditionally do it based on that.
But I still need to print help(ErrorKind::DisplayHelp
) to stderr. Is there a way to get the colored help string?
No, and there won't be until some time during clap 4.x.
I was providing a way to workaround the problem until we do provide an API for this. This is my top clap priority once 4.0 is released.
Went ahead with this in #4248