1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
//! Simple argument parser //! //! The goals of this library are to correctly handle OsString and produce high-quality error //! messages. //! //! This library is like the traditional `getopt` with better error reporting. It converts an //! iterator of [`String`] or [`OsString`] to positional arguments and named arguments. //! //! Single and double hyphens are considered equivalent. This means that `-help` and `--help` are //! equivalent. Opinion: Most new projects should parse each argument as a separate flag. So, `-abc` //! should be parsed as one argument named `"abc"`, not three arguments named `"a"`, `"b"`, and //! `"c"`. Combining multiple flags into one argument is confusing, so it should only be used for //! programs that are called interactively very frequently, like `ls`. //! //! Options which take values can take values either as one argument, `-option=value`, or as two //! arguments, `-option value`. //! //! [`String`]: std::string::String //! [`OsString`]: std::ffi::OsString //! //! # Example //! //! ``` //! use simpleargs::{Arg, Args, UsageError, OptionError}; //! use std::ffi::OsString; //! use std::str::FromStr; //! //! fn parse_args<T>(mut args: Args<T>) -> Result<(), UsageError<OsString>> //! where //! T: Iterator<Item = OsString>, //! { //! // The input file //! let mut input: Option<OsString> = None; //! // True if -flag was set //! let mut flag = false; //! // The value of -xvalue, if it was used //! let mut xvalue: Option<i32> = None; //! loop { //! match args.next() { //! Arg::Positional(arg) => if input.is_some() { //! return Err(UsageError::UnexpectedArgument { arg }); //! } else { //! input = Some(arg) //! } //! Arg::Named(arg) => arg.parse(|name, value| match name { //! "flag" => { //! // parse() above will return an error for -flag=value, //! // because this function does not use 'value'. //! flag = true; //! Ok(()) //! } //! "xvalue" => { //! // Call as_str() for a str, or as_osstr() for OsStr. //! xvalue = Some(i32::from_str(value.as_str()?)?); //! Ok(()) //! } //! _ => Err(OptionError::Unknown), //! })?, //! Arg::End => break, //! Arg::Error(err) => return Err(err), //! } //! } //! let input = match input { //! Some(path) => path, //! None => return Err(UsageError::MissingArgument { name: "input".to_owned() }), //! }; //! Ok(()) //! } //! ``` #![deny(missing_docs)] pub mod arg; mod error; use std::ffi::OsStr; use arg::{ArgString, ParsedArg}; pub use error::{OptionError, UsageError}; /// A stream of arguments. pub struct Args<T> { args: T, allow_options: bool, } impl<T> Args<T> { /// Create an argument stream from an argument iterator. The program name should not be included /// in the argument stream. /// /// ``` /// use std::env; /// use simpleargs::Args; /// fn main() { /// let mut args_os = env::args_os(); /// args_os.next(); // Discard program name. /// let args = Args::from(args_os); /// } /// ``` pub fn from(args: T) -> Self { Args { args, allow_options: true, } } /// Get the remaining unparsed arguments in the stream. pub fn rest(self) -> T { self.args } } impl<T> Args<T> where T: Iterator, <T as Iterator>::Item: ArgString, { /// Get the next argument in the stream. pub fn next<'a>(&'a mut self) -> Arg<'a, T> { let arg = match self.args.next() { None => return Arg::End, Some(arg) => arg, }; if !self.allow_options { return Arg::Positional(arg); } let arg = match arg.parse_arg() { Err(arg) => return Arg::Error(UsageError::InvalidArgument { arg }), Ok(arg) => arg, }; match arg { ParsedArg::Positional(arg) => Arg::Positional(arg), ParsedArg::EndOfFlags => { self.allow_options = false; match self.args.next() { None => Arg::End, Some(arg) => Arg::Positional(arg), } } ParsedArg::Named(name, data) => Arg::Named(NamedArgument { name, data, args: self, }), } } } /// A single argument in a stream of arguments. pub enum Arg<'a, T> where T: Iterator, { /// A positional argument. Positional(T::Item), /// A named argument, possibly with an associated value. Named(NamedArgument<'a, T>), /// End of the argument stream. End, /// Invalid argument syntax. Error(UsageError<T::Item>), } /// A named command-line argument which may or may not have an associated value. pub struct NamedArgument<'a, T> where T: Iterator, { name: String, data: Option<<T as Iterator>::Item>, args: &'a mut Args<T>, } impl<'a, T> NamedArgument<'a, T> where T: Iterator, <T as Iterator>::Item: ArgString, { /// Parse the named command-line option. /// /// The option name and value are passed to the supplied function. Any errors that the function /// returns are annotated with information about the option. /// /// An error is returned if the user supplied a value, but [`as_str`] or [`as_osstr`] is not /// called. /// /// [`as_str`]: simpleargs::Value::as_str /// [`as_osstr`]: simpleargs::Value::as_osstr pub fn parse<U, F>(self, f: F) -> Result<U, UsageError<<T as Iterator>::Item>> where for<'b> F: FnOnce(&'b str, Value<'b, T>) -> Result<U, OptionError>, { let NamedArgument { name, mut data, args, } = self; let mut consumed = false; let err = match f( &name, Value { data: &mut data, args, consumed: &mut consumed, }, ) { Err(err) => err, Ok(r) => { if consumed || data.is_none() { return Ok(r); } else { OptionError::UnexpectedParameter } } }; Err(UsageError::InvalidOption { name, value: data, err, }) } } /// A handle for getting the value associated with a named flag.UsageError /// /// This handle can only be used once, and is consumed. pub struct Value<'a, T> where T: Iterator, { data: &'a mut Option<<T as Iterator>::Item>, args: &'a mut Args<T>, consumed: &'a mut bool, } impl<'a, T> Value<'a, T> where T: Iterator, { /// Get the associated value. /// /// Returns an error if the user did not supply a value. fn value(self) -> Result<&'a T::Item, OptionError> { *self.consumed = true; match self.data { Some(x) => Ok(x), None => match self.args.args.next() { Some(x) => Ok(self.data.get_or_insert(x)), None => Err(OptionError::MissingParameter), }, } } } impl<'a, T> Value<'a, T> where T: Iterator, <T as Iterator>::Item: ArgString, { /// Get the associated value as a string. /// /// Note that ownership of the string is not passed. Ownership is kept by the NamedArgument so /// it can be attached to error messages. /// /// Returns an error if the user did not supply a value. pub fn as_str(self) -> Result<&'a str, OptionError> { match self.value()?.to_str() { Some(x) => Ok(x), None => Err(OptionError::InvalidUnicode), } } /// Get the associated value as an OsStr. /// /// Note that ownership of the string is not passed. Ownership is kept by the NamedArgument so /// it can be attached to error messages. /// /// Returns an error if the user did not supply a value. pub fn as_osstr(self) -> Result<&'a OsStr, OptionError> { self.value().map(ArgString::to_osstr) } } #[cfg(test)] mod test { use super::*; use std::str::FromStr; #[derive(Debug, PartialEq, Eq)] struct Parsed { positional: Vec<String>, flag: bool, xvalue: Option<i32>, } fn parse_args(args: &'static [&'static str]) -> Result<Parsed, UsageError<String>> { let mut args = Args::from(args.iter().map(|&s| s.to_owned())); let mut positional = Vec::new(); let mut flag = false; let mut xvalue = None; loop { match args.next() { Arg::Positional(x) => positional.push(x), Arg::Named(arg) => arg.parse(|name, arg| match name { "flag" => { flag = true; Ok(()) } "x" => { xvalue = Some(i32::from_str(arg.as_str()?)?); Ok(()) } _ => Err(OptionError::Unknown), })?, Arg::End => break, Arg::Error(err) => return Err(err), } } Ok(Parsed { positional, flag, xvalue, }) } #[test] fn success() { match parse_args(&["abc", "--flag", "-x=10", "--", "--", "--arg"]) { Err(err) => panic!("err: {:?}", err), Ok(r) => assert_eq!( r, Parsed { positional: vec!["abc".to_owned(), "--".to_owned(), "--arg".to_owned()], flag: true, xvalue: Some(10), } ), } } #[test] fn no_param() { let r = parse_args(&["--x"]); if let Err(e) = &r { if let UsageError::InvalidOption { name, value, err } = e { assert_eq!(name, "x"); assert!(value.is_none()); if let OptionError::MissingParameter = err { return; } } } panic!("incorrect result: {:?}", r); } #[test] fn bad_param() { let r = parse_args(&["-x", "0q"]); if let Err(e) = &r { if let UsageError::InvalidOption { name, value, err } = e { assert_eq!(name, "x"); assert_eq!(value, &Some("0q".to_owned())); if let OptionError::InvalidValue(_) = err { return; } } } panic!("incorrect result: {:?}", r); } }