-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Automatically detect allow_missing_positional #4837
Comments
The challenges this would run into
None of this is to say "no". This case is a bit more peculiar than something like |
This should have caused a debug assert to fire #!/usr/bin/env cargo-eval
//! ```cargo
//! [dependencies]
//! clap = { version = "4", features = ["derive"] }
//! ```
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(arg_required_else_help = true)]
pub struct Cli {
/// ...
pub name: Option<String>,
/// ...
pub dir: PathBuf,
}
fn main() {
let cli = Cli::parse();
dbg!(cli);
}
I could see us suggesting |
@epage I ran into the same issue when trying to implement |
atm a single vector of |
Please complete the following tasks
Clap Version
4.2.2
Describe your use case
I'd like to make a CLI that accepts either "arg1 arg2" or just "arg2" (the first argument is optional).
I wrote this:
This did cause clap to realize that
name
should be optional, based on the generated help:Notice the
[NAME]
vs<DIR>
.However, if I actually run the command with one argument:
I had to supply
#[command(allow_missing_positional = true)]
to make this work as expected.Describe the solution you'd like
I'd like clap to automatically assume that if it has a single positional argument specified with
Option
, it should assumeallow_missing_positional = true
.Alternatives, if applicable
If there's a strong reason not to autodetect this, then instead, I'd suggest producing an error on an
Option
positional argument withallow_missing_positional = false
.However, if at all possible I'd suggest making this work automatically.
Additional Context
No response
The text was updated successfully, but these errors were encountered: