Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add structopt #29

Closed
wants to merge 3 commits into from
Closed

Add structopt #29

wants to merge 3 commits into from

Conversation

Urhengulas
Copy link
Member

This PR introduces sctructopt for handling cli-params.

  • I tried to use argument names and descriptions like GNU ld
  • using structopt enables us to drop fn get_output_path(...) and dramatically shorten fn get_linker_scripts(...)

@Urhengulas Urhengulas marked this pull request as draft March 29, 2021 16:29
@Urhengulas
Copy link
Member Author

This PR is blocked by the fact that we need to accept way more args than we are actually using here. In best case we can just ignore them, but I couldn't figure out how to do that.

I've tried clap::AppSettings::TrailingVarArg, but this didn't work so far, since we also need to accept arguments in the middle, and not only at the end.

@Urhengulas
Copy link
Member Author

clap-rs/clap#1404 would be helpful for solving our blocker.

@Urhengulas
Copy link
Member Author

One approach I though might be useful is to "pre-filter" the arguments (std::env::args()) and then parse them with StructOpt::from_iter(...). A sketch is appended.

More generally some lightweight parser to extract specific args from the cli-args and ignore the rest, would be very useful for our kind of cli (being a cli-wrapper around another cli)!

pre-filter example

use std::{env, 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 app = Opt::clap().p.opts;

    // get long arguments, e.g. `--output`, `--script`
    let mut long = app.iter().filter_map(|opt| opt.s.long).collect::<Vec<_>>();
    long.extend(["help", "version"].iter());

    // get short arguments, e.g. `-o`, `-T`
    let mut short = app.iter().filter_map(|x| x.s.short).collect::<Vec<_>>();
    short.extend(['H', 'V'].iter());

    dbg!(long, short);

    // filter one element args
    let short_args = env::args()
        // check if `x` starts with '-' and 2nd char is in `short`
        .filter(|x| unimplemented!())
        .collect::<Vec<_>>();
    // filter two element args
    let long_args: Vec<&[String]> = unimplemented!();

    // merge args
    let args: Vec<String> = unimplemented!(); // shorts_args + long_args

    let opt: Opt = Opt::from_iter(args);
    dbg!(opt);
}

@Urhengulas Urhengulas closed this Aug 19, 2021
@Urhengulas Urhengulas deleted the add-structopt branch July 20, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant