PageRenderTime 53ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/crates/core/app.rs

https://github.com/BurntSushi/ripgrep
Rust | 3029 lines | 2636 code | 171 blank | 222 comment | 13 complexity | 8e00978dbda7f431c397449444e4035a MD5 | raw file
Possible License(s): MIT, Unlicense

Large files files are truncated, but you can click here to view the full file

  1. // This module defines the set of command line arguments that ripgrep supports,
  2. // including some light validation.
  3. //
  4. // This module is purposely written in a bare-bones way, since it is included
  5. // in ripgrep's build.rs file as a way to generate a man page and completion
  6. // files for common shells.
  7. //
  8. // The only other place that ripgrep deals with clap is in src/args.rs, which
  9. // is where we read clap's configuration from the end user's arguments and turn
  10. // it into a ripgrep-specific configuration type that is not coupled with clap.
  11. use clap::{self, crate_authors, crate_version, App, AppSettings};
  12. use lazy_static::lazy_static;
  13. const ABOUT: &str = "
  14. ripgrep (rg) recursively searches your current directory for a regex pattern.
  15. By default, ripgrep will respect your .gitignore and automatically skip hidden
  16. files/directories and binary files.
  17. Use -h for short descriptions and --help for more details.
  18. Project home page: https://github.com/BurntSushi/ripgrep
  19. ";
  20. const USAGE: &str = "
  21. rg [OPTIONS] PATTERN [PATH ...]
  22. rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...]
  23. rg [OPTIONS] --files [PATH ...]
  24. rg [OPTIONS] --type-list
  25. command | rg [OPTIONS] PATTERN";
  26. const TEMPLATE: &str = "\
  27. {bin} {version}
  28. {author}
  29. {about}
  30. USAGE:{usage}
  31. ARGS:
  32. {positionals}
  33. OPTIONS:
  34. {unified}";
  35. /// Build a clap application parameterized by usage strings.
  36. pub fn app() -> App<'static, 'static> {
  37. // We need to specify our version in a static because we've painted clap
  38. // into a corner. We've told it that every string we give it will be
  39. // 'static, but we need to build the version string dynamically. We can
  40. // fake the 'static lifetime with lazy_static.
  41. lazy_static! {
  42. static ref LONG_VERSION: String = long_version(None, true);
  43. }
  44. let mut app = App::new("ripgrep")
  45. .author(crate_authors!())
  46. .version(crate_version!())
  47. .long_version(LONG_VERSION.as_str())
  48. .about(ABOUT)
  49. .max_term_width(100)
  50. .setting(AppSettings::UnifiedHelpMessage)
  51. .setting(AppSettings::AllArgsOverrideSelf)
  52. .usage(USAGE)
  53. .template(TEMPLATE)
  54. .help_message("Prints help information. Use --help for more details.");
  55. for arg in all_args_and_flags() {
  56. app = app.arg(arg.claparg);
  57. }
  58. app
  59. }
  60. /// Return the "long" format of ripgrep's version string.
  61. ///
  62. /// If a revision hash is given, then it is used. If one isn't given, then
  63. /// the RIPGREP_BUILD_GIT_HASH env var is inspected for it. If that isn't set,
  64. /// then a revision hash is not included in the version string returned.
  65. ///
  66. /// If `cpu` is true, then the version string will include the compiled and
  67. /// runtime CPU features.
  68. pub fn long_version(revision_hash: Option<&str>, cpu: bool) -> String {
  69. // Do we have a git hash?
  70. // (Yes, if ripgrep was built on a machine with `git` installed.)
  71. let hash = match revision_hash.or(option_env!("RIPGREP_BUILD_GIT_HASH")) {
  72. None => String::new(),
  73. Some(githash) => format!(" (rev {})", githash),
  74. };
  75. if !cpu {
  76. format!("{}{}", crate_version!(), hash,)
  77. } else {
  78. let runtime = runtime_cpu_features();
  79. if runtime.is_empty() {
  80. format!(
  81. "{}{}\n{} (compiled)",
  82. crate_version!(),
  83. hash,
  84. compile_cpu_features().join(" ")
  85. )
  86. } else {
  87. format!(
  88. "{}{}\n{} (compiled)\n{} (runtime)",
  89. crate_version!(),
  90. hash,
  91. compile_cpu_features().join(" "),
  92. runtime.join(" ")
  93. )
  94. }
  95. }
  96. }
  97. /// Returns the relevant CPU features enabled at compile time.
  98. fn compile_cpu_features() -> Vec<&'static str> {
  99. let mut features = vec![];
  100. if cfg!(feature = "simd-accel") {
  101. features.push("+SIMD");
  102. } else {
  103. features.push("-SIMD");
  104. }
  105. if cfg!(feature = "avx-accel") {
  106. features.push("+AVX");
  107. } else {
  108. features.push("-AVX");
  109. }
  110. features
  111. }
  112. /// Returns the relevant CPU features enabled at runtime.
  113. #[cfg(target_arch = "x86_64")]
  114. fn runtime_cpu_features() -> Vec<&'static str> {
  115. // This is kind of a dirty violation of abstraction, since it assumes
  116. // knowledge about what specific SIMD features are being used.
  117. let mut features = vec![];
  118. if is_x86_feature_detected!("ssse3") {
  119. features.push("+SIMD");
  120. } else {
  121. features.push("-SIMD");
  122. }
  123. if is_x86_feature_detected!("avx2") {
  124. features.push("+AVX");
  125. } else {
  126. features.push("-AVX");
  127. }
  128. features
  129. }
  130. /// Returns the relevant CPU features enabled at runtime.
  131. #[cfg(not(target_arch = "x86_64"))]
  132. fn runtime_cpu_features() -> Vec<&'static str> {
  133. vec![]
  134. }
  135. /// Arg is a light alias for a clap::Arg that is specialized to compile time
  136. /// string literals.
  137. type Arg = clap::Arg<'static, 'static>;
  138. /// RGArg is a light wrapper around a clap::Arg and also contains some metadata
  139. /// about the underlying Arg so that it can be inspected for other purposes
  140. /// (e.g., hopefully generating a man page).
  141. ///
  142. /// Note that this type is purposely overly constrained to ripgrep's particular
  143. /// use of clap.
  144. #[allow(dead_code)]
  145. #[derive(Clone)]
  146. pub struct RGArg {
  147. /// The underlying clap argument.
  148. claparg: Arg,
  149. /// The name of this argument. This is always present and is the name
  150. /// used in the code to find the value of an argument at runtime.
  151. pub name: &'static str,
  152. /// A short documentation string describing this argument. This string
  153. /// should fit on a single line and be a complete sentence.
  154. ///
  155. /// This is shown in the `-h` output.
  156. pub doc_short: &'static str,
  157. /// A longer documentation string describing this argument. This usually
  158. /// starts with the contents of `doc_short`. This is also usually many
  159. /// lines, potentially paragraphs, and may contain examples and additional
  160. /// prose.
  161. ///
  162. /// This is shown in the `--help` output.
  163. pub doc_long: &'static str,
  164. /// Whether this flag is hidden or not.
  165. ///
  166. /// This is typically used for uncommon flags that only serve to override
  167. /// other flags. For example, --no-ignore is a prominent flag that disables
  168. /// ripgrep's gitignore functionality, but --ignore re-enables it. Since
  169. /// gitignore support is enabled by default, use of the --ignore flag is
  170. /// somewhat niche and relegated to special cases when users make use of
  171. /// configuration files to set defaults.
  172. ///
  173. /// Generally, these flags should be documented in the documentation for
  174. /// the flag they override.
  175. pub hidden: bool,
  176. /// The type of this argument.
  177. pub kind: RGArgKind,
  178. }
  179. /// The kind of a ripgrep argument.
  180. ///
  181. /// This can be one of three possibilities: a positional argument, a boolean
  182. /// switch flag or a flag that accepts exactly one argument. Each variant
  183. /// stores argument type specific data.
  184. ///
  185. /// Note that clap supports more types of arguments than this, but we don't
  186. /// (and probably shouldn't) use them in ripgrep.
  187. ///
  188. /// Finally, note that we don't capture *all* state about an argument in this
  189. /// type. Some state is only known to clap. There isn't any particular reason
  190. /// why; the state we do capture is motivated by use cases (like generating
  191. /// documentation).
  192. #[derive(Clone)]
  193. pub enum RGArgKind {
  194. /// A positional argument.
  195. Positional {
  196. /// The name of the value used in the `-h/--help` output. By
  197. /// convention, this is an all-uppercase string. e.g., `PATH` or
  198. /// `PATTERN`.
  199. value_name: &'static str,
  200. /// Whether an argument can be repeated multiple times or not.
  201. ///
  202. /// The only argument this applies to is PATH, where an end user can
  203. /// specify multiple paths for ripgrep to search.
  204. ///
  205. /// If this is disabled, then an argument can only be provided once.
  206. /// For example, PATTERN is one such argument. (Note that the
  207. /// -e/--regexp flag is distinct from the positional PATTERN argument,
  208. /// and it can be provided multiple times.)
  209. multiple: bool,
  210. },
  211. /// A boolean switch.
  212. Switch {
  213. /// The long name of a flag. This is always non-empty.
  214. long: &'static str,
  215. /// The short name of a flag. This is empty if a flag only has a long
  216. /// name.
  217. short: Option<&'static str>,
  218. /// Whether this switch can be provided multiple times where meaning
  219. /// is attached to the number of times this flag is given.
  220. ///
  221. /// Note that every switch can be provided multiple times. This
  222. /// particular state indicates whether all instances of a switch are
  223. /// relevant or not.
  224. ///
  225. /// For example, the -u/--unrestricted flag can be provided multiple
  226. /// times where each repeated use of it indicates more relaxing of
  227. /// ripgrep's filtering. Conversely, the -i/--ignore-case flag can
  228. /// also be provided multiple times, but it is simply considered either
  229. /// present or not. In these cases, -u/--unrestricted has `multiple`
  230. /// set to `true` while -i/--ignore-case has `multiple` set to `false`.
  231. multiple: bool,
  232. },
  233. /// A flag the accepts a single value.
  234. Flag {
  235. /// The long name of a flag. This is always non-empty.
  236. long: &'static str,
  237. /// The short name of a flag. This is empty if a flag only has a long
  238. /// name.
  239. short: Option<&'static str>,
  240. /// The name of the value used in the `-h/--help` output. By
  241. /// convention, this is an all-uppercase string. e.g., `PATH` or
  242. /// `PATTERN`.
  243. value_name: &'static str,
  244. /// Whether this flag can be provided multiple times with multiple
  245. /// distinct values.
  246. ///
  247. /// Note that every flag can be provided multiple times. This
  248. /// particular state indicates whether all instances of a flag are
  249. /// relevant or not.
  250. ///
  251. /// For example, the -g/--glob flag can be provided multiple times and
  252. /// all of its values should be interpreted by ripgrep. Conversely,
  253. /// while the -C/--context flag can also be provided multiple times,
  254. /// only its last instance is used while all previous instances are
  255. /// ignored. In these cases, -g/--glob has `multiple` set to `true`
  256. /// while -C/--context has `multiple` set to `false`.
  257. multiple: bool,
  258. /// A set of possible values for this flag. If an end user provides
  259. /// any value other than what's in this set, then clap will report an
  260. /// error.
  261. possible_values: Vec<&'static str>,
  262. },
  263. }
  264. impl RGArg {
  265. /// Create a positional argument.
  266. ///
  267. /// The `long_name` parameter is the name of the argument, e.g., `pattern`.
  268. /// The `value_name` parameter is a name that describes the type of
  269. /// argument this flag accepts. It should be in uppercase, e.g., PATH or
  270. /// PATTERN.
  271. fn positional(name: &'static str, value_name: &'static str) -> RGArg {
  272. RGArg {
  273. claparg: Arg::with_name(name).value_name(value_name),
  274. name,
  275. doc_short: "",
  276. doc_long: "",
  277. hidden: false,
  278. kind: RGArgKind::Positional { value_name, multiple: false },
  279. }
  280. }
  281. /// Create a boolean switch.
  282. ///
  283. /// The `long_name` parameter is the name of the flag, e.g., `--long-name`.
  284. ///
  285. /// All switches may be repeated an arbitrary number of times. If a switch
  286. /// is truly boolean, that consumers of clap's configuration should only
  287. /// check whether the flag is present or not. Otherwise, consumers may
  288. /// inspect the number of times the switch is used.
  289. fn switch(long_name: &'static str) -> RGArg {
  290. let claparg = Arg::with_name(long_name).long(long_name);
  291. RGArg {
  292. claparg,
  293. name: long_name,
  294. doc_short: "",
  295. doc_long: "",
  296. hidden: false,
  297. kind: RGArgKind::Switch {
  298. long: long_name,
  299. short: None,
  300. multiple: false,
  301. },
  302. }
  303. }
  304. /// Create a flag. A flag always accepts exactly one argument.
  305. ///
  306. /// The `long_name` parameter is the name of the flag, e.g., `--long-name`.
  307. /// The `value_name` parameter is a name that describes the type of
  308. /// argument this flag accepts. It should be in uppercase, e.g., PATH or
  309. /// PATTERN.
  310. ///
  311. /// All flags may be repeated an arbitrary number of times. If a flag has
  312. /// only one logical value, that consumers of clap's configuration should
  313. /// only use the last value.
  314. fn flag(long_name: &'static str, value_name: &'static str) -> RGArg {
  315. let claparg = Arg::with_name(long_name)
  316. .long(long_name)
  317. .value_name(value_name)
  318. .takes_value(true)
  319. .number_of_values(1);
  320. RGArg {
  321. claparg,
  322. name: long_name,
  323. doc_short: "",
  324. doc_long: "",
  325. hidden: false,
  326. kind: RGArgKind::Flag {
  327. long: long_name,
  328. short: None,
  329. value_name,
  330. multiple: false,
  331. possible_values: vec![],
  332. },
  333. }
  334. }
  335. /// Set the short flag name.
  336. ///
  337. /// This panics if this arg isn't a switch or a flag.
  338. fn short(mut self, name: &'static str) -> RGArg {
  339. match self.kind {
  340. RGArgKind::Positional { .. } => panic!("expected switch or flag"),
  341. RGArgKind::Switch { ref mut short, .. } => {
  342. *short = Some(name);
  343. }
  344. RGArgKind::Flag { ref mut short, .. } => {
  345. *short = Some(name);
  346. }
  347. }
  348. self.claparg = self.claparg.short(name);
  349. self
  350. }
  351. /// Set the "short" help text.
  352. ///
  353. /// This should be a single line. It is shown in the `-h` output.
  354. fn help(mut self, text: &'static str) -> RGArg {
  355. self.doc_short = text;
  356. self.claparg = self.claparg.help(text);
  357. self
  358. }
  359. /// Set the "long" help text.
  360. ///
  361. /// This should be at least a single line, usually longer. It is shown in
  362. /// the `--help` output.
  363. fn long_help(mut self, text: &'static str) -> RGArg {
  364. self.doc_long = text;
  365. self.claparg = self.claparg.long_help(text);
  366. self
  367. }
  368. /// Enable this argument to accept multiple values.
  369. ///
  370. /// Note that while switches and flags can always be repeated an arbitrary
  371. /// number of times, this particular method enables the flag to be
  372. /// logically repeated where each occurrence of the flag may have
  373. /// significance. That is, when this is disabled, then a switch is either
  374. /// present or not and a flag has exactly one value (the last one given).
  375. /// When this is enabled, then a switch has a count corresponding to the
  376. /// number of times it is used and a flag's value is a list of all values
  377. /// given.
  378. ///
  379. /// For the most part, this distinction is resolved by consumers of clap's
  380. /// configuration.
  381. fn multiple(mut self) -> RGArg {
  382. // Why not put `multiple` on RGArg proper? Because it's useful to
  383. // document it distinct for each different kind. See RGArgKind docs.
  384. match self.kind {
  385. RGArgKind::Positional { ref mut multiple, .. } => {
  386. *multiple = true;
  387. }
  388. RGArgKind::Switch { ref mut multiple, .. } => {
  389. *multiple = true;
  390. }
  391. RGArgKind::Flag { ref mut multiple, .. } => {
  392. *multiple = true;
  393. }
  394. }
  395. self.claparg = self.claparg.multiple(true);
  396. self
  397. }
  398. /// Hide this flag from all documentation.
  399. fn hidden(mut self) -> RGArg {
  400. self.hidden = true;
  401. self.claparg = self.claparg.hidden(true);
  402. self
  403. }
  404. /// Set the possible values for this argument. If this argument is not
  405. /// a flag, then this panics.
  406. ///
  407. /// If the end user provides any value other than what is given here, then
  408. /// clap will report an error to the user.
  409. ///
  410. /// Note that this will suppress clap's automatic output of possible values
  411. /// when using -h/--help, so users of this method should provide
  412. /// appropriate documentation for the choices in the "long" help text.
  413. fn possible_values(mut self, values: &[&'static str]) -> RGArg {
  414. match self.kind {
  415. RGArgKind::Positional { .. } => panic!("expected flag"),
  416. RGArgKind::Switch { .. } => panic!("expected flag"),
  417. RGArgKind::Flag { ref mut possible_values, .. } => {
  418. *possible_values = values.to_vec();
  419. self.claparg = self
  420. .claparg
  421. .possible_values(values)
  422. .hide_possible_values(true);
  423. }
  424. }
  425. self
  426. }
  427. /// Add an alias to this argument.
  428. ///
  429. /// Aliases are not show in the output of -h/--help.
  430. fn alias(mut self, name: &'static str) -> RGArg {
  431. self.claparg = self.claparg.alias(name);
  432. self
  433. }
  434. /// Permit this flag to have values that begin with a hypen.
  435. ///
  436. /// This panics if this arg is not a flag.
  437. fn allow_leading_hyphen(mut self) -> RGArg {
  438. match self.kind {
  439. RGArgKind::Positional { .. } => panic!("expected flag"),
  440. RGArgKind::Switch { .. } => panic!("expected flag"),
  441. RGArgKind::Flag { .. } => {
  442. self.claparg = self.claparg.allow_hyphen_values(true);
  443. }
  444. }
  445. self
  446. }
  447. /// Sets this argument to a required argument, unless one of the given
  448. /// arguments is provided.
  449. fn required_unless(mut self, names: &[&'static str]) -> RGArg {
  450. self.claparg = self.claparg.required_unless_one(names);
  451. self
  452. }
  453. /// Sets conflicting arguments. That is, if this argument is used whenever
  454. /// any of the other arguments given here are used, then clap will report
  455. /// an error.
  456. fn conflicts(mut self, names: &[&'static str]) -> RGArg {
  457. self.claparg = self.claparg.conflicts_with_all(names);
  458. self
  459. }
  460. /// Sets an overriding argument. That is, if this argument and the given
  461. /// argument are both provided by an end user, then the "last" one will
  462. /// win. ripgrep will behave as if any previous instantiations did not
  463. /// happen.
  464. fn overrides(mut self, name: &'static str) -> RGArg {
  465. self.claparg = self.claparg.overrides_with(name);
  466. self
  467. }
  468. /// Sets the default value of this argument when not specified at
  469. /// runtime.
  470. fn default_value(mut self, value: &'static str) -> RGArg {
  471. self.claparg = self.claparg.default_value(value);
  472. self
  473. }
  474. /// Sets the default value of this argument if and only if the argument
  475. /// given is present.
  476. fn default_value_if(
  477. mut self,
  478. value: &'static str,
  479. arg_name: &'static str,
  480. ) -> RGArg {
  481. self.claparg = self.claparg.default_value_if(arg_name, None, value);
  482. self
  483. }
  484. /// Indicate that any value given to this argument should be a number. If
  485. /// it's not a number, then clap will report an error to the end user.
  486. fn number(mut self) -> RGArg {
  487. self.claparg = self.claparg.validator(|val| {
  488. val.parse::<usize>().map(|_| ()).map_err(|err| err.to_string())
  489. });
  490. self
  491. }
  492. }
  493. // We add an extra space to long descriptions so that a blank line is inserted
  494. // between flag descriptions in --help output.
  495. macro_rules! long {
  496. ($lit:expr) => {
  497. concat!($lit, " ")
  498. };
  499. }
  500. /// Generate a sequence of all positional and flag arguments.
  501. pub fn all_args_and_flags() -> Vec<RGArg> {
  502. let mut args = vec![];
  503. // The positional arguments must be defined first and in order.
  504. arg_pattern(&mut args);
  505. arg_path(&mut args);
  506. // Flags can be defined in any order, but we do it alphabetically. Note
  507. // that each function may define multiple flags. For example,
  508. // `flag_encoding` defines `--encoding` and `--no-encoding`. Most `--no`
  509. // flags are hidden and merely mentioned in the docs of the corresponding
  510. // "positive" flag.
  511. flag_after_context(&mut args);
  512. flag_auto_hybrid_regex(&mut args);
  513. flag_before_context(&mut args);
  514. flag_binary(&mut args);
  515. flag_block_buffered(&mut args);
  516. flag_byte_offset(&mut args);
  517. flag_case_sensitive(&mut args);
  518. flag_color(&mut args);
  519. flag_colors(&mut args);
  520. flag_column(&mut args);
  521. flag_context(&mut args);
  522. flag_context_separator(&mut args);
  523. flag_count(&mut args);
  524. flag_count_matches(&mut args);
  525. flag_crlf(&mut args);
  526. flag_debug(&mut args);
  527. flag_dfa_size_limit(&mut args);
  528. flag_encoding(&mut args);
  529. flag_engine(&mut args);
  530. flag_file(&mut args);
  531. flag_files(&mut args);
  532. flag_files_with_matches(&mut args);
  533. flag_files_without_match(&mut args);
  534. flag_fixed_strings(&mut args);
  535. flag_follow(&mut args);
  536. flag_glob(&mut args);
  537. flag_glob_case_insensitive(&mut args);
  538. flag_heading(&mut args);
  539. flag_hidden(&mut args);
  540. flag_iglob(&mut args);
  541. flag_ignore_case(&mut args);
  542. flag_ignore_file(&mut args);
  543. flag_ignore_file_case_insensitive(&mut args);
  544. flag_include_zero(&mut args);
  545. flag_invert_match(&mut args);
  546. flag_json(&mut args);
  547. flag_line_buffered(&mut args);
  548. flag_line_number(&mut args);
  549. flag_line_regexp(&mut args);
  550. flag_max_columns(&mut args);
  551. flag_max_columns_preview(&mut args);
  552. flag_max_count(&mut args);
  553. flag_max_depth(&mut args);
  554. flag_max_filesize(&mut args);
  555. flag_mmap(&mut args);
  556. flag_multiline(&mut args);
  557. flag_multiline_dotall(&mut args);
  558. flag_no_config(&mut args);
  559. flag_no_ignore(&mut args);
  560. flag_no_ignore_dot(&mut args);
  561. flag_no_ignore_exclude(&mut args);
  562. flag_no_ignore_files(&mut args);
  563. flag_no_ignore_global(&mut args);
  564. flag_no_ignore_messages(&mut args);
  565. flag_no_ignore_parent(&mut args);
  566. flag_no_ignore_vcs(&mut args);
  567. flag_no_messages(&mut args);
  568. flag_no_pcre2_unicode(&mut args);
  569. flag_no_require_git(&mut args);
  570. flag_no_unicode(&mut args);
  571. flag_null(&mut args);
  572. flag_null_data(&mut args);
  573. flag_one_file_system(&mut args);
  574. flag_only_matching(&mut args);
  575. flag_path_separator(&mut args);
  576. flag_passthru(&mut args);
  577. flag_pcre2(&mut args);
  578. flag_pcre2_version(&mut args);
  579. flag_pre(&mut args);
  580. flag_pre_glob(&mut args);
  581. flag_pretty(&mut args);
  582. flag_quiet(&mut args);
  583. flag_regex_size_limit(&mut args);
  584. flag_regexp(&mut args);
  585. flag_replace(&mut args);
  586. flag_search_zip(&mut args);
  587. flag_smart_case(&mut args);
  588. flag_sort_files(&mut args);
  589. flag_sort(&mut args);
  590. flag_sortr(&mut args);
  591. flag_stats(&mut args);
  592. flag_text(&mut args);
  593. flag_threads(&mut args);
  594. flag_trim(&mut args);
  595. flag_type(&mut args);
  596. flag_type_add(&mut args);
  597. flag_type_clear(&mut args);
  598. flag_type_list(&mut args);
  599. flag_type_not(&mut args);
  600. flag_unrestricted(&mut args);
  601. flag_vimgrep(&mut args);
  602. flag_with_filename(&mut args);
  603. flag_word_regexp(&mut args);
  604. args
  605. }
  606. fn arg_pattern(args: &mut Vec<RGArg>) {
  607. const SHORT: &str = "A regular expression used for searching.";
  608. const LONG: &str = long!(
  609. "\
  610. A regular expression used for searching. To match a pattern beginning with a
  611. dash, use the -e/--regexp flag.
  612. For example, to search for the literal '-foo', you can use this flag:
  613. rg -e -foo
  614. You can also use the special '--' delimiter to indicate that no more flags
  615. will be provided. Namely, the following is equivalent to the above:
  616. rg -- -foo
  617. "
  618. );
  619. let arg = RGArg::positional("pattern", "PATTERN")
  620. .help(SHORT)
  621. .long_help(LONG)
  622. .required_unless(&[
  623. "file",
  624. "files",
  625. "regexp",
  626. "type-list",
  627. "pcre2-version",
  628. ]);
  629. args.push(arg);
  630. }
  631. fn arg_path(args: &mut Vec<RGArg>) {
  632. const SHORT: &str = "A file or directory to search.";
  633. const LONG: &str = long!(
  634. "\
  635. A file or directory to search. Directories are searched recursively. File \
  636. paths specified on the command line override glob and ignore rules. \
  637. "
  638. );
  639. let arg = RGArg::positional("path", "PATH")
  640. .help(SHORT)
  641. .long_help(LONG)
  642. .multiple();
  643. args.push(arg);
  644. }
  645. fn flag_after_context(args: &mut Vec<RGArg>) {
  646. const SHORT: &str = "Show NUM lines after each match.";
  647. const LONG: &str = long!(
  648. "\
  649. Show NUM lines after each match.
  650. This overrides the --context flag.
  651. "
  652. );
  653. let arg = RGArg::flag("after-context", "NUM")
  654. .short("A")
  655. .help(SHORT)
  656. .long_help(LONG)
  657. .number()
  658. .overrides("context");
  659. args.push(arg);
  660. }
  661. fn flag_auto_hybrid_regex(args: &mut Vec<RGArg>) {
  662. const SHORT: &str = "Dynamically use PCRE2 if necessary.";
  663. const LONG: &str = long!(
  664. "\
  665. DEPRECATED. Use --engine instead.
  666. When this flag is used, ripgrep will dynamically choose between supported regex
  667. engines depending on the features used in a pattern. When ripgrep chooses a
  668. regex engine, it applies that choice for every regex provided to ripgrep (e.g.,
  669. via multiple -e/--regexp or -f/--file flags).
  670. As an example of how this flag might behave, ripgrep will attempt to use
  671. its default finite automata based regex engine whenever the pattern can be
  672. successfully compiled with that regex engine. If PCRE2 is enabled and if the
  673. pattern given could not be compiled with the default regex engine, then PCRE2
  674. will be automatically used for searching. If PCRE2 isn't available, then this
  675. flag has no effect because there is only one regex engine to choose from.
  676. In the future, ripgrep may adjust its heuristics for how it decides which
  677. regex engine to use. In general, the heuristics will be limited to a static
  678. analysis of the patterns, and not to any specific runtime behavior observed
  679. while searching files.
  680. The primary downside of using this flag is that it may not always be obvious
  681. which regex engine ripgrep uses, and thus, the match semantics or performance
  682. profile of ripgrep may subtly and unexpectedly change. However, in many cases,
  683. all regex engines will agree on what constitutes a match and it can be nice
  684. to transparently support more advanced regex features like look-around and
  685. backreferences without explicitly needing to enable them.
  686. This flag can be disabled with --no-auto-hybrid-regex.
  687. "
  688. );
  689. let arg = RGArg::switch("auto-hybrid-regex")
  690. .help(SHORT)
  691. .long_help(LONG)
  692. .overrides("no-auto-hybrid-regex")
  693. .overrides("pcre2")
  694. .overrides("no-pcre2")
  695. .overrides("engine");
  696. args.push(arg);
  697. let arg = RGArg::switch("no-auto-hybrid-regex")
  698. .hidden()
  699. .overrides("auto-hybrid-regex")
  700. .overrides("pcre2")
  701. .overrides("no-pcre2")
  702. .overrides("engine");
  703. args.push(arg);
  704. }
  705. fn flag_before_context(args: &mut Vec<RGArg>) {
  706. const SHORT: &str = "Show NUM lines before each match.";
  707. const LONG: &str = long!(
  708. "\
  709. Show NUM lines before each match.
  710. This overrides the --context flag.
  711. "
  712. );
  713. let arg = RGArg::flag("before-context", "NUM")
  714. .short("B")
  715. .help(SHORT)
  716. .long_help(LONG)
  717. .number()
  718. .overrides("context");
  719. args.push(arg);
  720. }
  721. fn flag_binary(args: &mut Vec<RGArg>) {
  722. const SHORT: &str = "Search binary files.";
  723. const LONG: &str = long!(
  724. "\
  725. Enabling this flag will cause ripgrep to search binary files. By default,
  726. ripgrep attempts to automatically skip binary files in order to improve the
  727. relevance of results and make the search faster.
  728. Binary files are heuristically detected based on whether they contain a NUL
  729. byte or not. By default (without this flag set), once a NUL byte is seen,
  730. ripgrep will stop searching the file. Usually, NUL bytes occur in the beginning
  731. of most binary files. If a NUL byte occurs after a match, then ripgrep will
  732. still stop searching the rest of the file, but a warning will be printed.
  733. In contrast, when this flag is provided, ripgrep will continue searching a file
  734. even if a NUL byte is found. In particular, if a NUL byte is found then ripgrep
  735. will continue searching until either a match is found or the end of the file is
  736. reached, whichever comes sooner. If a match is found, then ripgrep will stop
  737. and print a warning saying that the search stopped prematurely.
  738. If you want ripgrep to search a file without any special NUL byte handling at
  739. all (and potentially print binary data to stdout), then you should use the
  740. '-a/--text' flag.
  741. The '--binary' flag is a flag for controlling ripgrep's automatic filtering
  742. mechanism. As such, it does not need to be used when searching a file
  743. explicitly or when searching stdin. That is, it is only applicable when
  744. recursively searching a directory.
  745. Note that when the '-u/--unrestricted' flag is provided for a third time, then
  746. this flag is automatically enabled.
  747. This flag can be disabled with '--no-binary'. It overrides the '-a/--text'
  748. flag.
  749. "
  750. );
  751. let arg = RGArg::switch("binary")
  752. .help(SHORT)
  753. .long_help(LONG)
  754. .overrides("no-binary")
  755. .overrides("text")
  756. .overrides("no-text");
  757. args.push(arg);
  758. let arg = RGArg::switch("no-binary")
  759. .hidden()
  760. .overrides("binary")
  761. .overrides("text")
  762. .overrides("no-text");
  763. args.push(arg);
  764. }
  765. fn flag_block_buffered(args: &mut Vec<RGArg>) {
  766. const SHORT: &str = "Force block buffering.";
  767. const LONG: &str = long!(
  768. "\
  769. When enabled, ripgrep will use block buffering. That is, whenever a matching
  770. line is found, it will be written to an in-memory buffer and will not be
  771. written to stdout until the buffer reaches a certain size. This is the default
  772. when ripgrep's stdout is redirected to a pipeline or a file. When ripgrep's
  773. stdout is connected to a terminal, line buffering will be used. Forcing block
  774. buffering can be useful when dumping a large amount of contents to a terminal.
  775. Forceful block buffering can be disabled with --no-block-buffered. Note that
  776. using --no-block-buffered causes ripgrep to revert to its default behavior of
  777. automatically detecting the buffering strategy. To force line buffering, use
  778. the --line-buffered flag.
  779. "
  780. );
  781. let arg = RGArg::switch("block-buffered")
  782. .help(SHORT)
  783. .long_help(LONG)
  784. .overrides("no-block-buffered")
  785. .overrides("line-buffered")
  786. .overrides("no-line-buffered");
  787. args.push(arg);
  788. let arg = RGArg::switch("no-block-buffered")
  789. .hidden()
  790. .overrides("block-buffered")
  791. .overrides("line-buffered")
  792. .overrides("no-line-buffered");
  793. args.push(arg);
  794. }
  795. fn flag_byte_offset(args: &mut Vec<RGArg>) {
  796. const SHORT: &str =
  797. "Print the 0-based byte offset for each matching line.";
  798. const LONG: &str = long!(
  799. "\
  800. Print the 0-based byte offset within the input file before each line of output.
  801. If -o (--only-matching) is specified, print the offset of the matching part
  802. itself.
  803. If ripgrep does transcoding, then the byte offset is in terms of the the result
  804. of transcoding and not the original data. This applies similarly to another
  805. transformation on the source, such as decompression or a --pre filter. Note
  806. that when the PCRE2 regex engine is used, then UTF-8 transcoding is done by
  807. default.
  808. "
  809. );
  810. let arg =
  811. RGArg::switch("byte-offset").short("b").help(SHORT).long_help(LONG);
  812. args.push(arg);
  813. }
  814. fn flag_case_sensitive(args: &mut Vec<RGArg>) {
  815. const SHORT: &str = "Search case sensitively (default).";
  816. const LONG: &str = long!(
  817. "\
  818. Search case sensitively.
  819. This overrides the -i/--ignore-case and -S/--smart-case flags.
  820. "
  821. );
  822. let arg = RGArg::switch("case-sensitive")
  823. .short("s")
  824. .help(SHORT)
  825. .long_help(LONG)
  826. .overrides("ignore-case")
  827. .overrides("smart-case");
  828. args.push(arg);
  829. }
  830. fn flag_color(args: &mut Vec<RGArg>) {
  831. const SHORT: &str = "Controls when to use color.";
  832. const LONG: &str = long!(
  833. "\
  834. This flag controls when to use colors. The default setting is 'auto', which
  835. means ripgrep will try to guess when to use colors. For example, if ripgrep is
  836. printing to a terminal, then it will use colors, but if it is redirected to a
  837. file or a pipe, then it will suppress color output. ripgrep will suppress color
  838. output in some other circumstances as well. For example, if the TERM
  839. environment variable is not set or set to 'dumb', then ripgrep will not use
  840. colors.
  841. The possible values for this flag are:
  842. never Colors will never be used.
  843. auto The default. ripgrep tries to be smart.
  844. always Colors will always be used regardless of where output is sent.
  845. ansi Like 'always', but emits ANSI escapes (even in a Windows console).
  846. When the --vimgrep flag is given to ripgrep, then the default value for the
  847. --color flag changes to 'never'.
  848. "
  849. );
  850. let arg = RGArg::flag("color", "WHEN")
  851. .help(SHORT)
  852. .long_help(LONG)
  853. .possible_values(&["never", "auto", "always", "ansi"])
  854. .default_value_if("never", "vimgrep");
  855. args.push(arg);
  856. }
  857. fn flag_colors(args: &mut Vec<RGArg>) {
  858. const SHORT: &str = "Configure color settings and styles.";
  859. const LONG: &str = long!(
  860. "\
  861. This flag specifies color settings for use in the output. This flag may be
  862. provided multiple times. Settings are applied iteratively. Colors are limited
  863. to one of eight choices: red, blue, green, cyan, magenta, yellow, white and
  864. black. Styles are limited to nobold, bold, nointense, intense, nounderline
  865. or underline.
  866. The format of the flag is '{type}:{attribute}:{value}'. '{type}' should be
  867. one of path, line, column or match. '{attribute}' can be fg, bg or style.
  868. '{value}' is either a color (for fg and bg) or a text style. A special format,
  869. '{type}:none', will clear all color settings for '{type}'.
  870. For example, the following command will change the match color to magenta and
  871. the background color for line numbers to yellow:
  872. rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo.
  873. Extended colors can be used for '{value}' when the terminal supports ANSI color
  874. sequences. These are specified as either 'x' (256-color) or 'x,x,x' (24-bit
  875. truecolor) where x is a number between 0 and 255 inclusive. x may be given as
  876. a normal decimal number or a hexadecimal number, which is prefixed by `0x`.
  877. For example, the following command will change the match background color to
  878. that represented by the rgb value (0,128,255):
  879. rg --colors 'match:bg:0,128,255'
  880. or, equivalently,
  881. rg --colors 'match:bg:0x0,0x80,0xFF'
  882. Note that the the intense and nointense style flags will have no effect when
  883. used alongside these extended color codes.
  884. "
  885. );
  886. let arg = RGArg::flag("colors", "COLOR_SPEC")
  887. .help(SHORT)
  888. .long_help(LONG)
  889. .multiple();
  890. args.push(arg);
  891. }
  892. fn flag_column(args: &mut Vec<RGArg>) {
  893. const SHORT: &str = "Show column numbers.";
  894. const LONG: &str = long!(
  895. "\
  896. Show column numbers (1-based). This only shows the column numbers for the first
  897. match on each line. This does not try to account for Unicode. One byte is equal
  898. to one column. This implies --line-number.
  899. This flag can be disabled with --no-column.
  900. "
  901. );
  902. let arg = RGArg::switch("column")
  903. .help(SHORT)
  904. .long_help(LONG)
  905. .overrides("no-column");
  906. args.push(arg);
  907. let arg = RGArg::switch("no-column").hidden().overrides("column");
  908. args.push(arg);
  909. }
  910. fn flag_context(args: &mut Vec<RGArg>) {
  911. const SHORT: &str = "Show NUM lines before and after each match.";
  912. const LONG: &str = long!(
  913. "\
  914. Show NUM lines before and after each match. This is equivalent to providing
  915. both the -B/--before-context and -A/--after-context flags with the same value.
  916. This overrides both the -B/--before-context and -A/--after-context flags.
  917. "
  918. );
  919. let arg = RGArg::flag("context", "NUM")
  920. .short("C")
  921. .help(SHORT)
  922. .long_help(LONG)
  923. .number()
  924. .overrides("before-context")
  925. .overrides("after-context");
  926. args.push(arg);
  927. }
  928. fn flag_context_separator(args: &mut Vec<RGArg>) {
  929. const SHORT: &str = "Set the context separator string.";
  930. const LONG: &str = long!(
  931. "\
  932. The string used to separate non-contiguous context lines in the output. This
  933. is only used when one of the context flags is used (-A, -B or -C). Escape
  934. sequences like \\x7F or \\t may be used. The default value is --.
  935. When the context separator is set to an empty string, then a line break
  936. is still inserted. To completely disable context separators, use the
  937. --no-context-separator flag.
  938. "
  939. );
  940. let arg = RGArg::flag("context-separator", "SEPARATOR")
  941. .help(SHORT)
  942. .long_help(LONG)
  943. .overrides("no-context-separator");
  944. args.push(arg);
  945. let arg = RGArg::switch("no-context-separator")
  946. .hidden()
  947. .overrides("context-separator");
  948. args.push(arg);
  949. }
  950. fn flag_count(args: &mut Vec<RGArg>) {
  951. const SHORT: &str = "Only show the count of matching lines for each file.";
  952. const LONG: &str = long!(
  953. "\
  954. This flag suppresses normal output and shows the number of lines that match
  955. the given patterns for each file searched. Each file containing a match has its
  956. path and count printed on each line. Note that this reports the number of lines
  957. that match and not the total number of matches.
  958. If only one file is given to ripgrep, then only the count is printed if there
  959. is a match. The --with-filename flag can be used to force printing the file
  960. path in this case.
  961. This overrides the --count-matches flag. Note that when --count is combined
  962. with --only-matching, then ripgrep behaves as if --count-matches was given.
  963. "
  964. );
  965. let arg = RGArg::switch("count")
  966. .short("c")
  967. .help(SHORT)
  968. .long_help(LONG)
  969. .overrides("count-matches");
  970. args.push(arg);
  971. }
  972. fn flag_count_matches(args: &mut Vec<RGArg>) {
  973. const SHORT: &str =
  974. "Only show the count of individual matches for each file.";
  975. const LONG: &str = long!(
  976. "\
  977. This flag suppresses normal output and shows the number of individual
  978. matches of the given patterns for each file searched. Each file
  979. containing matches has its path and match count printed on each line.
  980. Note that this reports the total number of individual matches and not
  981. the number of lines that match.
  982. If only one file is given to ripgrep, then only the count is printed if there
  983. is a match. The --with-filename flag can be used to force printing the file
  984. path in this case.
  985. This overrides the --count flag. Note that when --count is combined with
  986. --only-matching, then ripgrep behaves as if --count-matches was given.
  987. "
  988. );
  989. let arg = RGArg::switch("count-matches")
  990. .help(SHORT)
  991. .long_help(LONG)
  992. .overrides("count");
  993. args.push(arg);
  994. }
  995. fn flag_crlf(args: &mut Vec<RGArg>) {
  996. const SHORT: &str = "Support CRLF line terminators (useful on Windows).";
  997. const LONG: &str = long!(
  998. "\
  999. When enabled, ripgrep will treat CRLF ('\\r\\n') as a line terminator instead
  1000. of just '\\n'.
  1001. Principally, this permits '$' in regex patterns to match just before CRLF
  1002. instead of just before LF. The underlying regex engine may not support this
  1003. natively, so ripgrep will translate all instances of '$' to '(?:\\r??$)'. This
  1004. may produce slightly different than desired match offsets. It is intended as a
  1005. work-around until the regex engine supports this natively.
  1006. CRLF support can be disabled with --no-crlf.
  1007. "
  1008. );
  1009. let arg = RGArg::switch("crlf")
  1010. .help(SHORT)
  1011. .long_help(LONG)
  1012. .overrides("no-crlf")
  1013. .overrides("null-data");
  1014. args.push(arg);
  1015. let arg = RGArg::switch("no-crlf").hidden().overrides("crlf");
  1016. args.push(arg);
  1017. }
  1018. fn flag_debug(args: &mut Vec<RGArg>) {
  1019. const SHORT: &str = "Show debug messages.";
  1020. const LONG: &str = long!(
  1021. "\
  1022. Show debug messages. Please use this when filing a bug report.
  1023. The --debug flag is generally useful for figuring out why ripgrep skipped
  1024. searching a particular file. The debug messages should mention all files
  1025. skipped and why they were skipped.
  1026. To get even more debug output, use the --trace flag, which implies --debug
  1027. along with additional trace data. With --trace, the output could be quite
  1028. large and is generally more useful for development.
  1029. "
  1030. );
  1031. let arg = RGArg::switch("debug").help(SHORT).long_help(LONG);
  1032. args.push(arg);
  1033. let arg = RGArg::switch("trace").hidden().overrides("debug");
  1034. args.push(arg);
  1035. }
  1036. fn flag_dfa_size_limit(args: &mut Vec<RGArg>) {
  1037. const SHORT: &str = "The upper size limit of the regex DFA.";
  1038. const LONG: &str = long!(
  1039. "\
  1040. The upper size limit of the regex DFA. The default limit is 10M. This should
  1041. only be changed on very large regex inputs where the (slower) fallback regex
  1042. engine may otherwise be used if the limit is reached.
  1043. The argument accepts the same size suffixes as allowed in with the
  1044. --max-filesize flag.
  1045. "
  1046. );
  1047. let arg = RGArg::flag("dfa-size-limit", "NUM+SUFFIX?")
  1048. .help(SHORT)
  1049. .long_help(LONG);
  1050. args.push(arg);
  1051. }
  1052. fn flag_encoding(args: &mut Vec<RGArg>) {
  1053. const SHORT: &str = "Specify the text encoding of files to search.";
  1054. const LONG: &str = long!(
  1055. "\
  1056. Specify the text encoding that ripgrep will use on all files searched. The
  1057. default value is 'auto', which will cause ripgrep to do a best effort automatic
  1058. detection of encoding on a per-file basis. Automatic detection in this case
  1059. only applies to files that begin with a UTF-8 or UTF-16 byte-order mark (BOM).
  1060. No other automatic detection is performed. One can also specify 'none' which
  1061. will then completely disable BOM sniffing and always result in searching the
  1062. raw bytes, including a BOM if it's present, regardless of its encoding.
  1063. Other supported values can be found in the list of labels here:
  1064. https://encoding.spec.whatwg.org/#concept-encoding-get
  1065. For more details on encoding and how ripgrep deals with it, see GUIDE.md.
  1066. This flag can be disabled with --no-encoding.
  1067. "
  1068. );
  1069. let arg = RGArg::flag("encoding", "ENCODING")
  1070. .short("E")
  1071. .help(SHORT)
  1072. .long_help(LONG);
  1073. args.push(arg);
  1074. let arg = RGArg::switch("no-encoding").hidden().overrides("encoding");
  1075. args.push(arg);
  1076. }
  1077. fn flag_engine(args: &mut Vec<RGArg>) {
  1078. const SHORT: &str = "Specify which regexp engine to use.";
  1079. const LONG: &str = long!(
  1080. "\
  1081. Specify which regular expression engine to use. When you choose a regex engine,
  1082. it applies that choice for every regex provided to ripgrep (e.g., via multiple
  1083. -e/--regexp or -f/--file flags).
  1084. Accepted values are 'default', 'pcre2', or 'auto'.
  1085. The default value is 'default', which is the fastest and should be good for
  1086. most use cases. The 'pcre2' engine is generally useful when you want to use
  1087. features such as look-around or backreferences. 'auto' will dynamically choose
  1088. between supported regex engines depending on the features used in a pattern on
  1089. a best effort basis.
  1090. Note that the 'pcre2' engine is an optional ripgrep feature. If PCRE2 wasn't
  1091. including in your build of ripgrep, then using this flag will result in ripgrep
  1092. printing an error message and exiting.
  1093. This overrides previous uses of --pcre2 and --auto-hybrid-regex flags.
  1094. "
  1095. );
  1096. let arg = RGArg::flag("engine", "ENGINE")
  1097. .help(SHORT)
  1098. .long_help(LONG)
  1099. .possible_values(&["default", "pcre2", "auto"])
  1100. .default_value("default")
  1101. .overrides("pcre2")
  1102. .overrides("no-pcre2")
  1103. .overrides("auto-hybrid-regex")
  1104. .overrides("no-auto-hybrid-regex");
  1105. args.push(arg);
  1106. }
  1107. fn flag_file(args: &mut Vec<RGArg>) {
  1108. const SHORT: &str = "Search for patterns from the given file.";
  1109. const LONG: &str = long!(
  1110. "\
  1111. Search for patterns from the given file, with one pattern per line. When this
  1112. flag is used multiple times or in combination with the -e/--regexp flag,
  1113. then all patterns provided are searched. Empty pattern lines will match all
  1114. input lines, and the newline is not counted as part of the pattern.
  1115. A line is printed if and only if it matches at least one of the patterns.
  1116. "
  1117. );
  1118. let arg = RGArg::flag("file", "PATTERNFILE")
  1119. .short("f")
  1120. .help(SHORT)
  1121. .long_help(LONG)
  1122. .multiple()
  1123. .allow_leading_hyphen();
  1124. args.push(arg);
  1125. }
  1126. fn flag_files(args: &mut Vec<RGArg>) {
  1127. const SHORT: &str = "Print each file that would be searched.";
  1128. const LONG: &str = long!(
  1129. "\
  1130. Print each file that would be searched without actually performing the search.
  1131. This is useful to determine whether a particular file is being searched or not.
  1132. "
  1133. );
  1134. let arg = RGArg::switch("files")
  1135. .help(SHORT)
  1136. .long_help(LONG)
  1137. // This also technically conflicts with pattern, but the first file
  1138. // path will actually be in pattern.
  1139. .conflicts(&["file", "regexp", "type-list"]);
  1140. args.push(arg);
  1141. }
  1142. fn flag_files_with_matches(args: &mut Vec<RGArg>) {
  1143. const SHORT: &str = "Only print the paths with at least one match.";
  1144. const LONG: &str = long!(
  1145. "\
  1146. Only print the paths with at least one match.
  1147. This overrides --files-without-match.
  1148. "
  1149. );
  1150. let arg = RGArg::switch("files-with-matches")
  1151. .short("l")
  1152. .help(SHORT)
  1153. .long_help(LONG)
  1154. .overrides("files-without-match");
  1155. args.push(arg);
  1156. }
  1157. fn flag_files_without_match(args: &mut Vec<RGArg>) {
  1158. const SHORT: &str = "Only print the paths that contain zero matches.";
  1159. const LONG: &str = long!(
  1160. "\
  1161. Only print the paths that contain zero matches. This inverts/negates the
  1162. --files-with-matches flag.
  1163. This overrides --files-with-matches.
  1164. "
  1165. );
  1166. let arg = RGArg::switch("files-without-match")
  1167. .help(SHORT)
  1168. .long_help(LONG)
  1169. .overrides("files-with-matches");
  1170. args.push(arg);
  1171. }
  1172. fn flag_fixed_strings(args: &mut Vec<RGArg>) {
  1173. const SHORT: &str = "Treat the pattern as a literal string.";
  1174. const LONG: &str = long!(
  1175. "\
  1176. Treat the pattern as a literal string instead of a regular expression. When
  1177. this flag is used, special regular expression meta characters such as .(){}*+
  1178. do not need to be escaped.
  1179. This flag can be disabled with --no-fixed-strings.
  1180. "
  1181. );
  1182. let arg = RGArg::switch("fixed-strings")
  1183. .short("F")
  1184. .help(SHORT)
  1185. .long_help(LONG)
  1186. .overrides("no-fixed-strings");
  1187. args.push(arg);
  1188. let arg =
  1189. RGArg::switch("no-fixed-strings").hidden().overrides("fixed-strings");
  1190. args.push(arg);
  1191. }
  1192. fn flag_follow(args: &mut Vec<RGArg>) {
  1193. const SHORT: &str = "Follow symbolic links.";
  1194. const LONG: &str = long!(
  1195. "\
  1196. When this flag is enabled, ripgrep will follow symbolic links while traversing
  1197. directories. This is disabled by default. Note that ripgrep will check for
  1198. symbolic link loops and report errors if it finds one.
  1199. This flag can be disabled with --no-follow.
  1200. "
  1201. );
  1202. let arg = RGArg::switch("follow")
  1203. .short("L")
  1204. .help(SHORT)
  1205. .long_help(LONG)
  1206. .overrides("no-follow");
  1207. args.push(arg);
  1208. let arg = RGArg::switch("no-follow").hidden().overrides("follow");
  1209. args.push(arg);
  1210. }
  1211. fn flag_glob(args: &mut Vec<RGArg>) {
  1212. const SHORT: &str = "Include or exclude files.";
  1213. const LONG: &str = long!(
  1214. "\
  1215. Include or exclude files and directories for searching that match the given
  1216. glob. This always overrides any other ignore logic. Multiple glob flags may be
  1217. used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
  1218. it. If multiple globs match a file or directory, the glob given later in the
  1219. command line takes precedence.
  1220. When this flag is set, every file and directory is applied to it to test for
  1221. a match. So for example, if you only want to search in a particular directory
  1222. 'foo', then *-g foo* is incorrect because 'foo/bar' does not match the glob
  1223. 'foo'. Instead, you should use *-g 'foo/**'*.
  1224. "
  1225. );
  1226. let arg = RGArg::flag("glob", "GLOB")
  1227. .short("g")
  1228. .help(SHORT)
  1229. .long_help(LONG)
  1230. .multiple()
  1231. .allow_leading_hyphen();
  1232. args.push(arg);
  1233. }
  1234. fn flag_glob_case_insensitive(args: &mut Vec<RGArg>) {
  1235. const SHORT: &str = "Process all glob patterns case insensitively.";
  1236. const LONG: &str = long!(
  1237. "\
  1238. Process glob patterns given with the -g/--glob flag case insensitively. This
  1239. effectively treats --glob as --iglob.
  1240. This flag can be disabled with the --no-glob-case-insensitive flag.
  1241. "
  1242. );
  1243. let arg = RGArg::switch("glob-case-insensitive")
  1244. .help(SHORT)
  1245. .long_help(LONG)
  1246. .overrides("no-glob-case-insensitive");
  1247. args.push(arg);
  1248. let arg = RGArg::switch("no-glob-case-insensitive")
  1249. .hidden()
  1250. .overrides("glob-case-insensitive");
  1251. args.push(arg);
  1252. }
  1253. fn flag_heading(args: &mut Vec<RGArg>) {
  1254. const SHORT: &str = "Print matches grouped by each file.";
  1255. const LONG: &str = long!(
  1256. "\
  1257. This flag prints the file path above clusters of matches from each file instead
  1258. of printing the file path as a prefix for each matched line. This is the
  1259. default mode when printing to a terminal.
  1260. This overrides the --no-heading flag.
  1261. "
  1262. );
  1263. let arg = RGArg::switch("heading")
  1264. .help(SHORT)
  1265. .long_help(LONG)
  1266. .overrides("no-heading");
  1267. args.push(arg);
  1268. const NO_SHORT: &str = "Don't group matches by each file.";
  1269. const NO_LONG: &str = long!(
  1270. "\
  1271. Don't group matches by each file. If --no-heading is provided in addition to
  1272. the -H/--with-filename flag, then file paths will be pr

Large files files are truncated, but you can click here to view the full file