Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an enum that represents subcommands with StructOpt?

Referring to the "Git" example of StructOpt, I do not understand how I am then supposed to use the data from the arguments.

I am fairly new to Rust so I am guessing it is obvious. Unfortunately, all examples I can find with an enum only do a println! on the object so I am stuck. I thought I'd do a match but it does not work.

How would you then find which commands was passed by the user to run your program?

#[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),
        _ => (),
    }
}

Compilation:

05:42 $ cargo run -- add -i
   Compiling example v0.1.0 (file:///Users/froyer/src/example)
error[E0532]: expected tuple struct/variant, found struct variant `Git::Add`
  --> src/main.rs:41:9
   |
41 |         Git::Add(cmd) => println!("{:?}", cmd.interactive),
   |         ^^^^^^^^ did you mean `Git::Add { /* fields */ }`?

error[E0618]: expected function, found enum variant `opt`
  --> src/main.rs:40:11
   |
37 |     let opt = Git::from_args();
   |         --- `opt` defined here
...
40 |     match opt() {
   |           ^^^^^ not a function
help: `opt` is a unit variant, you need to write it without the parenthesis
   |
40 |     match opt {
   |           ^^^
like image 223
Dante Avatar asked Jun 04 '18 04:06

Dante


2 Answers

Thanks to issue #1 in the structopt repository, I finally understood how it is supposed to work :)

fn main () {
    match Git::from_args() {
        Git::Add { interactive, patch, files } => {
            println!("{:?}", interactive)
        },
        Git::Commit { message, all } => {
            //...
        }
        _ => (),
    }
}
like image 112
Dante Avatar answered Nov 16 '22 02:11

Dante


I encountered the same issue and thought I would flush out @kellpossible 's example further:

#[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);
            },

        }
    }
}

Hopefully this will help but would be interested if someone knows a better way to do this.

like image 20
sbeskur Avatar answered Nov 16 '22 02:11

sbeskur