From 25327fa44161ffe8b04f947d3b8936afc65ffce7 Mon Sep 17 00:00:00 2001 From: Zach Dziura Date: Thu, 30 Apr 2015 15:14:34 +0000 Subject: [PATCH] Now returns an error when not all program args are passed via command-line There was a lot of stuff added here. The most important addition is how Pirate now returns a MissingArgument kinded error if a required program argument isn't passed to the program. --- README.md | 11 ++++++----- src/errors.rs | 16 +++++++++------- src/lib.rs | 7 +++++-- src/opts.rs | 4 ++++ tests/main.rs | 1 + 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 598e824..d428063 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ Ever. In all seriousness, `getopts` is a fantastic library that gives the developers all of the tools necessary to create and interface with command-line arguments. However, with all that power comes -complexity. `getopts`, while straight forward to use, is verbose. The developer has to make repeated -method calls to add different command-line options. And while the only victim here is the -developer's wrists due to carpal tunnel, I felt that there was a better way to do things. +complexity. `getopts` -- while straight forward to use -- is verbose. The developer has to call +different functions repeatedly in order to add different command-line options to their programs. And +while the only victim here is the developer's wrists due to carpal tunnel, I felt that there was a +better way to do things. Enter Pirate (which should totally usurp `getopts` for the award of Most Originally Named Project Ever). @@ -32,7 +33,7 @@ pirate = "0.1.0" and this to your crate root: ```rust -extern crate getopts; +extern crate pirate; ``` Usage @@ -145,7 +146,7 @@ To Do - [ ] Create a helper function for generating `--help` output, rather than having the user create it manually. - [ ] Also create helper functions for defining the description section of the `--help` output. -- [ ] Refactor the `ErrorKind` enum into a struct that is able to represent more complex data (such +- [x] Refactor the `ErrorKind` enum into a struct that is able to represent more complex data (such giving the value of the invalid argument passed to the program). License diff --git a/src/errors.rs b/src/errors.rs index 4c1cf8a..d5f2733 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -28,34 +28,36 @@ pub struct Error { impl Error { pub fn new(kind: ErrorKind, offender: String) -> Error { Error { - kind: kind, - offender: offender, + kind: kind.clone(), + offender: offender.clone(), + desc: format!("{} {}", kind.description(), offender.clone()), } } } impl error::Error for Error { fn description(&self) -> &str { - &format!("{} {}", self.kind.description(), self.offender); + &self.desc } } impl Display for Error { fn fmt(&self, f: &mut Formatter) -> Result { - write!(f, "{}", self.description()) + write!(f, "{}", self.desc) } } +#[derive(Clone, Debug)] pub enum ErrorKind { InvalidOption, MissingArgument, } impl ErrorKind { - fn description(&self) -> &str { + fn description(&self) -> String { match *self { - ErrorKind::InvalidOption => "An invalid option was passed to the program:", - ErrorKind::MissingArgument => "A required argument is missing:", + ErrorKind::InvalidOption => String::from("An invalid option was passed to the program:"), + ErrorKind::MissingArgument => String::from("A required argument is missing:"), } } } diff --git a/src/lib.rs b/src/lib.rs index 4c1a5c6..4c42714 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ pub use errors::{Error, ErrorKind}; pub use matches::Matches; use opts::Opts; -pub fn parse(mut args: Args, options: &[&'static str]) -> Result { +pub fn parse(mut args: Args, options: &[&'static str]) -> Result { let mut matches: Matches = Matches::new(); let mut opts: Opts = opts(options); // Jesus, this is redundant... @@ -73,7 +73,10 @@ pub fn parse(mut args: Args, options: &[&'static str]) -> Result Ok(matches), + _ => Err(Error::new(ErrorKind::MissingArgument, opts.get_arg().unwrap())), + } } fn opts(opts: &[&'static str]) -> Opts { diff --git a/src/opts.rs b/src/opts.rs index 42f7efa..df042e1 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -50,4 +50,8 @@ impl Opts { pub fn get_arg(&mut self) -> Option { self.args.pop_front() } + + pub fn arg_len(&self) -> usize { + self.args.len() + } } diff --git a/tests/main.rs b/tests/main.rs index 96206cb..ba8dbc0 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -23,6 +23,7 @@ fn main() { } let input = matches.get("input").unwrap().parse::().unwrap(); + let num = match matches.get("n") { Some(n) => n.parse::().unwrap(), None => 1