rustrust-cargodocopt

Which is the command to run docopt in Rust


I am struggeling to find out which is the command to run a simple example of docopt but always I get the wrong result.

#[macro_use]
extern crate serde_derive;

use docopt::Docopt;

// Write the Docopt usage string.
const USAGE: &'static str = "
Usage: cp [-a] <source> <dest>
       cp [-a] <source>... <dir>
       tk [-a] <source> <dest>

Options:
    -a, --archive  Copy everything.
";

#[derive(Debug, Deserialize)]
struct Args {
    arg_source: String,
    arg_dest: String,
    arg_dir: String,
    flag_archive: bool,
}

fn main() {
    //let version = env!("CARGO_PKG_NAME").to_string() + ", version: " + env!("CARGO_PKG_VERSION");
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| {
            println!("{:?}", d);
            d.deserialize()
        })
        .unwrap_or_else(|e| {
            println!("Error");
            e.exit()
        });

    println!("{:?}", args);
}

For example if I run that command in the CLI cargo run -- cp -a file_a dest_a the argument struct is not correct one

Args { arg_source: "", arg_dest: "", arg_dir: "des", flag_archive: true }

V2:

Now with that new implementation and adding the binary name to the commands, still not finding the right way to get the commands:

const USAGE: &'static str = "
Usage: 
    terminal --arch ARCHIVE --dest DESTIN

Options:
    --arch, -a  The file that you want to copy
    --dest, -d  Where you want to copy the file
";

#[derive(Debug, Deserialize)]
struct Args {
    flag_arch: Option<String>,
    flag_dest: Option<String>,
}

The following commands I am running

cargo run -- terminal --arch muun --dest hello
// Args { flag_arch: Some(""), flag_dest: Some("") }
cargo run -- terminal --arch muun --dest hello 
// Error

Solution

  • After few iterations and some help I got working the command line parser with docopt running the following command: cargo run -- --arch muun --dest hello

    #[macro_use]
    extern crate serde_derive;
    
    use docopt::Docopt;
    
    // Write the Docopt usage string.
    const USAGE: &'static str = "
    Usage: 
        terminal --arch ARCHIVE --dest DESTIN 
    
    Options:
        --arch ARCHIVE  The file that you want to copy
        --dest DESTIN  Where you want to copy the file
    ";
    
    #[derive(Debug, Deserialize)]
    struct Args {
        flag_arch: Option<String>,
        flag_dest: Option<String>,
    }
    
    fn main() {
        //let version = env!("CARGO_PKG_NAME").to_string() + ", version: " + env!("CARGO_PKG_VERSION");
        let args: Args = Docopt::new(USAGE)
            .and_then(|d| {
                println!("{:?}", d);
                d.deserialize()
            })
            .unwrap_or_else(|e| {
                println!("Error");
                println!("{:?}", e);
                e.exit()
            });
    
        println!("Archive: {:?}", args.flag_arch);
        println!("Destination: {:?}", args.flag_dest);
        println!("Args: {:?}", args);
    }