Dan*_*nte 8 enums arguments rust
在提到StructOpt的“ Git”示例时,我不明白如何使用自变量中的数据。
我对Rust还是很陌生,所以我猜很明显。不幸的是,我可以用枚举找到的所有示例仅对println!对象执行一个操作,因此我被困住了。我以为我会做一个,match但没有用。
然后,您如何查找用户传递了哪些命令来运行程序?
#[macro_use]
extern crate structopt;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "git", about = "the stupid content tracker")]
enum Git {
#[structopt(name = "add")]
Add {
#[structopt(short = "i")]
interactive: bool,
#[structopt(short = "p")]
patch: bool,
#[structopt(parse(from_os_str))]
files: Vec<PathBuf>
},
#[structopt(name = "fetch")]
Fetch {
#[structopt(long = "dry-run")]
dry_run: bool,
#[structopt(long = "all")]
all: bool,
repository: Option<String>
},
#[structopt(name = "commit")]
Commit {
#[structopt(short = "m")]
message: Option<String>,
#[structopt(short = "a")]
all: bool
}
}
fn main() {
let opt = Git::from_args();
println!("{:?}", opt);
match opt() {
Git::Add(cmd) => println!("{:?}", cmd.interactive),
_ => (),
}
}
Run Code Online (Sandbox Code Playgroud)
汇编:
#[macro_use]
extern crate structopt;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "git", about = "the stupid content tracker")]
enum Git {
#[structopt(name = "add")]
Add {
#[structopt(short = "i")]
interactive: bool,
#[structopt(short = "p")]
patch: bool,
#[structopt(parse(from_os_str))]
files: Vec<PathBuf>
},
#[structopt(name = "fetch")]
Fetch {
#[structopt(long = "dry-run")]
dry_run: bool,
#[structopt(long = "all")]
all: bool,
repository: Option<String>
},
#[structopt(name = "commit")]
Commit {
#[structopt(short = "m")]
message: Option<String>,
#[structopt(short = "a")]
all: bool
}
}
fn main() {
let opt = Git::from_args();
println!("{:?}", opt);
match opt() {
Git::Add(cmd) => println!("{:?}", cmd.interactive),
_ => (),
}
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*nte 10
感谢structopt存储库中的问题#1,我终于了解了它应该如何工作:)
fn main () {
match Git::from_args() {
Git::Add { interactive, patch, files } => {
println!("{:?}", interactive)
},
Git::Commit { message, all } => {
//...
}
_ => (),
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,并认为我会进一步清除 @kellpossible 的例子:
#[macro_use]
extern crate structopt;
pub use structopt::StructOpt;
use std::path::PathBuf;
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about="how to use struct-opt crate")]
pub struct Opts{
#[structopt(short = "v", parse(from_occurrences))]
verbosity: u8,
// SUBCOMMANDS
#[structopt(subcommand)]
commands: Option<Git>
}
#[derive(StructOpt, Debug)]
#[structopt(name = "git", about = "the stupid content tracker")]
enum Git {
#[structopt(name = "add")]
Add (AddOpts),
#[structopt(name = "fetch")]
Fetch(FetchOpts),
#[structopt(name = "commit")]
Commit(CommitOpts)
}
#[derive(StructOpt, Debug)]
struct AddOpts {
#[structopt(short = "i")]
interactive: bool,
#[structopt(short = "p")]
patch: bool,
#[structopt(parse(from_os_str))]
files: Vec<PathBuf>
}
#[derive(Debug, StructOpt)]
pub struct FetchOpts {
#[structopt(long = "dry-run")]
dry_run: bool,
#[structopt(long = "all")]
all: bool,
repository: Option<String>
}
#[derive(Debug, StructOpt)]
pub struct CommitOpts {
#[structopt(short = "m")]
message: Option<String>,
#[structopt(short = "a")]
all: bool
}
fn main() {
println!("Hello subcommands!");
let opt = Opts::from_args();
handle_subcommand(opt);
}
fn handle_subcommand(opt: Opts){
// handle subcommands
if let Some(subcommand) = opt.commands{
match subcommand {
Git::Add(cfg) => {
println!("handle Add: {:?}", cfg);
},
Git::Commit(cfg) => {
println!("handle Commit: {:?}", cfg);
},
Git::Fetch(cfg) => {
println!("handle Fetch: {:?}", cfg);
},
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,但如果有人知道更好的方法会感兴趣。
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |