Talel BELHAJSALEM
Talel BELHAJSALEM

Reputation: 4324

Rust structopt fails to get argument value starting with -

I have a program that runs a custom command, lets say it is called cmd, and the program can take custom arguments for that command. So, using structopt crate I can created a struct that looks like the following:

#[derive(StructOpt)]
struct Args {
    #[structopt(short, long)
    custom: String
}

So, when running the program with the following example:

cargo run -- -c "-e env"

-e env is an argument for the cmd command that will be run as a child process.

But, the program fails with the following error:

error: Found argument '-e env' which wasn't expected, or isn't valid in this context

I already checked this answer, but it is not the same case as mine.

What I tried is the following:

fn parse_custom_command(custom: &str) -> Result<String, ParseCharError> {
    custom.split_whitespace().collect()
}

#[derive(StructOpt)]
struct Args {
    #[structopt(short, long, parse(try_from_str = parse_custom_command)]
    custom: String
}

But, I get the same error.

I know that is hard to tell the parser that -e is not an option in the struct, I think it needs to know at lease one of two:

How can I solve this issue ?

Upvotes: 1

Views: 73

Answers (0)

Related Questions