mod.rs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //! Parsing command-line strings into exa options.
  2. //!
  3. //! This module imports exa’s configuration types, such as `View` (the details
  4. //! of displaying multiple files) and `DirAction` (what to do when encountering
  5. //! a directory), and implements `deduce` methods on them so they can be
  6. //! configured using command-line options.
  7. //!
  8. //!
  9. //! ## Useless and overridden options
  10. //!
  11. //! Let’s say exa was invoked with just one argument: `exa --inode`. The
  12. //! `--inode` option is used in the details view, where it adds the inode
  13. //! column to the output. But because the details view is *only* activated with
  14. //! the `--long` argument, adding `--inode` without it would not have any
  15. //! effect.
  16. //!
  17. //! For a long time, exa’s philosophy was that the user should be warned
  18. //! whenever they could be mistaken like this. If you tell exa to display the
  19. //! inode, and it *doesn’t* display the inode, isn’t that more annoying than
  20. //! having it throw an error back at you?
  21. //!
  22. //! However, this doesn’t take into account *configuration*. Say a user wants
  23. //! to configure exa so that it lists inodes in the details view, but otherwise
  24. //! functions normally. A common way to do this for command-line programs is to
  25. //! define a shell alias that specifies the details they want to use every
  26. //! time. For the inode column, the alias would be:
  27. //!
  28. //! `alias exa="exa --inode"`
  29. //!
  30. //! Using this alias means that although the inode column will be shown in the
  31. //! details view, you’re now *only* allowed to use the details view, as any
  32. //! other view type will result in an error. Oops!
  33. //!
  34. //! Another example is when an option is specified twice, such as `exa
  35. //! --sort=Name --sort=size`. Did the user change their mind about sorting, and
  36. //! accidentally specify the option twice?
  37. //!
  38. //! Again, exa rejected this case, throwing an error back to the user instead
  39. //! of trying to guess how they want their output sorted. And again, this
  40. //! doesn’t take into account aliases being used to set defaults. A user who
  41. //! wants their files to be sorted case-insensitively may configure their shell
  42. //! with the following:
  43. //!
  44. //! `alias exa="exa --sort=Name"`
  45. //!
  46. //! Just like the earlier example, the user now can’t use any other sort order,
  47. //! because exa refuses to guess which one they meant. It’s *more* annoying to
  48. //! have to go back and edit the command than if there were no error.
  49. //!
  50. //! Fortunately, there’s a heuristic for telling which options came from an
  51. //! alias and which came from the actual command-line: aliased options are
  52. //! nearer the beginning of the options array, and command-line options are
  53. //! nearer the end. This means that after the options have been parsed, exa
  54. //! needs to traverse them *backwards* to find the last-most-specified one.
  55. //!
  56. //! For example, invoking exa with `exa --sort=size` when that alias is present
  57. //! would result in a full command-line of:
  58. //!
  59. //! `exa --sort=Name --sort=size`
  60. //!
  61. //! `--sort=size` should override `--sort=Name` because it’s closer to the end
  62. //! of the arguments array. In fact, because there’s no way to tell where the
  63. //! arguments came from -- it’s just a heuristic -- this will still work even
  64. //! if no aliases are being used!
  65. //!
  66. //! Finally, this isn’t just useful when options could override each other.
  67. //! Creating an alias `exal=”exa --long --inode --header”` then invoking `exal
  68. //! --grid --long` shouldn’t complain about `--long` being given twice when
  69. //! it’s clear what the user wants.
  70. use std::ffi::{OsStr, OsString};
  71. use fs::dir_action::DirAction;
  72. use fs::filter::FileFilter;
  73. use output::{View, Mode, details, grid_details};
  74. mod colours;
  75. mod dir_action;
  76. mod filter;
  77. mod view;
  78. mod help;
  79. use self::help::HelpString;
  80. mod version;
  81. use self::version::VersionString;
  82. mod misfire;
  83. pub use self::misfire::Misfire;
  84. pub mod vars;
  85. pub use self::vars::Vars;
  86. mod parser;
  87. mod flags;
  88. use self::parser::MatchedFlags;
  89. /// These **options** represent a parsed, error-checked versions of the
  90. /// user’s command-line options.
  91. #[derive(Debug)]
  92. pub struct Options {
  93. /// The action to perform when encountering a directory rather than a
  94. /// regular file.
  95. pub dir_action: DirAction,
  96. /// How to sort and filter files before outputting them.
  97. pub filter: FileFilter,
  98. /// The type of output to use (lines, grid, or details).
  99. pub view: View,
  100. }
  101. impl Options {
  102. /// Parse the given iterator of command-line strings into an Options
  103. /// struct and a list of free filenames, using the environment variables
  104. /// for extra options.
  105. #[allow(unused_results)]
  106. pub fn parse<'args, I, V>(args: I, vars: &V) -> Result<(Options, Vec<&'args OsStr>), Misfire>
  107. where I: IntoIterator<Item=&'args OsString>,
  108. V: Vars {
  109. use options::parser::{Matches, Strictness};
  110. use options::vars;
  111. let strictness = match vars.get(vars::EXA_STRICT) {
  112. None => Strictness::UseLastArguments,
  113. Some(ref t) if t.is_empty() => Strictness::UseLastArguments,
  114. _ => Strictness::ComplainAboutRedundantArguments,
  115. };
  116. let Matches { flags, frees } = match flags::ALL_ARGS.parse(args, strictness) {
  117. Ok(m) => m,
  118. Err(e) => return Err(Misfire::InvalidOptions(e)),
  119. };
  120. HelpString::deduce(&flags).map_err(Misfire::Help)?;
  121. VersionString::deduce(&flags).map_err(Misfire::Version)?;
  122. let options = Options::deduce(&flags, vars)?;
  123. Ok((options, frees))
  124. }
  125. /// Whether the View specified in this set of options includes a Git
  126. /// status column. It’s only worth trying to discover a repository if the
  127. /// results will end up being displayed.
  128. pub fn should_scan_for_git(&self) -> bool {
  129. match self.view.mode {
  130. Mode::Details(details::Options { table: Some(ref table), .. }) |
  131. Mode::GridDetails(grid_details::Options { details: details::Options { table: Some(ref table), .. }, .. }) => table.extra_columns.git,
  132. _ => false,
  133. }
  134. }
  135. /// Determines the complete set of options based on the given command-line
  136. /// arguments, after they’ve been parsed.
  137. fn deduce<V: Vars>(matches: &MatchedFlags, vars: &V) -> Result<Options, Misfire> {
  138. let dir_action = DirAction::deduce(matches)?;
  139. let filter = FileFilter::deduce(matches)?;
  140. let view = View::deduce(matches, vars)?;
  141. Ok(Options { dir_action, view, filter })
  142. }
  143. }
  144. #[cfg(test)]
  145. pub mod test {
  146. use super::{Options, Misfire, flags};
  147. use options::parser::{Arg, MatchedFlags};
  148. use std::ffi::OsString;
  149. #[derive(PartialEq, Debug)]
  150. pub enum Strictnesses {
  151. Last,
  152. Complain,
  153. Both,
  154. }
  155. /// This function gets used by the other testing modules.
  156. /// It can run with one or both strictness values: if told to run with
  157. /// both, then both should resolve to the same result.
  158. ///
  159. /// It returns a vector with one or two elements in.
  160. /// These elements can then be tested with assert_eq or what have you.
  161. pub fn parse_for_test<T, F>(inputs: &[&str], args: &'static [&'static Arg], strictnesses: Strictnesses, get: F) -> Vec<T>
  162. where F: Fn(&MatchedFlags) -> T
  163. {
  164. use self::Strictnesses::*;
  165. use options::parser::{Args, Strictness};
  166. use std::ffi::OsString;
  167. let bits = inputs.into_iter().map(|&o| os(o)).collect::<Vec<OsString>>();
  168. let mut rezzies = Vec::new();
  169. if strictnesses == Last || strictnesses == Both {
  170. let results = Args(args).parse(bits.iter(), Strictness::UseLastArguments);
  171. rezzies.push(get(&results.unwrap().flags));
  172. }
  173. if strictnesses == Complain || strictnesses == Both {
  174. let results = Args(args).parse(bits.iter(), Strictness::ComplainAboutRedundantArguments);
  175. rezzies.push(get(&results.unwrap().flags));
  176. }
  177. rezzies
  178. }
  179. /// Creates an `OSStr` (used in tests)
  180. #[cfg(test)]
  181. fn os(input: &str) -> OsString {
  182. let mut os = OsString::new();
  183. os.push(input);
  184. os
  185. }
  186. #[test]
  187. fn files() {
  188. let args = [ os("this file"), os("that file") ];
  189. let outs = Options::parse(&args, &None).unwrap().1;
  190. assert_eq!(outs, vec![ &os("this file"), &os("that file") ])
  191. }
  192. #[test]
  193. fn no_args() {
  194. let nothing: Vec<OsString> = Vec::new();
  195. let outs = Options::parse(&nothing, &None).unwrap().1;
  196. assert!(outs.is_empty()); // Listing the `.` directory is done in main.rs
  197. }
  198. #[test]
  199. fn long_across() {
  200. let args = [ os("--long"), os("--across") ];
  201. let opts = Options::parse(&args, &None);
  202. assert_eq!(opts.unwrap_err(), Misfire::Useless(&flags::ACROSS, true, &flags::LONG))
  203. }
  204. #[test]
  205. fn oneline_across() {
  206. let args = [ os("--oneline"), os("--across") ];
  207. let opts = Options::parse(&args, &None);
  208. assert_eq!(opts.unwrap_err(), Misfire::Useless(&flags::ACROSS, true, &flags::ONE_LINE))
  209. }
  210. }