Structopt: Access to `usage` for custom error messages

Created on 3 May 2018  路  12Comments  路  Source: TeXitoi/structopt

In some cases, I need to do validation after-the-fact.. With clap, I have access to ArgMatches which can let be print the usage. I'm not aware of a way to access that from structopt.

doc

Most helpful comment

@thiagoarrais I think we should explicitly state that StructOpt trait has more than just from_args function. Perhaps somewhere in How to derive section.

All 12 comments

I'm not sure I understand, but StructOpt::clap and StructOpt::from_clap doesn't allow you to do what you need?

So it looks like this will require

let app = Opts::clap();
let matches = app.get_matches();
let opts = Opts::from_clap(&matches);
....
if error_case {
    bail!("Describe error\n{}", matches.usage());
}

I assume this will be the best we should get. Anything more would require somehow keeping the matches around which would probably be too much for the few cases where this is needed.

Granted, my main use case for manual validation is the problems I've run into with raw. Most or all I could do with clap if it was easier to do and didn't become too verbose (#104 would help).

Thus this issuse is fixed for you? You can close it if yes, or refine the question/evolution else.

It might be nice to more explicitly document the workflow for accessing the matches (not the exact use case). It is sometimes hard with macros to know whats going on. It is nice that you provide a lot of functionality through a trait, improving discoverability but it would still be nice to note the relationship of the provided parts and then fit them together to solve a need.

Feel free to re-open if you want to do that.

TODO: better document that #[derive(StructOpt)] gives you acces to the structopt::StructOpt trait, with other things that just from_args.

related to this (maybe), in clap, I can call app.print_usage(). How does one do this in structopt?

Opt::clap().print_usage();

great thanks! (absolutely fantastic crate by the way. really makes creating a cli a no brainer)

Glad to help here. How do you think this would be better documented, @TeXitoi?

  1. Add "How to get the underlying clap matches" to main docs.rs page (with examples)?
  2. Add such a section to the repo's README?
  3. Include a quick observation on the how to derive section in docs.rs?

@thiagoarrais I think we should explicitly state that StructOpt trait has more than just from_args function. Perhaps somewhere in How to derive section.

I validate @CreepySkeleton's suggestion.

Nice. I've sent a PR. Let's continue this convo there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tathanhdinh picture tathanhdinh  路  6Comments

polarathene picture polarathene  路  6Comments

lucab picture lucab  路  9Comments

rask picture rask  路  8Comments

swfsql picture swfsql  路  3Comments