-
Notifications
You must be signed in to change notification settings - Fork 0
Ignore and save in vector unknow args #111
Comments
Comment by forkbomb9 I found this: clap-rs/clap#1361 It's exactly what I was looking for... maybe we should focus on that issue. |
Comment by zkat I could still quite use this: I want to be able to pass-through the args to a specific subcommand to a child process, ignoring all the direct args to the subcommand itself. |
Comment by cecton It would be useful for me too |
Comment by cecton I just found out how to do it! I used: I did it with structopt but you can translate to the clap equivalent. |
Comment by Urhengulas Hi @CreepySkeleton, @pksunkara, I'd be interested in implementing this, if there is some guidance in how to approach this best. My idea would be to do it very similar to What do you think? |
Comment by Urhengulas
Hi @pksunkara, I've tried that, but this only ignores arguments "at the end", not "in the middle". Let me outline my situation: What I wantLet's assume I have following app: use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
struct Opt {
#[structopt(short = "-L", long, parse(from_os_str))]
library_path: Vec<PathBuf>,
#[structopt(short, long, parse(from_os_str))]
output: PathBuf,
#[structopt(short = "T", long)]
script: Vec<String>,
}
fn main() {
let opt: Opt = Opt::from_args();
dbg!(opt);
} Therefore my cli can be invoked like this: $ cli -T memory.x -o odir/ -L ldir/ -T memory2.x
[src/main.rs:29] opt = Opt {
library_path: [
"ldir/",
],
output: "odir/",
script: [
"memory2.x",
"memory.x",
],
}
But now I also want to allow the user to add arbitrary arguments at all positions, like this (which is the short form of this): $ cli \
--eh-frame-hdr \
-flavor gnu \
-L ldir1/ \
file.o \
-o odir/ \
--gc-sections \
-L ldir2/ \
-L ldir3/ \
-Tmemory.x \
-Bdynamic In my software I only care about Therfore I'd like aboves invocation to result in sth like this: [src/main.rs:29] opt = Opt {
library_path: [
"ldir1/",
"ldir2/",
"ldir3/",
],
output: "dir/",
script: [
"memory.x",
],
# `_rest` is nice to have, but actually not needed in my case
_rest: [
"--eh-frame-hdr",
"-flavor",
"gnu",
"file.o",
"--gc-sections",
"-Bdynamic",
]
} Why
|
Comment by pksunkara You are welcome to try implementing it but I think this would need a complete parsing logic refactor since we need to design the new logic from ground up. |
Comment by Urhengulas
@pksunkara I am not familiar with the clap parsing logic, but that is what I feared.. Do you know some workaround or other crate which could help in my case by any chance? |
Comment by pksunkara Implementing #1880 might help you with this. You can try researching that direction. |
Comment by cecton
You can ssk the user to provide the parameters for the inner process separately:
That's what |
Comment by Urhengulas
@cecton Thank you for the tip. The problem is that I want to provide compatibility with another cli, But I only want to capture some of the arguments to do some preprocessing and then hand over all the arguments to The approach I am trying to go for now, is to preprocess the args to only include the ones i am interested in and then parse this with |
Comment by Urhengulas
This feature actually looks like a possible solution for me! |
Issue by forkbomb9
Wednesday Jan 23, 2019 at 23:53 GMT
Originally opened as clap-rs/clap#1404
Affected Version of clap
Bug or Feature Request Summary
It would be great to have a way to save unknow args into a Vec<> or slice. For example, an Arg::with_name() option, e.g.
Arg::with_name('unknow').unknow_args()
or something like this.Maybe it could be that instead of calling
get_matches()
on the arg list, add aget_know_matches()
that returns a tuple, the first element being what would beget_matches()
and the second a vector of the unknow args... Something like Python'sargparse
:And then, if you call
myprog -d --something
, you have inmatches
the normal clap behaviour, and inunknow_matches
a vector containg'--something'
I don't know if I'm explaining it well, as English is not my primary language.
EDIT: Save unknow in vector or slice instead of ignoring them
The reason for this is that i'm building a program that has "subargs" (e.g.
myprogram -Xh
is the message help formyprogram -X
, but not the same help asmyprogram -Yh
ormyprogram -h
.) I can build this by adding-X
and-Y
arguments, and run another function based on which arg was used, but clap needs to know all arguments, and that's why this would be nice.The text was updated successfully, but these errors were encountered: