help.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. use std::fmt;
  2. use options::flags;
  3. use options::parser::MatchedFlags;
  4. use fs::feature::xattr;
  5. static OPTIONS: &str = r##"
  6. -?, --help show list of command-line options
  7. -v, --version show version of exa
  8. DISPLAY OPTIONS
  9. -1, --oneline display one entry per line
  10. -l, --long display extended file metadata as a table
  11. -G, --grid display entries as a grid (default)
  12. -x, --across sort the grid across, rather than downwards
  13. -R, --recurse recurse into directories
  14. -T, --tree recurse into directories as a tree
  15. -F, --classify display type indicator by file names
  16. --colo[u]r=WHEN when to use terminal colours (always, auto, never)
  17. --colo[u]r-scale highlight levels of file sizes distinctly
  18. FILTERING AND SORTING OPTIONS
  19. -a, --all show hidden and 'dot' files
  20. -d, --list-dirs list directories like regular files
  21. -L, --level DEPTH limit the depth of recursion
  22. -r, --reverse reverse the sort order
  23. -s, --sort SORT_FIELD which field to sort by
  24. --group-directories-first list directories before other files
  25. -D, --only-dirs list only directories
  26. -I, --ignore-glob GLOBS glob patterns (pipe-separated) of files to ignore
  27. --git-ignore Ignore files mentioned in '.gitignore'
  28. Valid sort fields: name, Name, extension, Extension, size, type,
  29. modified, accessed, created, inode, and none.
  30. date, time, old, and new all refer to modified.
  31. "##;
  32. static LONG_OPTIONS: &str = r##"
  33. LONG VIEW OPTIONS
  34. -b, --binary list file sizes with binary prefixes
  35. -B, --bytes list file sizes in bytes, without any prefixes
  36. -g, --group list each file's group
  37. -h, --header add a header row to each column
  38. -H, --links list each file's number of hard links
  39. -i, --inode list each file's inode number
  40. -m, --modified use the modified timestamp field
  41. -S, --blocks show number of file system blocks
  42. -t, --time FIELD which timestamp field to list (modified, accessed, created)
  43. -u, --accessed use the accessed timestamp field
  44. -U, --created use the created timestamp field
  45. --time-style how to format timestamps (default, iso, long-iso, full-iso)"##;
  46. static GIT_HELP: &str = r##" --git list each file's Git status, if tracked"##;
  47. static EXTENDED_HELP: &str = r##" -@, --extended list each file's extended attributes and sizes"##;
  48. /// All the information needed to display the help text, which depends
  49. /// on which features are enabled and whether the user only wants to
  50. /// see one section’s help.
  51. #[derive(PartialEq, Debug)]
  52. pub struct HelpString {
  53. /// Only show the help for the long section, not all the help.
  54. only_long: bool,
  55. /// Whether the --git option should be included in the help.
  56. git: bool,
  57. /// Whether the --extended option should be included in the help.
  58. xattrs: bool,
  59. }
  60. impl HelpString {
  61. /// Determines how to show help, if at all, based on the user’s
  62. /// command-line arguments. This one works backwards from the other
  63. /// ‘deduce’ functions, returning Err if help needs to be shown.
  64. ///
  65. /// We don’t do any strict-mode error checking here: it’s OK to give
  66. /// the --help or --long flags more than once. Actually checking for
  67. /// errors when the user wants help is kind of petty!
  68. pub fn deduce(matches: &MatchedFlags) -> Result<(), HelpString> {
  69. if matches.count(&flags::HELP) > 0 {
  70. let only_long = matches.count(&flags::LONG) > 0;
  71. let git = cfg!(feature="git");
  72. let xattrs = xattr::ENABLED;
  73. Err(HelpString { only_long, git, xattrs })
  74. }
  75. else {
  76. Ok(()) // no help needs to be shown
  77. }
  78. }
  79. }
  80. impl fmt::Display for HelpString {
  81. /// Format this help options into an actual string of help
  82. /// text to be displayed to the user.
  83. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  84. writeln!(f, "Usage:\n exa [options] [files...]")?;
  85. if !self.only_long {
  86. write!(f, "{}", OPTIONS)?;
  87. }
  88. write!(f, "{}", LONG_OPTIONS)?;
  89. if self.git {
  90. write!(f, "\n{}", GIT_HELP)?;
  91. }
  92. if self.xattrs {
  93. write!(f, "\n{}", EXTENDED_HELP)?;
  94. }
  95. Ok(())
  96. }
  97. }
  98. #[cfg(test)]
  99. mod test {
  100. use options::Options;
  101. use std::ffi::OsString;
  102. fn os(input: &'static str) -> OsString {
  103. let mut os = OsString::new();
  104. os.push(input);
  105. os
  106. }
  107. #[test]
  108. fn help() {
  109. let args = [ os("--help") ];
  110. let opts = Options::parse(&args, &None);
  111. assert!(opts.is_err())
  112. }
  113. #[test]
  114. fn help_with_file() {
  115. let args = [ os("--help"), os("me") ];
  116. let opts = Options::parse(&args, &None);
  117. assert!(opts.is_err())
  118. }
  119. #[test]
  120. fn unhelpful() {
  121. let args = [];
  122. let opts = Options::parse(&args, &None);
  123. assert!(opts.is_ok()) // no help when --help isn’t passed
  124. }
  125. }