error.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. use std::ffi::OsString;
  2. use std::fmt;
  3. use std::num::ParseIntError;
  4. use crate::options::flags;
  5. use crate::options::parser::{Arg, Flag, ParseError};
  6. /// Something wrong with the combination of options the user has picked.
  7. #[derive(PartialEq, Eq, Debug)]
  8. pub enum OptionsError {
  9. /// There was an error (from `getopts`) parsing the arguments.
  10. Parse(ParseError),
  11. /// The user supplied an illegal choice to an Argument.
  12. BadArgument(&'static Arg, OsString),
  13. /// The user supplied a set of options that are unsupported
  14. Unsupported(String),
  15. /// An option was given twice or more in strict mode.
  16. Duplicate(Flag, Flag),
  17. /// Two options were given that conflict with one another.
  18. Conflict(&'static Arg, &'static Arg),
  19. /// An option was given that does nothing when another one either is or
  20. /// isn’t present.
  21. Useless(&'static Arg, bool, &'static Arg),
  22. /// An option was given that does nothing when either of two other options
  23. /// are not present.
  24. Useless2(&'static Arg, &'static Arg, &'static Arg),
  25. /// A very specific edge case where --tree can’t be used with --all twice.
  26. TreeAllAll,
  27. /// A numeric option was given that failed to be parsed as a number.
  28. FailedParse(String, NumberSource, ParseIntError),
  29. /// A glob ignore was given that failed to be parsed as a pattern.
  30. FailedGlobPattern(String),
  31. }
  32. /// The source of a string that failed to be parsed as a number.
  33. #[derive(PartialEq, Eq, Debug)]
  34. pub enum NumberSource {
  35. /// It came... from a command-line argument!
  36. Arg(&'static Arg),
  37. /// It came... from the environment!
  38. Env(&'static str),
  39. }
  40. impl From<glob::PatternError> for OptionsError {
  41. fn from(error: glob::PatternError) -> Self {
  42. Self::FailedGlobPattern(error.to_string())
  43. }
  44. }
  45. impl fmt::Display for NumberSource {
  46. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  47. match self {
  48. Self::Arg(arg) => write!(f, "option {}", arg),
  49. Self::Env(env) => write!(f, "environment variable {}", env),
  50. }
  51. }
  52. }
  53. impl fmt::Display for OptionsError {
  54. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  55. use crate::options::parser::TakesValue;
  56. match self {
  57. Self::BadArgument(arg, attempt) => {
  58. if let TakesValue::Necessary(Some(values)) = arg.takes_value {
  59. write!(f, "Option {} has no {:?} setting ({})", arg, attempt, Choices(values))
  60. }
  61. else {
  62. write!(f, "Option {} has no {:?} setting", arg, attempt)
  63. }
  64. }
  65. Self::Parse(e) => write!(f, "{}", e),
  66. Self::Unsupported(e) => write!(f, "{}", e),
  67. Self::Conflict(a, b) => write!(f, "Option {} conflicts with option {}", a, b),
  68. Self::Duplicate(a, b) if a == b => write!(f, "Flag {} was given twice", a),
  69. Self::Duplicate(a, b) => write!(f, "Flag {} conflicts with flag {}", a, b),
  70. Self::Useless(a, false, b) => write!(f, "Option {} is useless without option {}", a, b),
  71. Self::Useless(a, true, b) => write!(f, "Option {} is useless given option {}", a, b),
  72. Self::Useless2(a, b1, b2) => write!(f, "Option {} is useless without options {} or {}", a, b1, b2),
  73. Self::TreeAllAll => write!(f, "Option --tree is useless given --all --all"),
  74. Self::FailedParse(s, n, e) => write!(f, "Value {:?} not valid for {}: {}", s, n, e),
  75. Self::FailedGlobPattern(ref e) => write!(f, "Failed to parse glob pattern: {}", e),
  76. }
  77. }
  78. }
  79. impl OptionsError {
  80. /// Try to second-guess what the user was trying to do, depending on what
  81. /// went wrong.
  82. pub fn suggestion(&self) -> Option<&'static str> {
  83. // ‘ls -lt’ and ‘ls -ltr’ are common combinations
  84. match self {
  85. Self::BadArgument(time, r) if *time == &flags::TIME && r == "r" => {
  86. Some("To sort oldest files last, try \"--sort oldest\", or just \"-sold\"")
  87. }
  88. Self::Parse(ParseError::NeedsValue { ref flag, .. }) if *flag == Flag::Short(b't') => {
  89. Some("To sort newest files last, try \"--sort newest\", or just \"-snew\"")
  90. }
  91. _ => {
  92. None
  93. }
  94. }
  95. }
  96. }
  97. /// A list of legal choices for an argument-taking option.
  98. #[derive(PartialEq, Eq, Debug)]
  99. pub struct Choices(pub &'static [&'static str]);
  100. impl fmt::Display for Choices {
  101. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  102. write!(f, "choices: {}", self.0.join(", "))
  103. }
  104. }