-
Notifications
You must be signed in to change notification settings - Fork 71
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
refactor: use derive
interface of clap
#110
Conversation
dd28e6f
to
a4d7c46
Compare
#[macro_use] | ||
extern crate slog; | ||
|
||
use clap::ArgAction; | ||
use clap::{CommandFactory, Parser as _}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is commandfactory necessary here? most of the flag-only examples don't seem to require it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so we can call Cli::command
to feed a Command
structure to clap_completion::generate
below. If one removes the import, they get the following compiler error (assuming a compilation based on a4d7c46):
error[E0599]: no function or associated item named `command` found for struct `Cli` in the current scope
--> src/main.rs:53:35
|
12 | struct Cli {
| ---------- function or associated item `command` not found for this struct
...
53 | let mut args_clone = Cli::command();
| ^^^^^^^ function or associated item not found in `Cli`
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
1 + use clap::CommandFactory;
|
For more information about this error, try `rustc --explain E0599`.
error: could not compile `git-absorb` (bin "git-absorb") due to 1 previous error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider this resolved, but I'll leave this open for visibility.
23ec0eb
to
a851db8
Compare
This comment was marked as resolved.
This comment was marked as resolved.
@ErichDonGubler i want to implement how would I implement this now? |
@kiprasmel: It's still possible, though it might look a bit odd! I didn't realize I had affected you here. 😅 If you're determined to use the #[derive(Debug, Eq, clap::Parser, PartialEq)]
struct Cli {
#[clap(long)]
foo: bool,
#[arg(long, action = clap::ArgAction::SetTrue, overrides_with = "foo")]
no_foo: (),
} In case it's interesting, I've often found that I prefer using a doubly-optional #[derive(Debug, Eq, clap::Parser, PartialEq)]
struct Cli {
#[clap(long)]
// One can remove the outer `Option` to force specification of the value on
// the CLI.
foo: Option<Option<Foo>>,
}
#[derive(Clone, Debug, Eq, PartialEq, clap::ValueEnum)]
enum Foo {
Yes,
No,
Whatever,
} |
@ErichDonGubler yes I thought about having the foo and no_foo as cli args, and resolve them into final value when passing in the config to run absorb. but again, i want a new --foo or --no-foo to overwrite the previous one, thus need to access the index. is there a way to simply access the argv strings and simply do it myself? |
for the record, having graceful precedence of |
If you examine the cases in both the tests and the playground link, I believe even the question of precedence is handled for you; AFAICT, the last flag specified "wins". Am I not understanding your intent? |
Hey there! 👋🏻 I figured this would be a nice way to make editing the CLI more approachable, since this only takes on 1 new dependency (
heck
0.5). LMK what you think!