docopt / docopt.rs

Docopt for Rust (command line argument parser).
The Unlicense
754 stars 83 forks source link

Can't decode paths #135

Open remram44 opened 9 years ago

remram44 commented 9 years ago

Surely someone has written a program that uses files, so I'm wondering if I'm the one doing something wrong?

It looks like, when encountering a PathBuf field, docopt parses the argument as a number, and fills the PathBuf with a single byte with that value.

extern crate docopt;
extern crate rustc_serialize;

use docopt::Docopt;
use std::path::PathBuf;

static USAGE: &'static str = "
foo generator.

Usage:
  foo <path>

Options:
  -h --help     Show this screen.
";

#[derive(Debug, RustcDecodable)]
struct Args {
    arg_path: PathBuf,
}

fn main() {
    {
        let argv = ["foo", "97"];
        let args: Args = Docopt::new(USAGE)
                                 .unwrap()
                                 .argv(argv.iter())
                                 .decode()
                                 .unwrap();
        println!("{:?}", args); // Args { arg_path: "a" }
    }
    {
        let argv = ["foo", "some/path"];
        let args: Args = Docopt::new(USAGE)
                                 .unwrap()
                                 .argv(argv.iter())
                                 .decode()
                                 .unwrap(); // panics
    }
}
remram44 commented 9 years ago

Because decoding PathBuf is decoding Vec<u8>, I'm assuming that docopt mistakes a PathBuf option for a u8 option that can be passed multiple times (note that Vec<PathBuf> still doesn't work).