PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 printed as a prefix for
  1273. every matched line. This is the default mode when not printing to a terminal.
  1274. This overrides the --heading flag.
  1275. "
  1276. );
  1277. let arg = RGArg::switch("no-heading")
  1278. .help(NO_SHORT)
  1279. .long_help(NO_LONG)
  1280. .overrides("heading");
  1281. args.push(arg);
  1282. }
  1283. fn flag_hidden(args: &mut Vec<RGArg>) {
  1284. const SHORT: &str = "Search hidden files and directories.";
  1285. const LONG: &str = long!(
  1286. "\
  1287. Search hidden files and directories. By default, hidden files and directories
  1288. are skipped. Note that if a hidden file or a directory is whitelisted in an
  1289. ignore file, then it will be searched even if this flag isn't provided.
  1290. This flag can be disabled with --no-hidden.
  1291. "
  1292. );
  1293. let arg = RGArg::switch("hidden")
  1294. .help(SHORT)
  1295. .long_help(LONG)
  1296. .overrides("no-hidden");
  1297. args.push(arg);
  1298. let arg = RGArg::switch("no-hidden").hidden().overrides("hidden");
  1299. args.push(arg);
  1300. }
  1301. fn flag_iglob(args: &mut Vec<RGArg>) {
  1302. const SHORT: &str = "Include or exclude files case insensitively.";
  1303. const LONG: &str = long!(
  1304. "\
  1305. Include or exclude files and directories for searching that match the given
  1306. glob. This always overrides any other ignore logic. Multiple glob flags may be
  1307. used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude
  1308. it. Globs are matched case insensitively.
  1309. "
  1310. );
  1311. let arg = RGArg::flag("iglob", "GLOB")
  1312. .help(SHORT)
  1313. .long_help(LONG)
  1314. .multiple()
  1315. .allow_leading_hyphen();
  1316. args.push(arg);
  1317. }
  1318. fn flag_ignore_case(args: &mut Vec<RGArg>) {
  1319. const SHORT: &str = "Case insensitive search.";
  1320. const LONG: &str = long!(
  1321. "\
  1322. When this flag is provided, the given patterns will be searched case
  1323. insensitively. The case insensitivity rules used by ripgrep conform to
  1324. Unicode's \"simple\" case folding rules.
  1325. This flag overrides -s/--case-sensitive and -S/--smart-case.
  1326. "
  1327. );
  1328. let arg = RGArg::switch("ignore-case")
  1329. .short("i")
  1330. .help(SHORT)
  1331. .long_help(LONG)
  1332. .overrides("case-sensitive")
  1333. .overrides("smart-case");
  1334. args.push(arg);
  1335. }
  1336. fn flag_ignore_file(args: &mut Vec<RGArg>) {
  1337. const SHORT: &str = "Specify additional ignore files.";
  1338. const LONG: &str = long!(
  1339. "\
  1340. Specifies a path to one or more .gitignore format rules files. These patterns
  1341. are applied after the patterns found in .gitignore and .ignore are applied
  1342. and are matched relative to the current working directory. Multiple additional
  1343. ignore files can be specified by using the --ignore-file flag several times.
  1344. When specifying multiple ignore files, earlier files have lower precedence
  1345. than later files.
  1346. If you are looking for a way to include or exclude files and directories
  1347. directly on the command line, then used -g instead.
  1348. "
  1349. );
  1350. let arg = RGArg::flag("ignore-file", "PATH")
  1351. .help(SHORT)
  1352. .long_help(LONG)
  1353. .multiple()
  1354. .allow_leading_hyphen();
  1355. args.push(arg);
  1356. }
  1357. fn flag_ignore_file_case_insensitive(args: &mut Vec<RGArg>) {
  1358. const SHORT: &str = "Process ignore files case insensitively.";
  1359. const LONG: &str = long!(
  1360. "\
  1361. Process ignore files (.gitignore, .ignore, etc.) case insensitively. Note that
  1362. this comes with a performance penalty and is most useful on case insensitive
  1363. file systems (such as Windows).
  1364. This flag can be disabled with the --no-ignore-file-case-insensitive flag.
  1365. "
  1366. );
  1367. let arg = RGArg::switch("ignore-file-case-insensitive")
  1368. .help(SHORT)
  1369. .long_help(LONG)
  1370. .overrides("no-ignore-file-case-insensitive");
  1371. args.push(arg);
  1372. let arg = RGArg::switch("no-ignore-file-case-insensitive")
  1373. .hidden()
  1374. .overrides("ignore-file-case-insensitive");
  1375. args.push(arg);
  1376. }
  1377. fn flag_include_zero(args: &mut Vec<RGArg>) {
  1378. const SHORT: &str = "Include files with zero matches in summary";
  1379. const LONG: &str = long!(
  1380. "\
  1381. When used with --count or --count-matches, print the number of matches for
  1382. each file even if there were zero matches. This is disabled by default but can
  1383. be enabled to make ripgrep behave more like grep.
  1384. "
  1385. );
  1386. let arg = RGArg::switch("include-zero").help(SHORT).long_help(LONG);
  1387. args.push(arg);
  1388. }
  1389. fn flag_invert_match(args: &mut Vec<RGArg>) {
  1390. const SHORT: &str = "Invert matching.";
  1391. const LONG: &str = long!(
  1392. "\
  1393. Invert matching. Show lines that do not match the given patterns.
  1394. "
  1395. );
  1396. let arg =
  1397. RGArg::switch("invert-match").short("v").help(SHORT).long_help(LONG);
  1398. args.push(arg);
  1399. }
  1400. fn flag_json(args: &mut Vec<RGArg>) {
  1401. const SHORT: &str = "Show search results in a JSON Lines format.";
  1402. const LONG: &str = long!(
  1403. "\
  1404. Enable printing results in a JSON Lines format.
  1405. When this flag is provided, ripgrep will emit a sequence of messages, each
  1406. encoded as a JSON object, where there are five different message types:
  1407. **begin** - A message that indicates a file is being searched and contains at
  1408. least one match.
  1409. **end** - A message the indicates a file is done being searched. This message
  1410. also include summary statistics about the search for a particular file.
  1411. **match** - A message that indicates a match was found. This includes the text
  1412. and offsets of the match.
  1413. **context** - A message that indicates a contextual line was found. This
  1414. includes the text of the line, along with any match information if the search
  1415. was inverted.
  1416. **summary** - The final message emitted by ripgrep that contains summary
  1417. statistics about the search across all files.
  1418. Since file paths or the contents of files are not guaranteed to be valid UTF-8
  1419. and JSON itself must be representable by a Unicode encoding, ripgrep will emit
  1420. all data elements as objects with one of two keys: 'text' or 'bytes'. 'text' is
  1421. a normal JSON string when the data is valid UTF-8 while 'bytes' is the base64
  1422. encoded contents of the data.
  1423. The JSON Lines format is only supported for showing search results. It cannot
  1424. be used with other flags that emit other types of output, such as --files,
  1425. --files-with-matches, --files-without-match, --count or --count-matches.
  1426. ripgrep will report an error if any of the aforementioned flags are used in
  1427. concert with --json.
  1428. Other flags that control aspects of the standard output such as
  1429. --only-matching, --heading, --replace, --max-columns, etc., have no effect
  1430. when --json is set.
  1431. A more complete description of the JSON format used can be found here:
  1432. https://docs.rs/grep-printer/*/grep_printer/struct.JSON.html
  1433. The JSON Lines format can be disabled with --no-json.
  1434. "
  1435. );
  1436. let arg = RGArg::switch("json")
  1437. .help(SHORT)
  1438. .long_help(LONG)
  1439. .overrides("no-json")
  1440. .conflicts(&[
  1441. "count",
  1442. "count-matches",
  1443. "files",
  1444. "files-with-matches",
  1445. "files-without-match",
  1446. ]);
  1447. args.push(arg);
  1448. let arg = RGArg::switch("no-json").hidden().overrides("json");
  1449. args.push(arg);
  1450. }
  1451. fn flag_line_buffered(args: &mut Vec<RGArg>) {
  1452. const SHORT: &str = "Force line buffering.";
  1453. const LONG: &str = long!(
  1454. "\
  1455. When enabled, ripgrep will use line buffering. That is, whenever a matching
  1456. line is found, it will be flushed to stdout immediately. This is the default
  1457. when ripgrep's stdout is connected to a terminal, but otherwise, ripgrep will
  1458. use block buffering, which is typically faster. This flag forces ripgrep to
  1459. use line buffering even if it would otherwise use block buffering. This is
  1460. typically useful in shell pipelines, e.g.,
  1461. 'tail -f something.log | rg foo --line-buffered | rg bar'.
  1462. Forceful line buffering can be disabled with --no-line-buffered. Note that
  1463. using --no-line-buffered causes ripgrep to revert to its default behavior of
  1464. automatically detecting the buffering strategy. To force block buffering, use
  1465. the --block-buffered flag.
  1466. "
  1467. );
  1468. let arg = RGArg::switch("line-buffered")
  1469. .help(SHORT)
  1470. .long_help(LONG)
  1471. .overrides("no-line-buffered")
  1472. .overrides("block-buffered")
  1473. .overrides("no-block-buffered");
  1474. args.push(arg);
  1475. let arg = RGArg::switch("no-line-buffered")
  1476. .hidden()
  1477. .overrides("line-buffered")
  1478. .overrides("block-buffered")
  1479. .overrides("no-block-buffered");
  1480. args.push(arg);
  1481. }
  1482. fn flag_line_number(args: &mut Vec<RGArg>) {
  1483. const SHORT: &str = "Show line numbers.";
  1484. const LONG: &str = long!(
  1485. "\
  1486. Show line numbers (1-based). This is enabled by default when searching in a
  1487. terminal.
  1488. "
  1489. );
  1490. let arg = RGArg::switch("line-number")
  1491. .short("n")
  1492. .help(SHORT)
  1493. .long_help(LONG)
  1494. .overrides("no-line-number");
  1495. args.push(arg);
  1496. const NO_SHORT: &str = "Suppress line numbers.";
  1497. const NO_LONG: &str = long!(
  1498. "\
  1499. Suppress line numbers. This is enabled by default when not searching in a
  1500. terminal.
  1501. "
  1502. );
  1503. let arg = RGArg::switch("no-line-number")
  1504. .short("N")
  1505. .help(NO_SHORT)
  1506. .long_help(NO_LONG)
  1507. .overrides("line-number");
  1508. args.push(arg);
  1509. }
  1510. fn flag_line_regexp(args: &mut Vec<RGArg>) {
  1511. const SHORT: &str = "Only show matches surrounded by line boundaries.";
  1512. const LONG: &str = long!(
  1513. "\
  1514. Only show matches surrounded by line boundaries. This is equivalent to putting
  1515. ^...$ around all of the search patterns. In other words, this only prints lines
  1516. where the entire line participates in a match.
  1517. This overrides the --word-regexp flag.
  1518. "
  1519. );
  1520. let arg = RGArg::switch("line-regexp")
  1521. .short("x")
  1522. .help(SHORT)
  1523. .long_help(LONG)
  1524. .overrides("word-regexp");
  1525. args.push(arg);
  1526. }
  1527. fn flag_max_columns(args: &mut Vec<RGArg>) {
  1528. const SHORT: &str = "Don't print lines longer than this limit.";
  1529. const LONG: &str = long!(
  1530. "\
  1531. Don't print lines longer than this limit in bytes. Longer lines are omitted,
  1532. and only the number of matches in that line is printed.
  1533. When this flag is omitted or is set to 0, then it has no effect.
  1534. "
  1535. );
  1536. let arg = RGArg::flag("max-columns", "NUM")
  1537. .short("M")
  1538. .help(SHORT)
  1539. .long_help(LONG)
  1540. .number();
  1541. args.push(arg);
  1542. }
  1543. fn flag_max_columns_preview(args: &mut Vec<RGArg>) {
  1544. const SHORT: &str = "Print a preview for lines exceeding the limit.";
  1545. const LONG: &str = long!(
  1546. "\
  1547. When the '--max-columns' flag is used, ripgrep will by default completely
  1548. replace any line that is too long with a message indicating that a matching
  1549. line was removed. When this flag is combined with '--max-columns', a preview
  1550. of the line (corresponding to the limit size) is shown instead, where the part
  1551. of the line exceeding the limit is not shown.
  1552. If the '--max-columns' flag is not set, then this has no effect.
  1553. This flag can be disabled with '--no-max-columns-preview'.
  1554. "
  1555. );
  1556. let arg = RGArg::switch("max-columns-preview")
  1557. .help(SHORT)
  1558. .long_help(LONG)
  1559. .overrides("no-max-columns-preview");
  1560. args.push(arg);
  1561. let arg = RGArg::switch("no-max-columns-preview")
  1562. .hidden()
  1563. .overrides("max-columns-preview");
  1564. args.push(arg);
  1565. }
  1566. fn flag_max_count(args: &mut Vec<RGArg>) {
  1567. const SHORT: &str = "Limit the number of matches.";
  1568. const LONG: &str = long!(
  1569. "\
  1570. Limit the number of matching lines per file searched to NUM.
  1571. "
  1572. );
  1573. let arg = RGArg::flag("max-count", "NUM")
  1574. .short("m")
  1575. .help(SHORT)
  1576. .long_help(LONG)
  1577. .number();
  1578. args.push(arg);
  1579. }
  1580. fn flag_max_depth(args: &mut Vec<RGArg>) {
  1581. const SHORT: &str = "Descend at most NUM directories.";
  1582. const LONG: &str = long!(
  1583. "\
  1584. Limit the depth of directory traversal to NUM levels beyond the paths given. A
  1585. value of zero only searches the explicitly given paths themselves.
  1586. For example, 'rg --max-depth 0 dir/' is a no-op because dir/ will not be
  1587. descended into. 'rg --max-depth 1 dir/' will search only the direct children of
  1588. 'dir'.
  1589. "
  1590. );
  1591. let arg = RGArg::flag("max-depth", "NUM")
  1592. .help(SHORT)
  1593. .long_help(LONG)
  1594. .alias("maxdepth")
  1595. .number();
  1596. args.push(arg);
  1597. }
  1598. fn flag_max_filesize(args: &mut Vec<RGArg>) {
  1599. const SHORT: &str = "Ignore files larger than NUM in size.";
  1600. const LONG: &str = long!(
  1601. "\
  1602. Ignore files larger than NUM in size. This does not apply to directories.
  1603. The input format accepts suffixes of K, M or G which correspond to kilobytes,
  1604. megabytes and gigabytes, respectively. If no suffix is provided the input is
  1605. treated as bytes.
  1606. Examples: --max-filesize 50K or --max-filesize 80M
  1607. "
  1608. );
  1609. let arg =
  1610. RGArg::flag("max-filesize", "NUM+SUFFIX?").help(SHORT).long_help(LONG);
  1611. args.push(arg);
  1612. }
  1613. fn flag_mmap(args: &mut Vec<RGArg>) {
  1614. const SHORT: &str = "Search using memory maps when possible.";
  1615. const LONG: &str = long!(
  1616. "\
  1617. Search using memory maps when possible. This is enabled by default when ripgrep
  1618. thinks it will be faster.
  1619. Memory map searching doesn't currently support all options, so if an
  1620. incompatible option (e.g., --context) is given with --mmap, then memory maps
  1621. will not be used.
  1622. Note that ripgrep may abort unexpectedly when --mmap if it searches a file that
  1623. is simultaneously truncated.
  1624. This flag overrides --no-mmap.
  1625. "
  1626. );
  1627. let arg =
  1628. RGArg::switch("mmap").help(SHORT).long_help(LONG).overrides("no-mmap");
  1629. args.push(arg);
  1630. const NO_SHORT: &str = "Never use memory maps.";
  1631. const NO_LONG: &str = long!(
  1632. "\
  1633. Never use memory maps, even when they might be faster.
  1634. This flag overrides --mmap.
  1635. "
  1636. );
  1637. let arg = RGArg::switch("no-mmap")
  1638. .help(NO_SHORT)
  1639. .long_help(NO_LONG)
  1640. .overrides("mmap");
  1641. args.push(arg);
  1642. }
  1643. fn flag_multiline(args: &mut Vec<RGArg>) {
  1644. const SHORT: &str = "Enable matching across multiple lines.";
  1645. const LONG: &str = long!(
  1646. "\
  1647. Enable matching across multiple lines.
  1648. When multiline mode is enabled, ripgrep will lift the restriction that a match
  1649. cannot include a line terminator. For example, when multiline mode is not
  1650. enabled (the default), then the regex '\\p{any}' will match any Unicode
  1651. codepoint other than '\\n'. Similarly, the regex '\\n' is explicitly forbidden,
  1652. and if you try to use it, ripgrep will return an error. However, when multiline
  1653. mode is enabled, '\\p{any}' will match any Unicode codepoint, including '\\n',
  1654. and regexes like '\\n' are permitted.
  1655. An important caveat is that multiline mode does not change the match semantics
  1656. of '.'. Namely, in most regex matchers, a '.' will by default match any
  1657. character other than '\\n', and this is true in ripgrep as well. In order to
  1658. make '.' match '\\n', you must enable the \"dot all\" flag inside the regex.
  1659. For example, both '(?s).' and '(?s:.)' have the same semantics, where '.' will
  1660. match any character, including '\\n'. Alternatively, the '--multiline-dotall'
  1661. flag may be passed to make the \"dot all\" behavior the default. This flag only
  1662. applies when multiline search is enabled.
  1663. There is no limit on the number of the lines that a single match can span.
  1664. **WARNING**: Because of how the underlying regex engine works, multiline
  1665. searches may be slower than normal line-oriented searches, and they may also
  1666. use more memory. In particular, when multiline mode is enabled, ripgrep
  1667. requires that each file it searches is laid out contiguously in memory
  1668. (either by reading it onto the heap or by memory-mapping it). Things that
  1669. cannot be memory-mapped (such as stdin) will be consumed until EOF before
  1670. searching can begin. In general, ripgrep will only do these things when
  1671. necessary. Specifically, if the --multiline flag is provided but the regex
  1672. does not contain patterns that would match '\\n' characters, then ripgrep
  1673. will automatically avoid reading each file into memory before searching it.
  1674. Nevertheless, if you only care about matches spanning at most one line, then it
  1675. is always better to disable multiline mode.
  1676. This flag can be disabled with --no-multiline.
  1677. "
  1678. );
  1679. let arg = RGArg::switch("multiline")
  1680. .short("U")
  1681. .help(SHORT)
  1682. .long_help(LONG)
  1683. .overrides("no-multiline");
  1684. args.push(arg);
  1685. let arg = RGArg::switch("no-multiline").hidden().overrides("multiline");
  1686. args.push(arg);
  1687. }
  1688. fn flag_multiline_dotall(args: &mut Vec<RGArg>) {
  1689. const SHORT: &str = "Make '.' match new lines when multiline is enabled.";
  1690. const LONG: &str = long!(
  1691. "\
  1692. This flag enables \"dot all\" in your regex pattern, which causes '.' to match
  1693. newlines when multiline searching is enabled. This flag has no effect if
  1694. multiline searching isn't enabled with the --multiline flag.
  1695. Normally, a '.' will match any character except newlines. While this behavior
  1696. typically isn't relevant for line-oriented matching (since matches can span at
  1697. most one line), this can be useful when searching with the -U/--multiline flag.
  1698. By default, the multiline mode runs without this flag.
  1699. This flag is generally intended to be used in an alias or your ripgrep config
  1700. file if you prefer \"dot all\" semantics by default. Note that regardless of
  1701. whether this flag is used, \"dot all\" semantics can still be controlled via
  1702. inline flags in the regex pattern itself, e.g., '(?s:.)' always enables \"dot
  1703. all\" whereas '(?-s:.)' always disables \"dot all\".
  1704. This flag can be disabled with --no-multiline-dotall.
  1705. "
  1706. );
  1707. let arg = RGArg::switch("multiline-dotall")
  1708. .help(SHORT)
  1709. .long_help(LONG)
  1710. .overrides("no-multiline-dotall");
  1711. args.push(arg);
  1712. let arg = RGArg::switch("no-multiline-dotall")
  1713. .hidden()
  1714. .overrides("multiline-dotall");
  1715. args.push(arg);
  1716. }
  1717. fn flag_no_config(args: &mut Vec<RGArg>) {
  1718. const SHORT: &str = "Never read configuration files.";
  1719. const LONG: &str = long!(
  1720. "\
  1721. Never read configuration files. When this flag is present, ripgrep will not
  1722. respect the RIPGREP_CONFIG_PATH environment variable.
  1723. If ripgrep ever grows a feature to automatically read configuration files in
  1724. pre-defined locations, then this flag will also disable that behavior as well.
  1725. "
  1726. );
  1727. let arg = RGArg::switch("no-config").help(SHORT).long_help(LONG);
  1728. args.push(arg);
  1729. }
  1730. fn flag_no_ignore(args: &mut Vec<RGArg>) {
  1731. const SHORT: &str = "Don't respect ignore files.";
  1732. const LONG: &str = long!(
  1733. "\
  1734. Don't respect ignore files (.gitignore, .ignore, etc.). This implies
  1735. --no-ignore-dot, --no-ignore-exclude, --no-ignore-global, no-ignore-parent and
  1736. --no-ignore-vcs.
  1737. This does *not* imply --no-ignore-files, since --ignore-file is specified
  1738. explicitly as a command line argument.
  1739. This flag can be disabled with the --ignore flag.
  1740. "
  1741. );
  1742. let arg = RGArg::switch("no-ignore")
  1743. .help(SHORT)
  1744. .long_help(LONG)
  1745. .overrides("ignore");
  1746. args.push(arg);
  1747. let arg = RGArg::switch("ignore").hidden().overrides("no-ignore");
  1748. args.push(arg);
  1749. }
  1750. fn flag_no_ignore_dot(args: &mut Vec<RGArg>) {
  1751. const SHORT: &str = "Don't respect .ignore files.";
  1752. const LONG: &str = long!(
  1753. "\
  1754. Don't respect .ignore files.
  1755. This flag can be disabled with the --ignore-dot flag.
  1756. "
  1757. );
  1758. let arg = RGArg::switch("no-ignore-dot")
  1759. .help(SHORT)
  1760. .long_help(LONG)
  1761. .overrides("ignore-dot");
  1762. args.push(arg);
  1763. let arg = RGArg::switch("ignore-dot").hidden().overrides("no-ignore-dot");
  1764. args.push(arg);
  1765. }
  1766. fn flag_no_ignore_exclude(args: &mut Vec<RGArg>) {
  1767. const SHORT: &str = "Don't respect local exclusion files.";
  1768. const LONG: &str = long!(
  1769. "\
  1770. Don't respect ignore files that are manually configured for the repository
  1771. such as git's '.git/info/exclude'.
  1772. This flag can be disabled with the --ignore-exclude flag.
  1773. "
  1774. );
  1775. let arg = RGArg::switch("no-ignore-exclude")
  1776. .help(SHORT)
  1777. .long_help(LONG)
  1778. .overrides("ignore-exclude");
  1779. args.push(arg);
  1780. let arg = RGArg::switch("ignore-exclude")
  1781. .hidden()
  1782. .overrides("no-ignore-exclude");
  1783. args.push(arg);
  1784. }
  1785. fn flag_no_ignore_files(args: &mut Vec<RGArg>) {
  1786. const SHORT: &str = "Don't respect --ignore-file arguments.";
  1787. const LONG: &str = long!(
  1788. "\
  1789. When set, any --ignore-file flags, even ones that come after this flag, are
  1790. ignored.
  1791. This flag can be disabled with the --ignore-files flag.
  1792. "
  1793. );
  1794. let arg = RGArg::switch("no-ignore-files")
  1795. .help(SHORT)
  1796. .long_help(LONG)
  1797. .overrides("ignore-files");
  1798. args.push(arg);
  1799. let arg =
  1800. RGArg::switch("ignore-files").hidden().overrides("no-ignore-files");
  1801. args.push(arg);
  1802. }
  1803. fn flag_no_ignore_global(args: &mut Vec<RGArg>) {
  1804. const SHORT: &str = "Don't respect global ignore files.";
  1805. const LONG: &str = long!(
  1806. "\
  1807. Don't respect ignore files that come from \"global\" sources such as git's
  1808. `core.excludesFile` configuration option (which defaults to
  1809. `$HOME/.config/git/ignore`).
  1810. This flag can be disabled with the --ignore-global flag.
  1811. "
  1812. );
  1813. let arg = RGArg::switch("no-ignore-global")
  1814. .help(SHORT)
  1815. .long_help(LONG)
  1816. .overrides("ignore-global");
  1817. args.push(arg);
  1818. let arg =
  1819. RGArg::switch("ignore-global").hidden().overrides("no-ignore-global");
  1820. args.push(arg);
  1821. }
  1822. fn flag_no_ignore_messages(args: &mut Vec<RGArg>) {
  1823. const SHORT: &str = "Suppress gitignore parse error messages.";
  1824. const LONG: &str = long!(
  1825. "\
  1826. Suppresses all error messages related to parsing ignore files such as .ignore
  1827. or .gitignore.
  1828. This flag can be disabled with the --ignore-messages flag.
  1829. "
  1830. );
  1831. let arg = RGArg::switch("no-ignore-messages")
  1832. .help(SHORT)
  1833. .long_help(LONG)
  1834. .overrides("ignore-messages");
  1835. args.push(arg);
  1836. let arg = RGArg::switch("ignore-messages")
  1837. .hidden()
  1838. .overrides("no-ignore-messages");
  1839. args.push(arg);
  1840. }
  1841. fn flag_no_ignore_parent(args: &mut Vec<RGArg>) {
  1842. const SHORT: &str = "Don't respect ignore files in parent directories.";
  1843. const LONG: &str = long!(
  1844. "\
  1845. Don't respect ignore files (.gitignore, .ignore, etc.) in parent directories.
  1846. This flag can be disabled with the --ignore-parent flag.
  1847. "
  1848. );
  1849. let arg = RGArg::switch("no-ignore-parent")
  1850. .help(SHORT)
  1851. .long_help(LONG)
  1852. .overrides("ignore-parent");
  1853. args.push(arg);
  1854. let arg =
  1855. RGArg::switch("ignore-parent").hidden().overrides("no-ignore-parent");
  1856. args.push(arg);
  1857. }
  1858. fn flag_no_ignore_vcs(args: &mut Vec<RGArg>) {
  1859. const SHORT: &str = "Don't respect VCS ignore files.";
  1860. const LONG: &str = long!(
  1861. "\
  1862. Don't respect version control ignore files (.gitignore, etc.). This implies
  1863. --no-ignore-parent for VCS files. Note that .ignore files will continue to be
  1864. respected.
  1865. This flag can be disabled with the --ignore-vcs flag.
  1866. "
  1867. );
  1868. let arg = RGArg::switch("no-ignore-vcs")
  1869. .help(SHORT)
  1870. .long_help(LONG)
  1871. .overrides("ignore-vcs");
  1872. args.push(arg);
  1873. let arg = RGArg::switch("ignore-vcs").hidden().overrides("no-ignore-vcs");
  1874. args.push(arg);
  1875. }
  1876. fn flag_no_messages(args: &mut Vec<RGArg>) {
  1877. const SHORT: &str = "Suppress some error messages.";
  1878. const LONG: &str = long!(
  1879. "\
  1880. Suppress all error messages related to opening and reading files. Error
  1881. messages related to the syntax of the pattern given are still shown.
  1882. This flag can be disabled with the --messages flag.
  1883. "
  1884. );
  1885. let arg = RGArg::switch("no-messages")
  1886. .help(SHORT)
  1887. .long_help(LONG)
  1888. .overrides("messages");
  1889. args.push(arg);
  1890. let arg = RGArg::switch("messages").hidden().overrides("no-messages");
  1891. args.push(arg);
  1892. }
  1893. fn flag_no_pcre2_unicode(args: &mut Vec<RGArg>) {
  1894. const SHORT: &str = "Disable Unicode mode for PCRE2 matching.";
  1895. const LONG: &str = long!(
  1896. "\
  1897. DEPRECATED. Use --no-unicode instead.
  1898. This flag is now an alias for --no-unicode. And --pcre2-unicode is an alias
  1899. for --unicode.
  1900. "
  1901. );
  1902. let arg = RGArg::switch("no-pcre2-unicode")
  1903. .help(SHORT)
  1904. .long_help(LONG)
  1905. .overrides("pcre2-unicode")
  1906. .overrides("unicode");
  1907. args.push(arg);
  1908. let arg = RGArg::switch("pcre2-unicode")
  1909. .hidden()
  1910. .overrides("no-pcre2-unicode")
  1911. .overrides("no-unicode");
  1912. args.push(arg);
  1913. }
  1914. fn flag_no_require_git(args: &mut Vec<RGArg>) {
  1915. const SHORT: &str = "Do not require a git repository to use gitignores.";
  1916. const LONG: &str = long!(
  1917. "\
  1918. By default, ripgrep will only respect global gitignore rules, .gitignore rules
  1919. and local exclude rules if ripgrep detects that you are searching inside a
  1920. git repository. This flag allows you to relax this restriction such that
  1921. ripgrep will respect all git related ignore rules regardless of whether you're
  1922. searching in a git repository or not.
  1923. This flag can be disabled with --require-git.
  1924. "
  1925. );
  1926. let arg = RGArg::switch("no-require-git")
  1927. .help(SHORT)
  1928. .long_help(LONG)
  1929. .overrides("require-git");
  1930. args.push(arg);
  1931. let arg =
  1932. RGArg::switch("require-git").hidden().overrides("no-require-git");
  1933. args.push(arg);
  1934. }
  1935. fn flag_no_unicode(args: &mut Vec<RGArg>) {
  1936. const SHORT: &str = "Disable Unicode mode.";
  1937. const LONG: &str = long!(
  1938. "\
  1939. By default, ripgrep will enable \"Unicode mode\" in all of its regexes. This
  1940. has a number of consequences:
  1941. * '.' will only match valid UTF-8 encoded scalar values.
  1942. * Classes like '\\w', '\\s', '\\d' are all Unicode aware and much bigger
  1943. than their ASCII only versions.
  1944. * Case insensitive matching will use Unicode case folding.
  1945. * A large array of classes like '\\p{Emoji}' are available.
  1946. * Word boundaries ('\\b' and '\\B') use the Unicode definition of a word
  1947. character.
  1948. In some cases it can be desirable to turn these things off. The --no-unicode
  1949. flag will do exactly that.
  1950. For PCRE2 specifically, Unicode mode represents a critical trade off in the
  1951. user experience of ripgrep. In particular, unlike the default regex engine,
  1952. PCRE2 does not support the ability to search possibly invalid UTF-8 with
  1953. Unicode features enabled. Instead, PCRE2 *requires* that everything it searches
  1954. when Unicode mode is enabled is valid UTF-8. (Or valid UTF-16/UTF-32, but for
  1955. the purposes of ripgrep, we only discuss UTF-8.) This means that if you have
  1956. PCRE2's Unicode mode enabled and you attempt to search invalid UTF-8, then
  1957. the search for that file will halt and print an error. For this reason, when
  1958. PCRE2's Unicode mode is enabled, ripgrep will automatically \"fix\" invalid
  1959. UTF-8 sequences by replacing them with the Unicode replacement codepoint. This
  1960. penalty does not occur when using the default regex engine.
  1961. If you would rather see the encoding errors surfaced by PCRE2 when Unicode mode
  1962. is enabled, then pass the --no-encoding flag to disable all transcoding.
  1963. The --no-unicode flag can be disabled with --unicode. Note that
  1964. --no-pcre2-unicode and --pcre2-unicode are aliases for --no-unicode and
  1965. --unicode, respectively.
  1966. "
  1967. );
  1968. let arg = RGArg::switch("no-unicode")
  1969. .help(SHORT)
  1970. .long_help(LONG)
  1971. .overrides("unicode")
  1972. .overrides("pcre2-unicode");
  1973. args.push(arg);
  1974. let arg = RGArg::switch("unicode")
  1975. .hidden()
  1976. .overrides("no-unicode")
  1977. .overrides("no-pcre2-unicode");
  1978. args.push(arg);
  1979. }
  1980. fn flag_null(args: &mut Vec<RGArg>) {
  1981. const SHORT: &str = "Print a NUL byte after file paths.";
  1982. const LONG: &str = long!(
  1983. "\
  1984. Whenever a file path is printed, follow it with a NUL byte. This includes
  1985. printing file paths before matches, and when printing a list of matching files
  1986. such as with --count, --files-with-matches and --files. This option is useful
  1987. for use with xargs.
  1988. "
  1989. );
  1990. let arg = RGArg::switch("null").short("0").help(SHORT).long_help(LONG);
  1991. args.push(arg);
  1992. }
  1993. fn flag_null_data(args: &mut Vec<RGArg>) {
  1994. const SHORT: &str = "Use NUL as a line terminator instead of \\n.";
  1995. const LONG: &str = long!(
  1996. "\
  1997. Enabling this option causes ripgrep to use NUL as a line terminator instead of
  1998. the default of '\\n'.
  1999. This is useful when searching large binary files that would otherwise have very
  2000. long lines if '\\n' were used as the line terminator. In particular, ripgrep
  2001. requires that, at a minimum, each line must fit into memory. Using NUL instead
  2002. can be a useful stopgap to keep memory requirements low and avoid OOM (out of
  2003. memory) conditions.
  2004. This is also useful for processing NUL delimited data, such as that emitted
  2005. when using ripgrep's -0/--null flag or find's --print0 flag.
  2006. Using this flag implies -a/--text.
  2007. "
  2008. );
  2009. let arg = RGArg::switch("null-data")
  2010. .help(SHORT)
  2011. .long_help(LONG)
  2012. .overrides("crlf");
  2013. args.push(arg);
  2014. }
  2015. fn flag_one_file_system(args: &mut Vec<RGArg>) {
  2016. const SHORT: &str =
  2017. "Do not descend into directories on other file systems.";
  2018. const LONG: &str = long!(
  2019. "\
  2020. When enabled, ripgrep will not cross file system boundaries relative to where
  2021. the search started from.
  2022. Note that this applies to each path argument given to ripgrep. For example, in
  2023. the command 'rg --one-file-system /foo/bar /quux/baz', ripgrep will search both
  2024. '/foo/bar' and '/quux/baz' even if they are on different file systems, but will
  2025. not cross a file system boundary when traversing each path's directory tree.
  2026. This is similar to find's '-xdev' or '-mount' flag.
  2027. This flag can be disabled with --no-one-file-system.
  2028. "
  2029. );
  2030. let arg = RGArg::switch("one-file-system")
  2031. .help(SHORT)
  2032. .long_help(LONG)
  2033. .overrides("no-one-file-system");
  2034. args.push(arg);
  2035. let arg = RGArg::switch("no-one-file-system")
  2036. .hidden()
  2037. .overrides("one-file-system");
  2038. args.push(arg);
  2039. }
  2040. fn flag_only_matching(args: &mut Vec<RGArg>) {
  2041. const SHORT: &str = "Print only matched parts of a line.";
  2042. const LONG: &str = long!(
  2043. "\
  2044. Print only the matched (non-empty) parts of a matching line, with each such
  2045. part on a separate output line.
  2046. "
  2047. );
  2048. let arg =
  2049. RGArg::switch("only-matching").short("o").help(SHORT).long_help(LONG);
  2050. args.push(arg);
  2051. }
  2052. fn flag_path_separator(args: &mut Vec<RGArg>) {
  2053. const SHORT: &str = "Set the path separator.";
  2054. const LONG: &str = long!(
  2055. "\
  2056. Set the path separator to use when printing file paths. This defaults to your
  2057. platform's path separator, which is / on Unix and \\ on Windows. This flag is
  2058. intended for overriding the default when the environment demands it (e.g.,
  2059. cygwin). A path separator is limited to a single byte.
  2060. "
  2061. );
  2062. let arg =
  2063. RGArg::flag("path-separator", "SEPARATOR").help(SHORT).long_help(LONG);
  2064. args.push(arg);
  2065. }
  2066. fn flag_passthru(args: &mut Vec<RGArg>) {
  2067. const SHORT: &str = "Print both matching and non-matching lines.";
  2068. const LONG: &str = long!(
  2069. "\
  2070. Print both matching and non-matching lines.
  2071. Another way to achieve a similar effect is by modifying your pattern to match
  2072. the empty string. For example, if you are searching using 'rg foo' then using
  2073. 'rg \"^|foo\"' instead will emit every line in every file searched, but only
  2074. occurrences of 'foo' will be highlighted. This flag enables the same behavior
  2075. without needing to modify the pattern.
  2076. "
  2077. );
  2078. let arg = RGArg::switch("passthru")
  2079. .help(SHORT)
  2080. .long_help(LONG)
  2081. .alias("passthrough");
  2082. args.push(arg);
  2083. }
  2084. fn flag_pcre2(args: &mut Vec<RGArg>) {
  2085. const SHORT: &str = "Enable PCRE2 matching.";
  2086. const LONG: &str = long!(
  2087. "\
  2088. When this flag is present, ripgrep will use the PCRE2 regex engine instead of
  2089. its default regex engine.
  2090. This is generally useful when you want to use features such as look-around
  2091. or backreferences.
  2092. Note that PCRE2 is an optional ripgrep feature. If PCRE2 wasn't included in
  2093. your build of ripgrep, then using this flag will result in ripgrep printing
  2094. an error message and exiting. PCRE2 may also have worse user experience in
  2095. some cases, since it has fewer introspection APIs than ripgrep's default regex
  2096. engine. For example, if you use a '\\n' in a PCRE2 regex without the
  2097. '-U/--multiline' flag, then ripgrep will silently fail to match anything
  2098. instead of reporting an error immediately (like it does with the default
  2099. regex engine).
  2100. Related flags: --no-pcre2-unicode
  2101. This flag can be disabled with --no-pcre2.
  2102. "
  2103. );
  2104. let arg = RGArg::switch("pcre2")
  2105. .short("P")
  2106. .help(SHORT)
  2107. .long_help(LONG)
  2108. .overrides("no-pcre2")
  2109. .overrides("auto-hybrid-regex")
  2110. .overrides("no-auto-hybrid-regex")
  2111. .overrides("engine");
  2112. args.push(arg);
  2113. let arg = RGArg::switch("no-pcre2")
  2114. .hidden()
  2115. .overrides("pcre2")
  2116. .overrides("auto-hybrid-regex")
  2117. .overrides("no-auto-hybrid-regex")
  2118. .overrides("engine");
  2119. args.push(arg);
  2120. }
  2121. fn flag_pcre2_version(args: &mut Vec<RGArg>) {
  2122. const SHORT: &str = "Print the version of PCRE2 that ripgrep uses.";
  2123. const LONG: &str = long!(
  2124. "\
  2125. When this flag is present, ripgrep will print the version of PCRE2 in use,
  2126. along with other information, and then exit. If PCRE2 is not available, then
  2127. ripgrep will print an error message and exit with an error code.
  2128. "
  2129. );
  2130. let arg = RGArg::switch("pcre2-version").help(SHORT).long_help(LONG);
  2131. args.push(arg);
  2132. }
  2133. fn flag_pre(args: &mut Vec<RGArg>) {
  2134. const SHORT: &str = "search outputs of COMMAND FILE for each FILE";
  2135. const LONG: &str = long!(
  2136. "\
  2137. For each input FILE, search the standard output of COMMAND FILE rather than the
  2138. contents of FILE. This option expects the COMMAND program to either be an
  2139. absolute path or to be available in your PATH. Either an empty string COMMAND
  2140. or the '--no-pre' flag will disable this behavior.
  2141. WARNING: When this flag is set, ripgrep will unconditionally spawn a
  2142. process for every file that is searched. Therefore, this can incur an
  2143. unnecessarily large performance penalty if you don't otherwise need the
  2144. flexibility offered by this flag. One possible mitigation to this is to use
  2145. the '--pre-glob' flag to limit which files a preprocessor is run with.
  2146. A preprocessor is not run when ripgrep is searching stdin.
  2147. When searching over sets of files that may require one of several decoders
  2148. as preprocessors, COMMAND should be a wrapper program or script which first
  2149. classifies FILE based on magic numbers/content or based on the FILE name and
  2150. then dispatches to an appropriate preprocessor. Each COMMAND also has its
  2151. standard input connected to FILE for convenience.
  2152. For example, a shell script for COMMAND might look like:
  2153. case \"$1\" in
  2154. *.pdf)
  2155. exec pdftotext \"$1\" -
  2156. ;;
  2157. *)
  2158. case $(file \"$1\") in
  2159. *Zstandard*)
  2160. exec pzstd -cdq
  2161. ;;
  2162. *)
  2163. exec cat
  2164. ;;
  2165. esac
  2166. ;;
  2167. esac
  2168. The above script uses `pdftotext` to convert a PDF file to plain text. For
  2169. all other files, the script uses the `file` utility to sniff the type of the
  2170. file based on its contents. If it is a compressed file in the Zstandard format,
  2171. then `pzstd` is used to decompress the contents to stdout.
  2172. This overrides the -z/--search-zip flag.
  2173. "
  2174. );
  2175. let arg = RGArg::flag("pre", "COMMAND")
  2176. .help(SHORT)
  2177. .long_help(LONG)
  2178. .overrides("no-pre")
  2179. .overrides("search-zip");
  2180. args.push(arg);
  2181. let arg = RGArg::switch("no-pre").hidden().overrides("pre");
  2182. args.push(arg);
  2183. }
  2184. fn flag_pre_glob(args: &mut Vec<RGArg>) {
  2185. const SHORT: &str =
  2186. "Include or exclude files from a preprocessing command.";
  2187. const LONG: &str = long!(
  2188. "\
  2189. This flag works in conjunction with the --pre flag. Namely, when one or more
  2190. --pre-glob flags are given, then only files that match the given set of globs
  2191. will be handed to the command specified by the --pre flag. Any non-matching
  2192. files will be searched without using the preprocessor command.
  2193. This flag is useful when searching many files with the --pre flag. Namely,
  2194. it permits the ability to avoid process overhead for files that don't need
  2195. preprocessing. For example, given the following shell script, 'pre-pdftotext':
  2196. #!/bin/sh
  2197. pdftotext \"$1\" -
  2198. then it is possible to use '--pre pre-pdftotext --pre-glob \'*.pdf\'' to make
  2199. it so ripgrep only executes the 'pre-pdftotext' command on files with a '.pdf'
  2200. extension.
  2201. Multiple --pre-glob flags may be used. Globbing rules match .gitignore globs.
  2202. Precede a glob with a ! to exclude it.
  2203. This flag has no effect if the --pre flag is not used.
  2204. "
  2205. );
  2206. let arg = RGArg::flag("pre-glob", "GLOB")
  2207. .help(SHORT)
  2208. .long_help(LONG)
  2209. .multiple()
  2210. .allow_leading_hyphen();
  2211. args.push(arg);
  2212. }
  2213. fn flag_pretty(args: &mut Vec<RGArg>) {
  2214. const SHORT: &str = "Alias for --color always --heading --line-number.";
  2215. const LONG: &str = long!(
  2216. "\
  2217. This is a convenience alias for '--color always --heading --line-number'. This
  2218. flag is useful when you still want pretty output even if you're piping ripgrep
  2219. to another program or file. For example: 'rg -p foo | less -R'.
  2220. "
  2221. );
  2222. let arg = RGArg::switch("pretty").short("p").help(SHORT).long_help(LONG);
  2223. args.push(arg);
  2224. }
  2225. fn flag_quiet(args: &mut Vec<RGArg>) {
  2226. const SHORT: &str = "Do not print anything to stdout.";
  2227. const LONG: &str = long!(
  2228. "\
  2229. Do not print anything to stdout. If a match is found in a file, then ripgrep
  2230. will stop searching. This is useful when ripgrep is used only for its exit
  2231. code (which will be an error if no matches are found).
  2232. When --files is used, then ripgrep will stop finding files after finding the
  2233. first file that matches all ignore rules.
  2234. "
  2235. );
  2236. let arg = RGArg::switch("quiet").short("q").help(SHORT).long_help(LONG);
  2237. args.push(arg);
  2238. }
  2239. fn flag_regex_size_limit(args: &mut Vec<RGArg>) {
  2240. const SHORT: &str = "The upper size limit of the compiled regex.";
  2241. const LONG: &str = long!(
  2242. "\
  2243. The upper size limit of the compiled regex. The default limit is 10M.
  2244. The argument accepts the same size suffixes as allowed in the --max-filesize
  2245. flag.
  2246. "
  2247. );
  2248. let arg = RGArg::flag("regex-size-limit", "NUM+SUFFIX?")
  2249. .help(SHORT)
  2250. .long_help(LONG);
  2251. args.push(arg);
  2252. }
  2253. fn flag_regexp(args: &mut Vec<RGArg>) {
  2254. const SHORT: &str = "A pattern to search for.";
  2255. const LONG: &str = long!(
  2256. "\
  2257. A pattern to search for. This option can be provided multiple times, where
  2258. all patterns given are searched. Lines matching at least one of the provided
  2259. patterns are printed. This flag can also be used when searching for patterns
  2260. that start with a dash.
  2261. For example, to search for the literal '-foo', you can use this flag:
  2262. rg -e -foo
  2263. You can also use the special '--' delimiter to indicate that no more flags
  2264. will be provided. Namely, the following is equivalent to the above:
  2265. rg -- -foo
  2266. "
  2267. );
  2268. let arg = RGArg::flag("regexp", "PATTERN")
  2269. .short("e")
  2270. .help(SHORT)
  2271. .long_help(LONG)
  2272. .multiple()
  2273. .allow_leading_hyphen();
  2274. args.push(arg);
  2275. }
  2276. fn flag_replace(args: &mut Vec<RGArg>) {
  2277. const SHORT: &str = "Replace matches with the given text.";
  2278. const LONG: &str = long!(
  2279. "\
  2280. Replace every match with the text given when printing results. Neither this
  2281. flag nor any other ripgrep flag will modify your files.
  2282. Capture group indices (e.g., $5) and names (e.g., $foo) are supported in the
  2283. replacement string. In shells such as Bash and zsh, you should wrap the
  2284. pattern in single quotes instead of double quotes. Otherwise, capture group
  2285. indices will be replaced by expanded shell variables which will most likely
  2286. be empty.
  2287. To write a literal '$', use '$$'.
  2288. Note that the replacement by default replaces each match, and NOT the entire
  2289. line. To replace the entire line, you should match the entire line.
  2290. This flag can be used with the -o/--only-matching flag.
  2291. "
  2292. );
  2293. let arg = RGArg::flag("replace", "REPLACEMENT_TEXT")
  2294. .short("r")
  2295. .help(SHORT)
  2296. .long_help(LONG)
  2297. .allow_leading_hyphen();
  2298. args.push(arg);
  2299. }
  2300. fn flag_search_zip(args: &mut Vec<RGArg>) {
  2301. const SHORT: &str = "Search in compressed files.";
  2302. const LONG: &str = long!(
  2303. "\
  2304. Search in compressed files. Currently gzip, bzip2, xz, LZ4, LZMA, Brotli and
  2305. Zstd files are supported. This option expects the decompression binaries to be
  2306. available in your PATH.
  2307. This flag can be disabled with --no-search-zip.
  2308. "
  2309. );
  2310. let arg = RGArg::switch("search-zip")
  2311. .short("z")
  2312. .help(SHORT)
  2313. .long_help(LONG)
  2314. .overrides("no-search-zip")
  2315. .overrides("pre");
  2316. args.push(arg);
  2317. let arg = RGArg::switch("no-search-zip").hidden().overrides("search-zip");
  2318. args.push(arg);
  2319. }
  2320. fn flag_smart_case(args: &mut Vec<RGArg>) {
  2321. const SHORT: &str = "Smart case search.";
  2322. const LONG: &str = long!(
  2323. "\
  2324. Searches case insensitively if the pattern is all lowercase. Search case
  2325. sensitively otherwise.
  2326. This overrides the -s/--case-sensitive and -i/--ignore-case flags.
  2327. "
  2328. );
  2329. let arg = RGArg::switch("smart-case")
  2330. .short("S")
  2331. .help(SHORT)
  2332. .long_help(LONG)
  2333. .overrides("case-sensitive")
  2334. .overrides("ignore-case");
  2335. args.push(arg);
  2336. }
  2337. fn flag_sort_files(args: &mut Vec<RGArg>) {
  2338. const SHORT: &str = "DEPRECATED";
  2339. const LONG: &str = long!(
  2340. "\
  2341. DEPRECATED: Use --sort or --sortr instead.
  2342. Sort results by file path. Note that this currently disables all parallelism
  2343. and runs search in a single thread.
  2344. This flag can be disabled with --no-sort-files.
  2345. "
  2346. );
  2347. let arg = RGArg::switch("sort-files")
  2348. .help(SHORT)
  2349. .long_help(LONG)
  2350. .hidden()
  2351. .overrides("no-sort-files")
  2352. .overrides("sort")
  2353. .overrides("sortr");
  2354. args.push(arg);
  2355. let arg = RGArg::switch("no-sort-files")
  2356. .hidden()
  2357. .overrides("sort-files")
  2358. .overrides("sort")
  2359. .overrides("sortr");
  2360. args.push(arg);
  2361. }
  2362. fn flag_sort(args: &mut Vec<RGArg>) {
  2363. const SHORT: &str =
  2364. "Sort results in ascending order. Implies --threads=1.";
  2365. const LONG: &str = long!(
  2366. "\
  2367. This flag enables sorting of results in ascending order. The possible values
  2368. for this flag are:
  2369. none (Default) Do not sort results. Fastest. Can be multi-threaded.
  2370. path Sort by file path. Always single-threaded.
  2371. modified Sort by the last modified time on a file. Always single-threaded.
  2372. accessed Sort by the last accessed time on a file. Always single-threaded.
  2373. created Sort by the creation time on a file. Always single-threaded.
  2374. If the chosen (manually or by-default) sorting criteria isn't available on your
  2375. system (for example, creation time is not available on ext4 file systems), then
  2376. ripgrep will attempt to detect this, print an error and exit without searching.
  2377. To sort results in reverse or descending order, use the --sortr flag. Also,
  2378. this flag overrides --sortr.
  2379. Note that sorting results currently always forces ripgrep to abandon
  2380. parallelism and run in a single thread.
  2381. "
  2382. );
  2383. let arg = RGArg::flag("sort", "SORTBY")
  2384. .help(SHORT)
  2385. .long_help(LONG)
  2386. .possible_values(&["path", "modified", "accessed", "created", "none"])
  2387. .overrides("sortr")
  2388. .overrides("sort-files")
  2389. .overrides("no-sort-files");
  2390. args.push(arg);
  2391. }
  2392. fn flag_sortr(args: &mut Vec<RGArg>) {
  2393. const SHORT: &str =
  2394. "Sort results in descending order. Implies --threads=1.";
  2395. const LONG: &str = long!(
  2396. "\
  2397. This flag enables sorting of results in descending order. The possible values
  2398. for this flag are:
  2399. none (Default) Do not sort results. Fastest. Can be multi-threaded.
  2400. path Sort by file path. Always single-threaded.
  2401. modified Sort by the last modified time on a file. Always single-threaded.
  2402. accessed Sort by the last accessed time on a file. Always single-threaded.
  2403. created Sort by the creation time on a file. Always single-threaded.
  2404. If the chosen (manually or by-default) sorting criteria isn't available on your
  2405. system (for example, creation time is not available on ext4 file systems), then
  2406. ripgrep will attempt to detect this, print an error and exit without searching.
  2407. To sort results in ascending order, use the --sort flag. Also, this flag
  2408. overrides --sort.
  2409. Note that sorting results currently always forces ripgrep to abandon
  2410. parallelism and run in a single thread.
  2411. "
  2412. );
  2413. let arg = RGArg::flag("sortr", "SORTBY")
  2414. .help(SHORT)
  2415. .long_help(LONG)
  2416. .possible_values(&["path", "modified", "accessed", "created", "none"])
  2417. .overrides("sort")
  2418. .overrides("sort-files")
  2419. .overrides("no-sort-files");
  2420. args.push(arg);
  2421. }
  2422. fn flag_stats(args: &mut Vec<RGArg>) {
  2423. const SHORT: &str = "Print statistics about this ripgrep search.";
  2424. const LONG: &str = long!(
  2425. "\
  2426. Print aggregate statistics about this ripgrep search. When this flag is
  2427. present, ripgrep will print the following stats to stdout at the end of the
  2428. search: number of matched lines, number of files with matches, number of files
  2429. searched, and the time taken for the entire search to complete.
  2430. This set of aggregate statistics may expand over time.
  2431. Note that this flag has no effect if --files, --files-with-matches or
  2432. --files-without-match is passed.
  2433. This flag can be disabled with --no-stats.
  2434. "
  2435. );
  2436. let arg = RGArg::switch("stats")
  2437. .help(SHORT)
  2438. .long_help(LONG)
  2439. .overrides("no-stats");
  2440. args.push(arg);
  2441. let arg = RGArg::switch("no-stats").hidden().overrides("stats");
  2442. args.push(arg);
  2443. }
  2444. fn flag_text(args: &mut Vec<RGArg>) {
  2445. const SHORT: &str = "Search binary files as if they were text.";
  2446. const LONG: &str = long!(
  2447. "\
  2448. Search binary files as if they were text. When this flag is present, ripgrep's
  2449. binary file detection is disabled. This means that when a binary file is
  2450. searched, its contents may be printed if there is a match. This may cause
  2451. escape codes to be printed that alter the behavior of your terminal.
  2452. When binary file detection is enabled it is imperfect. In general, it uses
  2453. a simple heuristic. If a NUL byte is seen during search, then the file is
  2454. considered binary and search stops (unless this flag is present).
  2455. Alternatively, if the '--binary' flag is used, then ripgrep will only quit
  2456. when it sees a NUL byte after it sees a match (or searches the entire file).
  2457. This flag can be disabled with '--no-text'. It overrides the '--binary' flag.
  2458. "
  2459. );
  2460. let arg = RGArg::switch("text")
  2461. .short("a")
  2462. .help(SHORT)
  2463. .long_help(LONG)
  2464. .overrides("no-text")
  2465. .overrides("binary")
  2466. .overrides("no-binary");
  2467. args.push(arg);
  2468. let arg = RGArg::switch("no-text")
  2469. .hidden()
  2470. .overrides("text")
  2471. .overrides("binary")
  2472. .overrides("no-binary");
  2473. args.push(arg);
  2474. }
  2475. fn flag_threads(args: &mut Vec<RGArg>) {
  2476. const SHORT: &str = "The approximate number of threads to use.";
  2477. const LONG: &str = long!(
  2478. "\
  2479. The approximate number of threads to use. A value of 0 (which is the default)
  2480. causes ripgrep to choose the thread count using heuristics.
  2481. "
  2482. );
  2483. let arg =
  2484. RGArg::flag("threads", "NUM").short("j").help(SHORT).long_help(LONG);
  2485. args.push(arg);
  2486. }
  2487. fn flag_trim(args: &mut Vec<RGArg>) {
  2488. const SHORT: &str = "Trim prefixed whitespace from matches.";
  2489. const LONG: &str = long!(
  2490. "\
  2491. When set, all ASCII whitespace at the beginning of each line printed will be
  2492. trimmed.
  2493. This flag can be disabled with --no-trim.
  2494. "
  2495. );
  2496. let arg =
  2497. RGArg::switch("trim").help(SHORT).long_help(LONG).overrides("no-trim");
  2498. args.push(arg);
  2499. let arg = RGArg::switch("no-trim").hidden().overrides("trim");
  2500. args.push(arg);
  2501. }
  2502. fn flag_type(args: &mut Vec<RGArg>) {
  2503. const SHORT: &str = "Only search files matching TYPE.";
  2504. const LONG: &str = long!(
  2505. "\
  2506. Only search files matching TYPE. Multiple type flags may be provided. Use the
  2507. --type-list flag to list all available types.
  2508. This flag supports the special value 'all', which will behave as if --type
  2509. was provided for every file type supported by ripgrep (including any custom
  2510. file types). The end result is that '--type all' causes ripgrep to search in
  2511. \"whitelist\" mode, where it will only search files it recognizes via its type
  2512. definitions.
  2513. "
  2514. );
  2515. let arg = RGArg::flag("type", "TYPE")
  2516. .short("t")
  2517. .help(SHORT)
  2518. .long_help(LONG)
  2519. .multiple();
  2520. args.push(arg);
  2521. }
  2522. fn flag_type_add(args: &mut Vec<RGArg>) {
  2523. const SHORT: &str = "Add a new glob for a file type.";
  2524. const LONG: &str = long!(
  2525. "\
  2526. Add a new glob for a particular file type. Only one glob can be added at a
  2527. time. Multiple --type-add flags can be provided. Unless --type-clear is used,
  2528. globs are added to any existing globs defined inside of ripgrep.
  2529. Note that this MUST be passed to every invocation of ripgrep. Type settings are
  2530. NOT persisted. See CONFIGURATION FILES for a workaround.
  2531. Example:
  2532. rg --type-add 'foo:*.foo' -tfoo PATTERN.
  2533. --type-add can also be used to include rules from other types with the special
  2534. include directive. The include directive permits specifying one or more other
  2535. type names (separated by a comma) that have been defined and its rules will
  2536. automatically be imported into the type specified. For example, to create a
  2537. type called src that matches C++, Python and Markdown files, one can use:
  2538. --type-add 'src:include:cpp,py,md'
  2539. Additional glob rules can still be added to the src type by using the
  2540. --type-add flag again:
  2541. --type-add 'src:include:cpp,py,md' --type-add 'src:*.foo'
  2542. Note that type names must consist only of Unicode letters or numbers.
  2543. Punctuation characters are not allowed.
  2544. "
  2545. );
  2546. let arg = RGArg::flag("type-add", "TYPE_SPEC")
  2547. .help(SHORT)
  2548. .long_help(LONG)
  2549. .multiple();
  2550. args.push(arg);
  2551. }
  2552. fn flag_type_clear(args: &mut Vec<RGArg>) {
  2553. const SHORT: &str = "Clear globs for a file type.";
  2554. const LONG: &str = long!(
  2555. "\
  2556. Clear the file type globs previously defined for TYPE. This only clears the
  2557. default type definitions that are found inside of ripgrep.
  2558. Note that this MUST be passed to every invocation of ripgrep. Type settings are
  2559. NOT persisted. See CONFIGURATION FILES for a workaround.
  2560. "
  2561. );
  2562. let arg = RGArg::flag("type-clear", "TYPE")
  2563. .help(SHORT)
  2564. .long_help(LONG)
  2565. .multiple();
  2566. args.push(arg);
  2567. }
  2568. fn flag_type_not(args: &mut Vec<RGArg>) {
  2569. const SHORT: &str = "Do not search files matching TYPE.";
  2570. const LONG: &str = long!(
  2571. "\
  2572. Do not search files matching TYPE. Multiple type-not flags may be provided. Use
  2573. the --type-list flag to list all available types.
  2574. "
  2575. );
  2576. let arg = RGArg::flag("type-not", "TYPE")
  2577. .short("T")
  2578. .help(SHORT)
  2579. .long_help(LONG)
  2580. .multiple();
  2581. args.push(arg);
  2582. }
  2583. fn flag_type_list(args: &mut Vec<RGArg>) {
  2584. const SHORT: &str = "Show all supported file types.";
  2585. const LONG: &str = long!(
  2586. "\
  2587. Show all supported file types and their corresponding globs.
  2588. "
  2589. );
  2590. let arg = RGArg::switch("type-list")
  2591. .help(SHORT)
  2592. .long_help(LONG)
  2593. // This also technically conflicts with PATTERN, but the first file
  2594. // path will actually be in PATTERN.
  2595. .conflicts(&["file", "files", "pattern", "regexp"]);
  2596. args.push(arg);
  2597. }
  2598. fn flag_unrestricted(args: &mut Vec<RGArg>) {
  2599. const SHORT: &str = "Reduce the level of \"smart\" searching.";
  2600. const LONG: &str = long!(
  2601. "\
  2602. Reduce the level of \"smart\" searching. A single -u won't respect .gitignore
  2603. (etc.) files. Two -u flags will additionally search hidden files and
  2604. directories. Three -u flags will additionally search binary files.
  2605. 'rg -uuu' is roughly equivalent to 'grep -r'.
  2606. "
  2607. );
  2608. let arg = RGArg::switch("unrestricted")
  2609. .short("u")
  2610. .help(SHORT)
  2611. .long_help(LONG)
  2612. .multiple();
  2613. args.push(arg);
  2614. }
  2615. fn flag_vimgrep(args: &mut Vec<RGArg>) {
  2616. const SHORT: &str = "Show results in vim compatible format.";
  2617. const LONG: &str = long!(
  2618. "\
  2619. Show results with every match on its own line, including line numbers and
  2620. column numbers. With this option, a line with more than one match will be
  2621. printed more than once.
  2622. "
  2623. );
  2624. let arg = RGArg::switch("vimgrep").help(SHORT).long_help(LONG);
  2625. args.push(arg);
  2626. }
  2627. fn flag_with_filename(args: &mut Vec<RGArg>) {
  2628. const SHORT: &str = "Print the file path with the matched lines.";
  2629. const LONG: &str = long!(
  2630. "\
  2631. Display the file path for matches. This is the default when more than one
  2632. file is searched. If --heading is enabled (the default when printing to a
  2633. terminal), the file path will be shown above clusters of matches from each
  2634. file; otherwise, the file name will be shown as a prefix for each matched line.
  2635. This flag overrides --no-filename.
  2636. "
  2637. );
  2638. let arg = RGArg::switch("with-filename")
  2639. .short("H")
  2640. .help(SHORT)
  2641. .long_help(LONG)
  2642. .overrides("no-filename");
  2643. args.push(arg);
  2644. const NO_SHORT: &str = "Never print the file path with the matched lines.";
  2645. const NO_LONG: &str = long!(
  2646. "\
  2647. Never print the file path with the matched lines. This is the default when
  2648. ripgrep is explicitly instructed to search one file or stdin.
  2649. This flag overrides --with-filename.
  2650. "
  2651. );
  2652. let arg = RGArg::switch("no-filename")
  2653. .short("I")
  2654. .help(NO_SHORT)
  2655. .long_help(NO_LONG)
  2656. .overrides("with-filename");
  2657. args.push(arg);
  2658. }
  2659. fn flag_word_regexp(args: &mut Vec<RGArg>) {
  2660. const SHORT: &str = "Only show matches surrounded by word boundaries.";
  2661. const LONG: &str = long!(
  2662. "\
  2663. Only show matches surrounded by word boundaries. This is roughly equivalent to
  2664. putting \\b before and after all of the search patterns.
  2665. This overrides the --line-regexp flag.
  2666. "
  2667. );
  2668. let arg = RGArg::switch("word-regexp")
  2669. .short("w")
  2670. .help(SHORT)
  2671. .long_help(LONG)
  2672. .overrides("line-regexp");
  2673. args.push(arg);
  2674. }