A few weeks ago the CLI WG released paw, a crate to try and make CLI parsing more of a first-class citizen in Rust (post). This allows arguments to be passed into fn main as long as a trait is implemented. Obviously we also added structopt support.
So far it's been working quite well, but we think we could make this even better if we could add the trait to structopt directly:
__Proposed__
#[derive(structopt::StructOpt)]
struct Args {}
#[paw::main]
fn main(args: Args) {
println!("{}", args);
}
__Current__
#[derive(paw_structopt::StructOpt, structopt::StructOpt)]
struct Args {}
#[paw::main]
fn main(args: Args) {
println!("{}", args);
}
The patch to structopt would be about 6 lines inside the structopt macro:
impl paw::ParseArgs for #name {
type Error = std::io::Error;
fn parse_args() -> Result<Self, Self::Error> {
Ok(<#name as structopt::StructOpt>::from_args())
}
}
Would you be open to a patch that allows structopt to be used directly from main?
#[derive(structopt::StructOpt)]
struct Args {
#[structopt(flatten)]
port: clap_port_flag::Address,
#[structopt(flatten)]
port: clap_port_flag::Port,
}
#[paw::main]
fn main(args: Args) -> Result<(), std::io::Error> {
let listener = TcpListener::bind((&*args.address, args.port))?;
println!("listening on {}", listener.local_addr()?);
for stream in listener.incoming() {
stream?.write(b"hello world!")?;
}
Ok(())
}
Can't you just impl<T> ParseArg for T where T: StructOpt
@TeXitoi unfortunately we can't because we're not allowed to implement traits for foreign types (believe it's because of orphan rules):

Maybe I'm missing something tho?
As paw-raw is very small, I'm OK adding the generic impl in the structopt crate (and not in the generated code as proposed above).
@yoshuawuyts I let you open the PR? I don't know when I'll find the time to do that, but if you do it, it'll be faster.
@TeXitoi yeah for sure; I learned today that @gameldar has been working on a PR that sounds quite promising!
I've just created PR #192 to address this. After trying a few different methods I had to go back to doing this in the structopt macro as the error @yoshuawuyts was seeing shows up because of the way ParseArgs is invoked. The only way I could get this working was to do it in paw which introduced a dependency on structopt for paw which given the intention to move paw to std at some point (so cannot have dependencies) is not an option.
resolved by #192
Most helpful comment
As paw-raw is very small, I'm OK adding the generic impl in the structopt crate (and not in the generated code as proposed above).