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

Question: how to extract {flags, opts, subcommands, ...} #1285

Closed
yoshuawuyts opened this issue Jun 3, 2018 · 4 comments · Fixed by #1736
Closed

Question: how to extract {flags, opts, subcommands, ...} #1285

yoshuawuyts opened this issue Jun 3, 2018 · 4 comments · Fixed by #1736
Labels
A-completion Area: completion generator E-hard Call for participation: Experience needed to fix: Hard / a lot
Milestone

Comments

@yoshuawuyts
Copy link

Heya 👋,

I was wondering what the recommended approach is to extract the currently used flags, subcommands, opts (and similar).

My goal is to generate man pages from a Clap struct, with the short versions and descriptions included. I've tried app.write_long_help(), but having to parse strings using regexes seems a bit wasteful.

I've also been digging into Clap's (undocumented) internals but given it's all undocumented, I take it that it might not have any stability guarantees.

Current Best Approach I've Found

#[macro_use]
extern crate structopt;

use structopt::StructOpt;

mod cli;

fn main() {
  let mut app = cli::Opts::clap();
  let mut stdout = std::io::stdout();
  app.write_long_help(&mut stdout).unwrap();   // We need to run this first.
  for f in app.p.flags() {
    println!("flag {:?}", &f);                 // Otherwise this doesn't work.
  }
}

So umm, to make these questions a bit more concrete:

Concrete Questions

  • What's going on in app.write_long_help that provisions app.p.flags() with its contents?
  • Is there a better way to extract flags than what we're currently doing?
  • If there currently isn't a better way, could we perhaps add an interface to access this information?

I hope these questions make sense. Thanks so much for your time! ✨

@kbknapp
Copy link
Member

kbknapp commented Jun 4, 2018

This is actually a goal of 3.x! So if you'd like to work on this problem set on the v3-master branch it would be HIGHLY appreciated!

The v3-master branch is quite different from the current master branch (i.e. v2) in that you iterate the subcommands and args directly and not through a Parser facade.

In the v3-master branch you'd use macros (flags!, opts!, subcommands!, positionals! etc.) which have already been defined.

@kbknapp kbknapp added T: new feature E-hard Call for participation: Experience needed to fix: Hard / a lot A-completion Area: completion generator labels Jun 4, 2018
@yoshuawuyts
Copy link
Author

Oh, this is amazing! - I'll dig further into the v3 branch then. Thanks for taking your time to reply! ✨

@pksunkara
Copy link
Member

Should be fixed by #1736. cc @CreepySkeleton

@pksunkara pksunkara added this to the 3.0 milestone Apr 9, 2020
@CreepySkeleton
Copy link
Contributor

Yeah, right. Will get to it 11, should be done in a couple of days from then.

@pksunkara pksunkara linked a pull request Apr 10, 2020 that will close this issue
@bors bors bot closed this as completed in #1736 Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator E-hard Call for participation: Experience needed to fix: Hard / a lot
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants