介绍
我目前有子命令正在使用structopt
,类似于其他答案。但是,我希望嵌套子命令的docker
工作方式类似:
docker image ls
docker image pull
当前代码
main.rs
mod cli;
use structopt::StructOpt;
use crate::cli::{Opt, Command};
fn main() {
let opt = Opt::from_args();
match opt.cmd {
Command::Subcommand { .. } => {
// Do something
}
}
}
cli/mod.rs
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub enum Command {
Subcommand {
// args
}
}
#[derive(StructOpt, Debug)]
pub struct Opt {
#[structopt(subcommand)]
pub cmd: Command,
}
我试过的
添加了另一个结构:main.rs
mod cli;
use structopt::StructOpt;
use crate::cli::{Opt, Resource};
fn main() {
let opt = Opt::from_args();
match opt.resource {
Resource::Command { .. } => {
// do something
}
}
}
cli/mod.rs
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub enum Command {
Subcommand {
// args
}
}
#[derive(StructOpt, Debug)]
pub struct Resource {
#[structopt(subcommand)]
image: Image
}
#[derive(StructOpt, Debug)]
pub struct Opt {
#[structopt(subcommand)]
pub resource: Resource,
}
但是我收到以下错误:
help: use fully-qualified syntax: `<Resource as Trait>::ImageCommand