有没有办法让用户提示输入括号内的字节并用逗号或类似的东西分隔?
./main bytes [0, 1, 2, 3, 4, 5]
我设法让它看起来像这样:
./main bytes 0 1 2 3 4 5
这是我的代码:
extern crate docopt;
#[macro_use]
extern crate serde_derive;
use docopt::Docopt;
const USAGE: &'static str = "
Puzzle Solver.
Usage:
puzzle_solver string <text>
puzzle_solver bytes [<bin>...]
puzzle_solver (-h | --help)
puzzle_solver --version
Options:
-h --help Show this screen.
--version Show version.
";
#[derive(Debug, Deserialize)]
struct Args {
cmd_string: bool,
arg_text: Option<String>,
cmd_bytes: bool,
arg_bin: Option<Vec<u8>>,
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
println!("ARGS: {:?}", args);
}
这是可能的,但您必须Deserialize
手动实施。
Vec<u8>
已经实现Deserialize
,并且该实现不知道包含逗号分隔的括号列表的字符串,也不知道docopt::Deserializer
,因为在命令行上传递列表的正常方法是逐个元素。因此,您必须创建一个新类型,该类型将从您想要的格式反序列化。
当然,您也可以实现Deref<Target = Vec<u8>>
与DerefMut
对Bytes
,如果你想要把它作为一个Vec<u8>
。有些人可能认为这是对 的轻微误用Deref
,但在这种情况下可能没问题。
extern crate docopt;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use docopt::Docopt;
use serde::de;
use std::fmt;
const USAGE: &'static str = "
Puzzle Solver.
Usage:
puzzle_solver string <text>
puzzle_solver bytes <bin>
puzzle_solver (-h | --help)
puzzle_solver --version
Options:
-h --help Show this screen.
--version Show version.
";
#[derive(Debug, Deserialize)]
struct Args {
cmd_string: bool,
arg_text: Option<String>,
cmd_bytes: bool,
arg_bin: Option<Bytes>,
}
#[derive(Debug)]
struct Bytes(Vec<u8>);
impl<'de> de::Deserialize<'de> for Bytes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct BytesVisitor;
impl<'de> de::Visitor<'de> for BytesVisitor {
type Value = Bytes;
fn expecting(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(formatter, "a bracketed, comma-delimited string")
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
let v = if v.starts_with('[') && v.ends_with(']') {
&v[1..v.len() - 1]
} else {
return Err(E::custom(format!("expected a bracketed list, got {:?}", v)));
};
let values: Result<Vec<u8>, _> = v.split(",").map(|s| s.trim().parse()).collect();
Ok(Bytes(values.map_err(E::custom)?))
}
}
deserializer.deserialize_str(BytesVisitor)
}
}
这是在操场上。这些是我为使其正常工作所做的更改:
[<bin>...]
为<bin>
这样 docopt 将知道寻找单个事物而不是一系列......事物。(如果你不这样做,docopt 实际上只是给你一个空字符串。)Bytes
周围引入 newtype包装器Vec<u8>
。serde::de::Deserialize
了Bytes
。这需要创建一个实现serde::de::Visitor
trait的结构,将分离字符串的代码放入其visit_str
方法中,并将访问者传递给deserialize_str
,这告诉Deserializer
期望一个字符串并将其传递给访问者的visit_str
。直到快完成时我才意识到它,但您可以visit_seq
改为实现,并使其解析bytes [1, 2, 3]
(不引用列表)。但我不会,因为这违反了命令行约定;如果您无论如何都使用 shell 来拆分参数,那么您应该一路走下去,只接受bytes 1 2 3
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句