parser.rs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. //! A general parser for command-line options.
  2. //!
  3. //! exa uses its own hand-rolled parser for command-line options. It supports
  4. //! the following syntax:
  5. //!
  6. //! - Long options: `--inode`, `--grid`
  7. //! - Long options with values: `--sort size`, `--level=4`
  8. //! - Short options: `-i`, `-G`
  9. //! - Short options with values: `-ssize`, `-L=4`
  10. //!
  11. //! These values can be mixed and matched: `exa -lssize --grid`. If you’ve used
  12. //! other command-line programs, then hopefully it’ll work much like them.
  13. //!
  14. //! Because exa already has its own files for the help text, shell completions,
  15. //! man page, and readme, so it can get away with having the options parser do
  16. //! very little: all it really needs to do is parse a slice of strings.
  17. //!
  18. //!
  19. //! ## UTF-8 and `OsStr`
  20. //!
  21. //! The parser uses `OsStr` as its string type. This is necessary for exa to
  22. //! list files that have invalid UTF-8 in their names: by treating file paths
  23. //! as bytes with no encoding, a file can be specified on the command-line and
  24. //! be looked up without having to be encoded into a `str` first.
  25. //!
  26. //! It also avoids the overhead of checking for invalid UTF-8 when parsing
  27. //! command-line options, as all the options and their values (such as
  28. //! `--sort size`) are guaranteed to just be 8-bit ASCII.
  29. use std::ffi::{OsStr, OsString};
  30. use std::fmt;
  31. use options::Misfire;
  32. /// A **short argument** is a single ASCII character.
  33. pub type ShortArg = u8;
  34. /// A **long argument** is a string. This can be a UTF-8 string, even though
  35. /// the arguments will all be unchecked OsStrings, because we don’t actually
  36. /// store the user’s input after it’s been matched to a flag, we just store
  37. /// which flag it was.
  38. pub type LongArg = &'static str;
  39. /// A **list of values** that an option can have, to be displayed when the
  40. /// user enters an invalid one or skips it.
  41. ///
  42. /// This is literally just help text, and won’t be used to validate a value to
  43. /// see if it’s correct.
  44. pub type Values = &'static [&'static str];
  45. /// A **flag** is either of the two argument types, because they have to
  46. /// be in the same array together.
  47. #[derive(PartialEq, Debug, Clone)]
  48. pub enum Flag {
  49. Short(ShortArg),
  50. Long(LongArg),
  51. }
  52. impl Flag {
  53. pub fn matches(&self, arg: &Arg) -> bool {
  54. match *self {
  55. Flag::Short(short) => arg.short == Some(short),
  56. Flag::Long(long) => arg.long == long,
  57. }
  58. }
  59. }
  60. impl fmt::Display for Flag {
  61. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  62. match *self {
  63. Flag::Short(short) => write!(f, "-{}", short as char),
  64. Flag::Long(long) => write!(f, "--{}", long),
  65. }
  66. }
  67. }
  68. /// Whether redundant arguments should be considered a problem.
  69. #[derive(PartialEq, Debug, Copy, Clone)]
  70. pub enum Strictness {
  71. /// Throw an error when an argument doesn’t do anything, either because
  72. /// it requires another argument to be specified, or because two conflict.
  73. ComplainAboutRedundantArguments,
  74. /// Search the arguments list back-to-front, giving ones specified later
  75. /// in the list priority over earlier ones.
  76. UseLastArguments,
  77. }
  78. /// Whether a flag takes a value. This is applicable to both long and short
  79. /// arguments.
  80. #[derive(Copy, Clone, PartialEq, Debug)]
  81. pub enum TakesValue {
  82. /// This flag has to be followed by a value.
  83. /// If there’s a fixed set of possible values, they can be printed out
  84. /// with the error text.
  85. Necessary(Option<Values>),
  86. /// This flag will throw an error if there’s a value after it.
  87. Forbidden,
  88. }
  89. /// An **argument** can be matched by one of the user’s input strings.
  90. #[derive(PartialEq, Debug)]
  91. pub struct Arg {
  92. /// The short argument that matches it, if any.
  93. pub short: Option<ShortArg>,
  94. /// The long argument that matches it. This is non-optional; all flags
  95. /// should at least have a descriptive long name.
  96. pub long: LongArg,
  97. /// Whether this flag takes a value or not.
  98. pub takes_value: TakesValue,
  99. }
  100. impl fmt::Display for Arg {
  101. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  102. write!(f, "--{}", self.long)?;
  103. if let Some(short) = self.short {
  104. write!(f, " (-{})", short as char)?;
  105. }
  106. Ok(())
  107. }
  108. }
  109. /// Literally just several args.
  110. #[derive(PartialEq, Debug)]
  111. pub struct Args(pub &'static [&'static Arg]);
  112. impl Args {
  113. /// Iterates over the given list of command-line arguments and parses
  114. /// them into a list of matched flags and free strings.
  115. pub fn parse<'args, I>(&self, inputs: I, strictness: Strictness) -> Result<Matches<'args>, ParseError>
  116. where I: IntoIterator<Item=&'args OsString> {
  117. use std::os::unix::ffi::OsStrExt;
  118. use self::TakesValue::*;
  119. let mut parsing = true;
  120. // The results that get built up.
  121. let mut result_flags = Vec::new();
  122. let mut frees: Vec<&OsStr> = Vec::new();
  123. // Iterate over the inputs with ā€œwhile letā€ because we need to advance
  124. // the iterator manually whenever an argument that takes a value
  125. // doesn’t have one in its string so it needs the next one.
  126. let mut inputs = inputs.into_iter();
  127. while let Some(arg) = inputs.next() {
  128. let bytes = arg.as_bytes();
  129. // Stop parsing if one of the arguments is the literal string ā€œ--ā€.
  130. // This allows a file named ā€œ--argā€ to be specified by passing in
  131. // the pair ā€œ-- --argā€, without it getting matched as a flag that
  132. // doesn’t exist.
  133. if !parsing {
  134. frees.push(arg)
  135. }
  136. else if arg == "--" {
  137. parsing = false;
  138. }
  139. // If the string starts with *two* dashes then it’s a long argument.
  140. else if bytes.starts_with(b"--") {
  141. let long_arg_name = OsStr::from_bytes(&bytes[2..]);
  142. // If there’s an equals in it, then the string before the
  143. // equals will be the flag’s name, and the string after it
  144. // will be its value.
  145. if let Some((before, after)) = split_on_equals(long_arg_name) {
  146. let arg = self.lookup_long(before)?;
  147. let flag = Flag::Long(arg.long);
  148. match arg.takes_value {
  149. Necessary(_) => result_flags.push((flag, Some(after))),
  150. Forbidden => return Err(ParseError::ForbiddenValue { flag })
  151. }
  152. }
  153. // If there’s no equals, then the entire string (apart from
  154. // the dashes) is the argument name.
  155. else {
  156. let arg = self.lookup_long(long_arg_name)?;
  157. let flag = Flag::Long(arg.long);
  158. match arg.takes_value {
  159. Forbidden => result_flags.push((flag, None)),
  160. Necessary(values) => {
  161. if let Some(next_arg) = inputs.next() {
  162. result_flags.push((flag, Some(next_arg)));
  163. }
  164. else {
  165. return Err(ParseError::NeedsValue { flag, values })
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // If the string starts with *one* dash then it’s one or more
  172. // short arguments.
  173. else if bytes.starts_with(b"-") && arg != "-" {
  174. let short_arg = OsStr::from_bytes(&bytes[1..]);
  175. // If there’s an equals in it, then the argument immediately
  176. // before the equals was the one that has the value, with the
  177. // others (if any) as value-less short ones.
  178. //
  179. // -x=abc => ā€˜x=abc’
  180. // -abcdx=fgh => ā€˜a’, ā€˜b’, ā€˜c’, ā€˜d’, ā€˜x=fgh’
  181. // -x= => error
  182. // -abcdx= => error
  183. //
  184. // There’s no way to give two values in a cluster like this:
  185. // it’s an error if any of the first set of arguments actually
  186. // takes a value.
  187. if let Some((before, after)) = split_on_equals(short_arg) {
  188. let (arg_with_value, other_args) = before.as_bytes().split_last().unwrap();
  189. // Process the characters immediately following the dash...
  190. for byte in other_args {
  191. let arg = self.lookup_short(*byte)?;
  192. let flag = Flag::Short(*byte);
  193. match arg.takes_value {
  194. Forbidden => result_flags.push((flag, None)),
  195. Necessary(values) => return Err(ParseError::NeedsValue { flag, values })
  196. }
  197. }
  198. // ...then the last one and the value after the equals.
  199. let arg = self.lookup_short(*arg_with_value)?;
  200. let flag = Flag::Short(arg.short.unwrap());
  201. match arg.takes_value {
  202. Necessary(_) => result_flags.push((flag, Some(after))),
  203. Forbidden => return Err(ParseError::ForbiddenValue { flag })
  204. }
  205. }
  206. // If there’s no equals, then every character is parsed as
  207. // its own short argument. However, if any of the arguments
  208. // takes a value, then the *rest* of the string is used as
  209. // its value, and if there’s no rest of the string, then it
  210. // uses the next one in the iterator.
  211. //
  212. // -a => ā€˜a’
  213. // -abc => ā€˜a’, ā€˜b’, ā€˜c’
  214. // -abxdef => ā€˜a’, ā€˜b’, ā€˜x=def’
  215. // -abx def => ā€˜a’, ā€˜b’, ā€˜x=def’
  216. // -abx => error
  217. //
  218. else {
  219. for (index, byte) in bytes.into_iter().enumerate().skip(1) {
  220. let arg = self.lookup_short(*byte)?;
  221. let flag = Flag::Short(*byte);
  222. match arg.takes_value {
  223. Forbidden => result_flags.push((flag, None)),
  224. Necessary(values) => {
  225. if index < bytes.len() - 1 {
  226. let remnants = &bytes[index+1 ..];
  227. result_flags.push((flag, Some(OsStr::from_bytes(remnants))));
  228. break;
  229. }
  230. else if let Some(next_arg) = inputs.next() {
  231. result_flags.push((flag, Some(next_arg)));
  232. }
  233. else {
  234. return Err(ParseError::NeedsValue { flag, values })
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. // Otherwise, it’s a free string, usually a file name.
  242. else {
  243. frees.push(arg)
  244. }
  245. }
  246. Ok(Matches { frees, flags: MatchedFlags { flags: result_flags, strictness } })
  247. }
  248. fn lookup_short(&self, short: ShortArg) -> Result<&Arg, ParseError> {
  249. match self.0.into_iter().find(|arg| arg.short == Some(short)) {
  250. Some(arg) => Ok(arg),
  251. None => Err(ParseError::UnknownShortArgument { attempt: short })
  252. }
  253. }
  254. fn lookup_long<'b>(&self, long: &'b OsStr) -> Result<&Arg, ParseError> {
  255. match self.0.into_iter().find(|arg| arg.long == long) {
  256. Some(arg) => Ok(arg),
  257. None => Err(ParseError::UnknownArgument { attempt: long.to_os_string() })
  258. }
  259. }
  260. }
  261. /// The **matches** are the result of parsing the user’s command-line strings.
  262. #[derive(PartialEq, Debug)]
  263. pub struct Matches<'args> {
  264. /// The flags that were parsed from the user’s input.
  265. pub flags: MatchedFlags<'args>,
  266. /// All the strings that weren’t matched as arguments, as well as anything
  267. /// after the special "--" string.
  268. pub frees: Vec<&'args OsStr>,
  269. }
  270. #[derive(PartialEq, Debug)]
  271. pub struct MatchedFlags<'args> {
  272. /// The individual flags from the user’s input, in the order they were
  273. /// originally given.
  274. ///
  275. /// Long and short arguments need to be kept in the same vector because
  276. /// we usually want the one nearest the end to count, and to know this,
  277. /// we need to know where they are in relation to one another.
  278. flags: Vec<(Flag, Option<&'args OsStr>)>,
  279. /// Whether to check for duplicate or redundant arguments.
  280. strictness: Strictness,
  281. }
  282. impl<'a> MatchedFlags<'a> {
  283. /// Whether the given argument was specified.
  284. /// Returns `true` if it was, `false` if it wasn’t, and an error in
  285. /// strict mode if it was specified more than once.
  286. pub fn has(&self, arg: &'static Arg) -> Result<bool, Misfire> {
  287. self.has_where(|flag| flag.matches(arg)).map(|flag| flag.is_some())
  288. }
  289. /// Returns the first found argument that satisfies the predicate, or
  290. /// nothing if none is found, or an error in strict mode if multiple
  291. /// argument satisfy the predicate.
  292. ///
  293. /// You’ll have to test the resulting flag to see which argument it was.
  294. pub fn has_where<P>(&self, predicate: P) -> Result<Option<&Flag>, Misfire>
  295. where P: Fn(&Flag) -> bool {
  296. if self.is_strict() {
  297. let all = self.flags.iter()
  298. .filter(|tuple| tuple.1.is_none() && predicate(&tuple.0))
  299. .collect::<Vec<_>>();
  300. if all.len() < 2 { Ok(all.first().map(|t| &t.0)) }
  301. else { Err(Misfire::Duplicate(all[0].0.clone(), all[1].0.clone())) }
  302. }
  303. else {
  304. let any = self.flags.iter().rev()
  305. .find(|tuple| tuple.1.is_none() && predicate(&tuple.0))
  306. .map(|tuple| &tuple.0);
  307. Ok(any)
  308. }
  309. }
  310. // This code could probably be better.
  311. // Both ā€˜has’ and ā€˜get’ immediately begin with a conditional, which makes
  312. // me think the functionality could be moved to inside Strictness.
  313. /// Returns the value of the given argument if it was specified, nothing
  314. /// if it wasn’t, and an error in strict mode if it was specified more
  315. /// than once.
  316. pub fn get(&self, arg: &'static Arg) -> Result<Option<&OsStr>, Misfire> {
  317. self.get_where(|flag| flag.matches(arg))
  318. }
  319. /// Returns the value of the argument that matches the predicate if it
  320. /// was specified, nothing if it wasn’t, and an error in strict mode if
  321. /// multiple arguments matched the predicate.
  322. ///
  323. /// It’s not possible to tell which flag the value belonged to from this.
  324. pub fn get_where<P>(&self, predicate: P) -> Result<Option<&OsStr>, Misfire>
  325. where P: Fn(&Flag) -> bool {
  326. if self.is_strict() {
  327. let those = self.flags.iter()
  328. .filter(|tuple| tuple.1.is_some() && predicate(&tuple.0))
  329. .collect::<Vec<_>>();
  330. if those.len() < 2 { Ok(those.first().cloned().map(|t| t.1.unwrap())) }
  331. else { Err(Misfire::Duplicate(those[0].0.clone(), those[1].0.clone())) }
  332. }
  333. else {
  334. let found = self.flags.iter().rev()
  335. .find(|tuple| tuple.1.is_some() && predicate(&tuple.0))
  336. .map(|tuple| tuple.1.unwrap());
  337. Ok(found)
  338. }
  339. }
  340. // It’s annoying that ā€˜has’ and ā€˜get’ won’t work when accidentally given
  341. // flags that do/don’t take values, but this should be caught by tests.
  342. /// Counts the number of occurrences of the given argument, even in
  343. /// strict mode.
  344. pub fn count(&self, arg: &Arg) -> usize {
  345. self.flags.iter()
  346. .filter(|tuple| tuple.0.matches(arg))
  347. .count()
  348. }
  349. /// Checks whether strict mode is on. This is usually done from within
  350. /// ā€˜has’ and ā€˜get’, but it’s available in an emergency.
  351. pub fn is_strict(&self) -> bool {
  352. self.strictness == Strictness::ComplainAboutRedundantArguments
  353. }
  354. }
  355. /// A problem with the user’s input that meant it couldn’t be parsed into a
  356. /// coherent list of arguments.
  357. #[derive(PartialEq, Debug)]
  358. pub enum ParseError {
  359. /// A flag that has to take a value was not given one.
  360. NeedsValue { flag: Flag, values: Option<Values> },
  361. /// A flag that can’t take a value *was* given one.
  362. ForbiddenValue { flag: Flag },
  363. /// A short argument, either alone or in a cluster, was not
  364. /// recognised by the program.
  365. UnknownShortArgument { attempt: ShortArg },
  366. /// A long argument was not recognised by the program.
  367. /// We don’t have a known &str version of the flag, so
  368. /// this may not be valid UTF-8.
  369. UnknownArgument { attempt: OsString },
  370. }
  371. // It’s technically possible for ParseError::UnknownArgument to borrow its
  372. // OsStr rather than owning it, but that would give ParseError a lifetime,
  373. // which would give Misfire a lifetime, which gets used everywhere. And this
  374. // only happens when an error occurs, so it’s not really worth it.
  375. /// Splits a string on its `=` character, returning the two substrings on
  376. /// either side. Returns `None` if there’s no equals or a string is missing.
  377. fn split_on_equals(input: &OsStr) -> Option<(&OsStr, &OsStr)> {
  378. use std::os::unix::ffi::OsStrExt;
  379. if let Some(index) = input.as_bytes().iter().position(|elem| *elem == b'=') {
  380. let (before, after) = input.as_bytes().split_at(index);
  381. // The after string contains the = that we need to remove.
  382. if !before.is_empty() && after.len() >= 2 {
  383. return Some((OsStr::from_bytes(before),
  384. OsStr::from_bytes(&after[1..])))
  385. }
  386. }
  387. None
  388. }
  389. /// Creates an `OSString` (used in tests)
  390. #[cfg(test)]
  391. fn os(input: &'static str) -> OsString {
  392. let mut os = OsString::new();
  393. os.push(input);
  394. os
  395. }
  396. #[cfg(test)]
  397. mod split_test {
  398. use super::{split_on_equals, os};
  399. macro_rules! test_split {
  400. ($name:ident: $input:expr => None) => {
  401. #[test]
  402. fn $name() {
  403. assert_eq!(split_on_equals(&os($input)),
  404. None);
  405. }
  406. };
  407. ($name:ident: $input:expr => $before:expr, $after:expr) => {
  408. #[test]
  409. fn $name() {
  410. assert_eq!(split_on_equals(&os($input)),
  411. Some((&*os($before), &*os($after))));
  412. }
  413. };
  414. }
  415. test_split!(empty: "" => None);
  416. test_split!(letter: "a" => None);
  417. test_split!(just: "=" => None);
  418. test_split!(intro: "=bbb" => None);
  419. test_split!(denou: "aaa=" => None);
  420. test_split!(equals: "aaa=bbb" => "aaa", "bbb");
  421. test_split!(sort: "--sort=size" => "--sort", "size");
  422. test_split!(more: "this=that=other" => "this", "that=other");
  423. }
  424. #[cfg(test)]
  425. mod parse_test {
  426. use super::*;
  427. pub fn os(input: &'static str) -> OsString {
  428. let mut os = OsString::new();
  429. os.push(input);
  430. os
  431. }
  432. macro_rules! test {
  433. ($name:ident: $inputs:expr => frees: $frees:expr, flags: $flags:expr) => {
  434. #[test]
  435. fn $name() {
  436. // Annoyingly the input &strs need to be converted to OsStrings
  437. let inputs: Vec<OsString> = $inputs.as_ref().into_iter().map(|&o| os(o)).collect();
  438. // Same with the frees
  439. let frees: Vec<OsString> = $frees.as_ref().into_iter().map(|&o| os(o)).collect();
  440. let frees: Vec<&OsStr> = frees.iter().map(|os| os.as_os_str()).collect();
  441. let flags = <[_]>::into_vec(Box::new($flags));
  442. let strictness = Strictness::UseLastArguments; // this isn’t even used
  443. let got = Args(TEST_ARGS).parse(inputs.iter(), strictness);
  444. let expected = Ok(Matches { frees, flags: MatchedFlags { flags, strictness } });
  445. assert_eq!(got, expected);
  446. }
  447. };
  448. ($name:ident: $inputs:expr => error $error:expr) => {
  449. #[test]
  450. fn $name() {
  451. use self::ParseError::*;
  452. let strictness = Strictness::UseLastArguments; // this isn’t even used
  453. let bits = $inputs.as_ref().into_iter().map(|&o| os(o)).collect::<Vec<OsString>>();
  454. let got = Args(TEST_ARGS).parse(bits.iter(), strictness);
  455. assert_eq!(got, Err($error));
  456. }
  457. };
  458. }
  459. const SUGGESTIONS: Values = &[ "example" ];
  460. static TEST_ARGS: &[&Arg] = &[
  461. &Arg { short: Some(b'l'), long: "long", takes_value: TakesValue::Forbidden },
  462. &Arg { short: Some(b'v'), long: "verbose", takes_value: TakesValue::Forbidden },
  463. &Arg { short: Some(b'c'), long: "count", takes_value: TakesValue::Necessary(None) },
  464. &Arg { short: Some(b't'), long: "type", takes_value: TakesValue::Necessary(Some(SUGGESTIONS)) }
  465. ];
  466. // Just filenames
  467. test!(empty: [] => frees: [], flags: []);
  468. test!(one_arg: ["exa"] => frees: [ "exa" ], flags: []);
  469. // Dashes and double dashes
  470. test!(one_dash: ["-"] => frees: [ "-" ], flags: []);
  471. test!(two_dashes: ["--"] => frees: [], flags: []);
  472. test!(two_file: ["--", "file"] => frees: [ "file" ], flags: []);
  473. test!(two_arg_l: ["--", "--long"] => frees: [ "--long" ], flags: []);
  474. test!(two_arg_s: ["--", "-l"] => frees: [ "-l" ], flags: []);
  475. // Long args
  476. test!(long: ["--long"] => frees: [], flags: [ (Flag::Long("long"), None) ]);
  477. test!(long_then: ["--long", "4"] => frees: [ "4" ], flags: [ (Flag::Long("long"), None) ]);
  478. test!(long_two: ["--long", "--verbose"] => frees: [], flags: [ (Flag::Long("long"), None), (Flag::Long("verbose"), None) ]);
  479. // Long args with values
  480. test!(bad_equals: ["--long=equals"] => error ForbiddenValue { flag: Flag::Long("long") });
  481. test!(no_arg: ["--count"] => error NeedsValue { flag: Flag::Long("count"), values: None });
  482. test!(arg_equals: ["--count=4"] => frees: [], flags: [ (Flag::Long("count"), Some(OsStr::new("4"))) ]);
  483. test!(arg_then: ["--count", "4"] => frees: [], flags: [ (Flag::Long("count"), Some(OsStr::new("4"))) ]);
  484. // Long args with values and suggestions
  485. test!(no_arg_s: ["--type"] => error NeedsValue { flag: Flag::Long("type"), values: Some(SUGGESTIONS) });
  486. test!(arg_equals_s: ["--type=exa"] => frees: [], flags: [ (Flag::Long("type"), Some(OsStr::new("exa"))) ]);
  487. test!(arg_then_s: ["--type", "exa"] => frees: [], flags: [ (Flag::Long("type"), Some(OsStr::new("exa"))) ]);
  488. // Short args
  489. test!(short: ["-l"] => frees: [], flags: [ (Flag::Short(b'l'), None) ]);
  490. test!(short_then: ["-l", "4"] => frees: [ "4" ], flags: [ (Flag::Short(b'l'), None) ]);
  491. test!(short_two: ["-lv"] => frees: [], flags: [ (Flag::Short(b'l'), None), (Flag::Short(b'v'), None) ]);
  492. test!(mixed: ["-v", "--long"] => frees: [], flags: [ (Flag::Short(b'v'), None), (Flag::Long("long"), None) ]);
  493. // Short args with values
  494. test!(bad_short: ["-l=equals"] => error ForbiddenValue { flag: Flag::Short(b'l') });
  495. test!(short_none: ["-c"] => error NeedsValue { flag: Flag::Short(b'c'), values: None });
  496. test!(short_arg_eq: ["-c=4"] => frees: [], flags: [(Flag::Short(b'c'), Some(OsStr::new("4"))) ]);
  497. test!(short_arg_then: ["-c", "4"] => frees: [], flags: [(Flag::Short(b'c'), Some(OsStr::new("4"))) ]);
  498. test!(short_two_together: ["-lctwo"] => frees: [], flags: [(Flag::Short(b'l'), None), (Flag::Short(b'c'), Some(OsStr::new("two"))) ]);
  499. test!(short_two_equals: ["-lc=two"] => frees: [], flags: [(Flag::Short(b'l'), None), (Flag::Short(b'c'), Some(OsStr::new("two"))) ]);
  500. test!(short_two_next: ["-lc", "two"] => frees: [], flags: [(Flag::Short(b'l'), None), (Flag::Short(b'c'), Some(OsStr::new("two"))) ]);
  501. // Short args with values and suggestions
  502. test!(short_none_s: ["-t"] => error NeedsValue { flag: Flag::Short(b't'), values: Some(SUGGESTIONS) });
  503. test!(short_two_together_s: ["-texa"] => frees: [], flags: [(Flag::Short(b't'), Some(OsStr::new("exa"))) ]);
  504. test!(short_two_equals_s: ["-t=exa"] => frees: [], flags: [(Flag::Short(b't'), Some(OsStr::new("exa"))) ]);
  505. test!(short_two_next_s: ["-t", "exa"] => frees: [], flags: [(Flag::Short(b't'), Some(OsStr::new("exa"))) ]);
  506. // Unknown args
  507. test!(unknown_long: ["--quiet"] => error UnknownArgument { attempt: os("quiet") });
  508. test!(unknown_long_eq: ["--quiet=shhh"] => error UnknownArgument { attempt: os("quiet") });
  509. test!(unknown_short: ["-q"] => error UnknownShortArgument { attempt: b'q' });
  510. test!(unknown_short_2nd: ["-lq"] => error UnknownShortArgument { attempt: b'q' });
  511. test!(unknown_short_eq: ["-q=shhh"] => error UnknownShortArgument { attempt: b'q' });
  512. test!(unknown_short_2nd_eq: ["-lq=shhh"] => error UnknownShortArgument { attempt: b'q' });
  513. }
  514. #[cfg(test)]
  515. mod matches_test {
  516. use super::*;
  517. macro_rules! test {
  518. ($name:ident: $input:expr, has $param:expr => $result:expr) => {
  519. #[test]
  520. fn $name() {
  521. let flags = MatchedFlags {
  522. flags: $input.to_vec(),
  523. strictness: Strictness::UseLastArguments,
  524. };
  525. assert_eq!(flags.has(&$param), Ok($result));
  526. }
  527. };
  528. }
  529. static VERBOSE: Arg = Arg { short: Some(b'v'), long: "verbose", takes_value: TakesValue::Forbidden };
  530. static COUNT: Arg = Arg { short: Some(b'c'), long: "count", takes_value: TakesValue::Necessary(None) };
  531. test!(short_never: [], has VERBOSE => false);
  532. test!(short_once: [(Flag::Short(b'v'), None)], has VERBOSE => true);
  533. test!(short_twice: [(Flag::Short(b'v'), None), (Flag::Short(b'v'), None)], has VERBOSE => true);
  534. test!(long_once: [(Flag::Long("verbose"), None)], has VERBOSE => true);
  535. test!(long_twice: [(Flag::Long("verbose"), None), (Flag::Long("verbose"), None)], has VERBOSE => true);
  536. test!(long_mixed: [(Flag::Long("verbose"), None), (Flag::Short(b'v'), None)], has VERBOSE => true);
  537. #[test]
  538. fn only_count() {
  539. let everything = os("everything");
  540. let flags = MatchedFlags {
  541. flags: vec![ (Flag::Short(b'c'), Some(&*everything)) ],
  542. strictness: Strictness::UseLastArguments,
  543. };
  544. assert_eq!(flags.get(&COUNT), Ok(Some(&*everything)));
  545. }
  546. #[test]
  547. fn rightmost_count() {
  548. let everything = os("everything");
  549. let nothing = os("nothing");
  550. let flags = MatchedFlags {
  551. flags: vec![ (Flag::Short(b'c'), Some(&*everything)),
  552. (Flag::Short(b'c'), Some(&*nothing)) ],
  553. strictness: Strictness::UseLastArguments,
  554. };
  555. assert_eq!(flags.get(&COUNT), Ok(Some(&*nothing)));
  556. }
  557. #[test]
  558. fn no_count() {
  559. let flags = MatchedFlags { flags: Vec::new(), strictness: Strictness::UseLastArguments };
  560. assert!(!flags.has(&COUNT).unwrap());
  561. }
  562. }