简体   繁体   English

如何使用structopt将特殊字符作为字符串参数传递?

[英]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.我正在使用 StructOpt 来获取用户想要搜索的单词。

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

The problem comes when you write something like command -a .当您编写诸如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:我知道该库正在尝试使用-a做某事并且struct中没有-a ,所以我尝试了: command "-a"但它显示:

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?有没有办法传递特殊字符,如-a并使用 StructOpt 检索信息( -a )?

You don't need to change anything, you only have to call the command using command -- -a .您无需更改任何内容,只需使用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.双破折号用于表示选项结束,在这种情况下,rest 是用户正在寻找的词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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