I'm using Clap to parse command-line arguments. Currently, I have something like this:
struct Cli {
/// Read (slurp) all input values into one array
#[arg(short, long)]
slurp: bool,
/// List of input files
remaining: Vec<String>,
}
(The actual type is much larger, but this serves for an illustration.)
Now, I would like to make an additional flag --args
: When this flag is given, every argument after it should end up in its corresponding vector.
Example: ./main --slurp file1 file2 --args foo bar
should create something like Cli { slurp: true, remaining: ["file1", "file2"], args: ["foo", "bar"]}
.
I was expecting this to look somewhat like the following, using trailing_var_arg
:
// Cargo.toml contains the following dependency:
// clap = { version = "4.0.0", features = ["derive"] }
use clap::{Parser, ValueEnum};
struct Cli {
/// Read (slurp) all input values into one array
#[arg(short, long)]
slurp: bool,
/// Arguments
#[arg(long, trailing_var_arg = true)] // <----------
args: Vec<String>,
/// List of input files
remaining: Vec<String>,
}
fn main() {
let cli = Cli::parse();
}
Alas, this panics, stating that "Arg::trailing_var_arg
can only apply to last positional".
I tried some other things, but all my attempts have failed.
Do you have any ideas?