structopt as main args through rust-cli/paw

Created on 26 May 2019  路  7Comments  路  Source: TeXitoi/structopt

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);
}

Implementation

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?


Full TCP Example

#[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(())
}
enhancement

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).

All 7 comments

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):

2019-05-27-114218_1920x1080

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wez picture wez  路  7Comments

jackjen picture jackjen  路  7Comments

lucab picture lucab  路  9Comments

mbrobbel picture mbrobbel  路  5Comments

TedDriggs picture TedDriggs  路  6Comments