简体   繁体   中英

How to pass special characters as string argument using structopt?

I have a command line to search a word inside a file. I'm using StructOpt to get the word that the user wants to search.

#[derive(Debug, StructOpt)]
pub struct Command {
    pub word_to_search: Option<String>,
}

The problem comes when you write something like command -a . I know that the library is trying to do something with -a and there is no -a in the struct , so I tried: command "-a" but it displays:

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

Is there a way to pass special characters like -a and retrieve the information ( -a ) using StructOpt?

You don't need to change anything, you only have to call the command using command -- -a . The double dash is used to indicate that it's the end of options and in this case the rest is the word that the user is looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM