feat(error): Add a 'render' method

This commit is contained in:
Ed Page 2022-09-22 09:43:09 -05:00
parent 4280fdfcbd
commit a2272a2c50
2 changed files with 22 additions and 0 deletions

View file

@ -272,6 +272,7 @@ Behavior Changes
- Can now pass runtime generated data to `Command`, `Arg`, `ArgGroup`, `PossibleValue`, etc without managing lifetimes with the `string` feature flag (#2150, #4223)
- New default `error-context`, `help` and `usage` feature flags that can be turned off for smaller binaries (#4236)
- *(error)* `Error::apply` for changing the formatter for dropping binary size (#4111)
- *(error)* `Command::render`
- *(help)* Show `PossibleValue::help` in long help (`--help`) (#3312)
- *(help)* New `{tab}` variable for `Command::help_template` (#4161)
- *(help)* `Command::render_help` and `Command::render_long_help`

View file

@ -195,6 +195,27 @@ impl<F: ErrorFormatter> Error<F> {
c.print()
}
/// Render the error message to a [`StyledStr`].
///
/// # Example
/// ```no_run
/// use clap::Command;
///
/// match Command::new("Command").try_get_matches() {
/// Ok(matches) => {
/// // do_something
/// },
/// Err(err) => {
/// let err = err.render();
/// println!("{}", err);
/// // do_something
/// },
/// };
/// ```
pub fn render(&self) -> StyledStr {
self.formatted().into_owned()
}
fn new(kind: ErrorKind) -> Self {
Self {
inner: Box::new(ErrorInner {