TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.71k stars 150 forks source link

Question - how can I print the arguments given an opt instance? #441

Closed avnerbarr closed 2 years ago

avnerbarr commented 3 years ago

I want to get the string representation leading to a particular Opt instance (after I've modified it).

for instance

#[derive(StructOpt)]
struct Opt {
a : String,
b: String,
c: i32,
...
z : String
}

...
let opts = Opt::from_args();
...
if some condition occured, change opts and print the correct representation {
  let new_opts = Opt { z: "this_value_should_be_written", ..Opt::from_args() }; // change some values
  print(how do I print "new_opts" in such a way that is valid for the Opt parser for next run?)
}
EverlastingBugstopper commented 3 years ago

you could add a debug derivation like so: #[derive(StructOpt, Debug)] and then print like println!("{:?}, new_opts); or even println!("{:#?}", new_opts);

TeXitoi commented 3 years ago

AFAIK, that's not possible, and it will be a quite complicated feature, relying deeply in clap internal.

therealbnut commented 3 years ago

Adding to this I want to use structopt for IPC with a forked child process, and so I'd like to be able to go from an Opt structure to a Vec/Iterator of argument strings.

ie.

enum Opt {
    #[structopt(short, long)]
    host_name: String,
}

// ["--host_name", "my-server"]
(Opt { host_name: "my-server".to_owned() }).args()

If this was possible then you could emit debugging info by doing:

println!("{:?}", op.args())
Xaeroxe commented 2 years ago

@TeXitoi would you be open to a PR implementing this?

TeXitoi commented 2 years ago

Better to propose to clapv3

TeXitoi commented 2 years ago

This is an enhancement, and structopt is now feature frozen.