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

clap::Error to colored string #4133

Closed
2 tasks done
sigoden opened this issue Aug 27, 2022 · 6 comments
Closed
2 tasks done

clap::Error to colored string #4133

sigoden opened this issue Aug 27, 2022 · 6 comments
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-blocked Status: Blocked on something else such as an RFC or other implementation work.
Milestone

Comments

@sigoden
Copy link

sigoden commented Aug 27, 2022

Please complete the following tasks

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(sigoden/argc#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.

  1. clap::error::Error::formatted

  2. clap::builder::StyledStr::write_colored

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

@sigoden sigoden added the C-enhancement Category: Raise on the bar on expectations label Aug 27, 2022
@epage epage added A-help Area: documentation, including docs.rs, readme, examples, etc... S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Aug 29, 2022
@epage epage added this to the 4.x milestone Aug 29, 2022
@epage
Copy link
Member

epage commented Aug 29, 2022

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.

@sigoden
Copy link
Author

sigoden commented Aug 29, 2022

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.

@epage
Copy link
Member

epage commented Aug 29, 2022

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.

@sigoden
Copy link
Author

sigoden commented Aug 29, 2022

But I still need to print help(ErrorKind::DisplayHelp) to stderr. Is there a way to get the colored help string?

@epage
Copy link
Member

epage commented Aug 29, 2022

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.

@epage
Copy link
Member

epage commented Sep 28, 2022

Went ahead with this in #4248

@epage epage closed this as completed Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-blocked Status: Blocked on something else such as an RFC or other implementation work.
Projects
None yet
Development

No branches or pull requests

2 participants