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

API for shared arguments is a bit weird #383

Closed
sgrif opened this issue Jan 23, 2016 · 4 comments
Closed

API for shared arguments is a bit weird #383

sgrif opened this issue Jan 23, 2016 · 4 comments

Comments

@sgrif
Copy link

sgrif commented Jan 23, 2016

So diesel_cli has a use case where basically all of our commands take the same argument. You can see our exact code here: https://github.com/sgrif/diesel/blob/331f81c353ec0c7f0b6897cb80cb405b2d9120bb/diesel_cli/src/main.rs#L13-L42

All of our commands except one take --database-url (and really it's not a big deal if it gets passed to that command as well). Right now, if I put that argument on the Clap::App instance, it requires that the command is run as diesel --database-url=url migration run, but I'd much rather it be diesel migration run --database-url=url, or really just allow it basically anywhere. (As an aside, we're having to wrap the argument in a closure because Arg doesn't derive Clone, but it probably could)

Does this seem like a reasonable/common enough use case to warrant support within clap itself?

@kbknapp
Copy link
Member

kbknapp commented Jan 25, 2016

If you add that Arg to your top level App instance but set the Arg::global(true) it should be exactly what you're looking for...i.e. it'll just propagate that particular Arg down through all child subcommands....if not it's a bug ;)

@kbknapp
Copy link
Member

kbknapp commented Jan 25, 2016

@sgrif Also I haven't forgotten about #372 I'm really hoping to have it out really soon with the 2.x base 😉

@kbknapp
Copy link
Member

kbknapp commented Jan 25, 2016

@sgrif Also I forgot to mention that in v2 it supports passing borrowed Arg structs, so you could create an Arg instance and just pass a borrow of that same instance to all your subcommands which doesn't require Cloneing like global(true) does in 1.x (Arg::global also exists in v2 and removes the Cloneing as well, just FYI it's essentially shorthand for passing a borrow to down through each subcommand Edit: Arg::global still clones, I was mistaken on how'd I'd implemented that...but passing a borrow does not clone so long as global(true) is not set).

Edit: Corrections

@sgrif
Copy link
Author

sgrif commented Jan 26, 2016

Yup, I just missed this. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants