PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/rust/clap/v3/crate/benches/05_ripgrep.rs

https://github.com/chromium/chromium
Rust | 952 lines | 904 code | 27 blank | 21 comment | 0 complexity | 394518d953e5eeb9cfb21c11292fff91 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Used to simulate a fairly large number of options/flags and parsing with thousands of positional
  2. // args
  3. //
  4. // CLI used is adapted from ripgrep 48a8a3a691220f9e5b2b08f4051abe8655ea7e8a
  5. use clap::{Arg, Command};
  6. use criterion::{criterion_group, criterion_main, Criterion};
  7. use std::collections::HashMap;
  8. use std::io::Cursor;
  9. use lazy_static::lazy_static;
  10. pub fn build_rg_with_short_help(c: &mut Criterion) {
  11. c.bench_function("build_rg_with_short_help", |b| b.iter(app_short));
  12. }
  13. pub fn build_rg_with_long_help(c: &mut Criterion) {
  14. c.bench_function("build_rg_with_long_help", |b| b.iter(app_long));
  15. }
  16. pub fn write_rg_short_help(c: &mut Criterion) {
  17. let mut cmd = app_short();
  18. c.bench_function("write_rg_short_help", |b| b.iter(|| build_help(&mut cmd)));
  19. }
  20. pub fn write_rg_long_help(c: &mut Criterion) {
  21. let mut cmd = app_long();
  22. c.bench_function("write_rg_long_help", |b| b.iter(|| build_help(&mut cmd)));
  23. }
  24. pub fn parse_rg(c: &mut Criterion) {
  25. c.bench_function("parse_rg", |b| {
  26. b.iter(|| app_short().get_matches_from(vec!["rg", "pat"]))
  27. });
  28. }
  29. pub fn parse_rg_with_complex(c: &mut Criterion) {
  30. c.bench_function("parse_rg_with_complex", |b| {
  31. b.iter(|| {
  32. app_short().get_matches_from(vec![
  33. "rg",
  34. "pat",
  35. "-cFlN",
  36. "-pqr=some",
  37. "--null",
  38. "--no-filename",
  39. "--no-messages",
  40. "-SH",
  41. "-C5",
  42. "--follow",
  43. "-e some",
  44. ])
  45. })
  46. });
  47. }
  48. pub fn parse_rg_with_lots(c: &mut Criterion) {
  49. c.bench_function("parse_rg_with_lots", |b| {
  50. b.iter(|| {
  51. app_short().get_matches_from(vec![
  52. "rg", "pat", "some", "some", "some", "some", "some", "some", "some", "some",
  53. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  54. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  55. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  56. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  57. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  58. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  59. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  60. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  61. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  62. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  63. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  64. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  65. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  66. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  67. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  68. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  69. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  70. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  71. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  72. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  73. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  74. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  75. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  76. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  77. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  78. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  79. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  80. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  81. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  82. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  83. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  84. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  85. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  86. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  87. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  88. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  89. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  90. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  91. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  92. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  93. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  94. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  95. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  96. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  97. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  98. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  99. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  100. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  101. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  102. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  103. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  104. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  105. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  106. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  107. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  108. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  109. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  110. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  111. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  112. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  113. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  114. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  115. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  116. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  117. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  118. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  119. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  120. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  121. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  122. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  123. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  124. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  125. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  126. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  127. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  128. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  129. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  130. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  131. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  132. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  133. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  134. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  135. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  136. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  137. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  138. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  139. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  140. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  141. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  142. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  143. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  144. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  145. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  146. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  147. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  148. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  149. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  150. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  151. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  152. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  153. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  154. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  155. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  156. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  157. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  158. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  159. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  160. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  161. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  162. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  163. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  164. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  165. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  166. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  167. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  168. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  169. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  170. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  171. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  172. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  173. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  174. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  175. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  176. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  177. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  178. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  179. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  180. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  181. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  182. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  183. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  184. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  185. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  186. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  187. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  188. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  189. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  190. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  191. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  192. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  193. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  194. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  195. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  196. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  197. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  198. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  199. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  200. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  201. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  202. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  203. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  204. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  205. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  206. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  207. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  208. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  209. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  210. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  211. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  212. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  213. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  214. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  215. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  216. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  217. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  218. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  219. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  220. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  221. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  222. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  223. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  224. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  225. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  226. "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
  227. "some", "some", "some", "some", "some", "some",
  228. ])
  229. })
  230. });
  231. }
  232. const ABOUT: &str = "
  233. ripgrep (rg) recursively searches your current directory for a regex pattern.
  234. ripgrep's regex engine uses finite automata and guarantees linear time
  235. searching. Because of this, features like backreferences and arbitrary
  236. lookaround are not supported.
  237. Project home page: https://github.com/BurntSushi/ripgrep
  238. Use -h for short descriptions and --help for more details.";
  239. const USAGE: &str = "
  240. rg [OPTIONS] <pattern> [<path> ...]
  241. rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
  242. rg [OPTIONS] --files [<path> ...]
  243. rg [OPTIONS] --type-list";
  244. const TEMPLATE: &str = "\
  245. {bin} {version}
  246. {author}
  247. {about}
  248. USAGE:{usage}
  249. ARGS:
  250. {positionals}
  251. OPTIONS:
  252. {options}";
  253. /// Build a clap application with short help strings.
  254. fn app_short() -> Command<'static> {
  255. cmd(false, |k| USAGES[k].short)
  256. }
  257. /// Build a clap application with long help strings.
  258. fn app_long() -> Command<'static> {
  259. cmd(true, |k| USAGES[k].long)
  260. }
  261. /// Build the help text of an application.
  262. fn build_help(cmd: &mut Command) -> String {
  263. let mut buf = Cursor::new(Vec::with_capacity(50));
  264. cmd.write_help(&mut buf).unwrap();
  265. let content = buf.into_inner();
  266. String::from_utf8(content).unwrap()
  267. }
  268. /// Build a clap application parameterized by usage strings.
  269. ///
  270. /// The function given should take a clap argument name and return a help
  271. /// string. `cmd` will panic if a usage string is not defined.
  272. ///
  273. /// This is an intentionally stand-alone module so that it can be used easily
  274. /// in a `build.rs` script to build shell completion files.
  275. fn cmd<F>(_next_line_help: bool, doc: F) -> Command<'static>
  276. where
  277. F: Fn(&'static str) -> &'static str,
  278. {
  279. let arg = |name| Arg::new(name).help(doc(name));
  280. let flag = |name| arg(name).long(name);
  281. Command::new("ripgrep")
  282. .author("BurntSushi") // simulating since it's only a bench
  283. .version("0.4.0") // Simulating
  284. .about(ABOUT)
  285. .max_term_width(100)
  286. .override_usage(USAGE)
  287. .help_template(TEMPLATE)
  288. // Handle help/version manually to make their output formatting
  289. // consistent with short/long views.
  290. .arg(arg("help-short").short('h'))
  291. .arg(flag("help"))
  292. .arg(flag("version").short('V'))
  293. // First, set up primary positional/flag arguments.
  294. .arg(arg("pattern").required_unless_present_any(&[
  295. "file",
  296. "files",
  297. "help-short",
  298. "help",
  299. "regexp",
  300. "type-list",
  301. "version",
  302. ]))
  303. .arg(
  304. arg("path")
  305. .takes_value(true)
  306. .multiple_values(true)
  307. .multiple_occurrences(true),
  308. )
  309. .arg(
  310. flag("regexp")
  311. .short('e')
  312. .allow_hyphen_values(true)
  313. .multiple_occurrences(true)
  314. .takes_value(true)
  315. .value_name("pattern"),
  316. )
  317. .arg(
  318. flag("files")
  319. // This should also conflict with `pattern`, but the first file
  320. // path will actually be in `pattern`.
  321. .conflicts_with_all(&["file", "regexp", "type-list"]),
  322. )
  323. .arg(flag("type-list").conflicts_with_all(&["file", "files", "pattern", "regexp"]))
  324. // Second, set up common flags.
  325. .arg(flag("text").short('a'))
  326. .arg(flag("count").short('c'))
  327. .arg(
  328. flag("color")
  329. .value_name("WHEN")
  330. .takes_value(true)
  331. .hide_possible_values(true)
  332. .possible_values(["never", "auto", "always", "ansi"]),
  333. )
  334. .arg(
  335. flag("colors")
  336. .value_name("SPEC")
  337. .multiple_occurrences(true)
  338. .takes_value(true),
  339. )
  340. .arg(flag("fixed-strings").short('F'))
  341. .arg(
  342. flag("glob")
  343. .short('g')
  344. .multiple_occurrences(true)
  345. .takes_value(true)
  346. .value_name("GLOB"),
  347. )
  348. .arg(flag("ignore-case").short('i'))
  349. .arg(flag("line-number").short('n'))
  350. .arg(flag("no-line-number").short('N'))
  351. .arg(flag("quiet").short('q'))
  352. .arg(
  353. flag("type")
  354. .short('t')
  355. .multiple_occurrences(true)
  356. .takes_value(true)
  357. .value_name("TYPE"),
  358. )
  359. .arg(
  360. flag("type-not")
  361. .short('T')
  362. .multiple_occurrences(true)
  363. .takes_value(true)
  364. .value_name("TYPE"),
  365. )
  366. .arg(flag("unrestricted").short('u').multiple_occurrences(true))
  367. .arg(flag("invert-match").short('v'))
  368. .arg(flag("word-regexp").short('w'))
  369. // Third, set up less common flags.
  370. .arg(
  371. flag("after-context")
  372. .short('A')
  373. .value_name("NUM")
  374. .validator(validate_number),
  375. )
  376. .arg(
  377. flag("before-context")
  378. .short('B')
  379. .value_name("NUM")
  380. .validator(validate_number),
  381. )
  382. .arg(
  383. flag("context")
  384. .short('C')
  385. .value_name("NUM")
  386. .validator(validate_number),
  387. )
  388. .arg(flag("column"))
  389. .arg(flag("context-separator").value_name("SEPARATOR"))
  390. .arg(flag("debug"))
  391. .arg(
  392. flag("file")
  393. .short('f')
  394. .value_name("FILE")
  395. .multiple_occurrences(true),
  396. )
  397. .arg(flag("files-with-matches").short('l'))
  398. .arg(flag("files-without-match"))
  399. .arg(flag("with-filename").short('H'))
  400. .arg(flag("no-filename"))
  401. .arg(flag("heading").overrides_with("no-heading"))
  402. .arg(flag("no-heading").overrides_with("heading"))
  403. .arg(flag("hidden"))
  404. .arg(
  405. flag("ignore-file")
  406. .value_name("FILE")
  407. .multiple_occurrences(true),
  408. )
  409. .arg(flag("follow").short('L'))
  410. .arg(
  411. flag("max-count")
  412. .short('m')
  413. .value_name("NUM")
  414. .validator(validate_number),
  415. )
  416. .arg(
  417. flag("maxdepth")
  418. .value_name("NUM")
  419. .validator(validate_number),
  420. )
  421. .arg(flag("mmap"))
  422. .arg(flag("no-messages"))
  423. .arg(flag("no-mmap"))
  424. .arg(flag("no-ignore"))
  425. .arg(flag("no-ignore-parent"))
  426. .arg(flag("no-ignore-vcs"))
  427. .arg(flag("null"))
  428. .arg(flag("path-separator").value_name("SEPARATOR"))
  429. .arg(flag("pretty").short('p'))
  430. .arg(flag("replace").short('r').value_name("ARG"))
  431. .arg(flag("case-sensitive").short('s'))
  432. .arg(flag("smart-case").short('S'))
  433. .arg(flag("sort-files"))
  434. .arg(
  435. flag("threads")
  436. .short('j')
  437. .value_name("ARG")
  438. .validator(validate_number),
  439. )
  440. .arg(flag("vimgrep"))
  441. .arg(
  442. flag("type-add")
  443. .value_name("TYPE")
  444. .multiple_occurrences(true),
  445. )
  446. .arg(
  447. flag("type-clear")
  448. .value_name("TYPE")
  449. .multiple_occurrences(true),
  450. )
  451. }
  452. struct Usage {
  453. short: &'static str,
  454. long: &'static str,
  455. }
  456. macro_rules! doc {
  457. ($map:expr, $name:expr, $short:expr) => {
  458. doc!($map, $name, $short, $short)
  459. };
  460. ($map:expr, $name:expr, $short:expr, $long:expr) => {
  461. $map.insert(
  462. $name,
  463. Usage {
  464. short: $short,
  465. long: concat!($long, "\n "),
  466. },
  467. );
  468. };
  469. }
  470. lazy_static! {
  471. static ref USAGES: HashMap<&'static str, Usage> = {
  472. let mut h = HashMap::new();
  473. doc!(
  474. h,
  475. "help-short",
  476. "Show short help output.",
  477. "Show short help output. Use --help to show more details."
  478. );
  479. doc!(
  480. h,
  481. "help",
  482. "Show verbose help output.",
  483. "When given, more details about flags are provided."
  484. );
  485. doc!(h, "version", "Print version information.");
  486. doc!(
  487. h,
  488. "pattern",
  489. "A regular expression used for searching.",
  490. "A regular expression used for searching. Multiple patterns \
  491. may be given. To match a pattern beginning with a -, use [-]."
  492. );
  493. doc!(
  494. h,
  495. "regexp",
  496. "A regular expression used for searching.",
  497. "A regular expression used for searching. Multiple patterns \
  498. may be given. To match a pattern beginning with a -, use [-]."
  499. );
  500. doc!(
  501. h,
  502. "path",
  503. "A file or directory to search.",
  504. "A file or directory to search. Directories are searched \
  505. recursively."
  506. );
  507. doc!(
  508. h,
  509. "files",
  510. "Print each file that would be searched.",
  511. "Print each file that would be searched without actually \
  512. performing the search. This is useful to determine whether a \
  513. particular file is being searched or not."
  514. );
  515. doc!(
  516. h,
  517. "type-list",
  518. "Show all supported file types.",
  519. "Show all supported file types and their corresponding globs."
  520. );
  521. doc!(h, "text", "Search binary files as if they were text.");
  522. doc!(h, "count", "Only show count of matches for each file.");
  523. doc!(
  524. h,
  525. "color",
  526. "When to use color. [default: auto]",
  527. "When to use color in the output. The possible values are \
  528. never, auto, always or ansi. The default is auto. When always \
  529. is used, coloring is attempted based on your environment. When \
  530. ansi used, coloring is forcefully done using ANSI escape color \
  531. codes."
  532. );
  533. doc!(
  534. h,
  535. "colors",
  536. "Configure color settings and styles.",
  537. "This flag specifies color settings for use in the output. \
  538. This flag may be provided multiple times. Settings are applied \
  539. iteratively. Colors are limited to one of eight choices: \
  540. red, blue, green, cyan, magenta, yellow, white and black. \
  541. Styles are limited to nobold, bold, nointense or intense.\n\n\
  542. The format of the flag is {type}:{attribute}:{value}. {type} \
  543. should be one of path, line or match. {attribute} can be fg, bg \
  544. or style. {value} is either a color (for fg and bg) or a text \
  545. style. A special format, {type}:none, will clear all color \
  546. settings for {type}.\n\nFor example, the following command will \
  547. change the match color to magenta and the background color for \
  548. line numbers to yellow:\n\n\
  549. rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo."
  550. );
  551. doc!(
  552. h,
  553. "fixed-strings",
  554. "Treat the pattern as a literal string.",
  555. "Treat the pattern as a literal string instead of a regular \
  556. expression. When this flag is used, special regular expression \
  557. meta characters such as (){}*+. do not need to be escaped."
  558. );
  559. doc!(
  560. h,
  561. "glob",
  562. "Include or exclude files/directories.",
  563. "Include or exclude files/directories for searching that \
  564. match the given glob. This always overrides any other \
  565. ignore logic. Multiple glob flags may be used. Globbing \
  566. rules match .gitignore globs. Precede a glob with a ! \
  567. to exclude it."
  568. );
  569. doc!(
  570. h,
  571. "ignore-case",
  572. "Case insensitive search.",
  573. "Case insensitive search. This is overridden by \
  574. --case-sensitive."
  575. );
  576. doc!(
  577. h,
  578. "line-number",
  579. "Show line numbers.",
  580. "Show line numbers (1-based). This is enabled by default when \
  581. searching in a tty."
  582. );
  583. doc!(
  584. h,
  585. "no-line-number",
  586. "Suppress line numbers.",
  587. "Suppress line numbers. This is enabled by default when NOT \
  588. searching in a tty."
  589. );
  590. doc!(
  591. h,
  592. "quiet",
  593. "Do not print anything to stdout.",
  594. "Do not print anything to stdout. If a match is found in a file, \
  595. stop searching. This is useful when ripgrep is used only for \
  596. its exit code."
  597. );
  598. doc!(
  599. h,
  600. "type",
  601. "Only search files matching TYPE.",
  602. "Only search files matching TYPE. Multiple type flags may be \
  603. provided. Use the --type-list flag to list all available \
  604. types."
  605. );
  606. doc!(
  607. h,
  608. "type-not",
  609. "Do not search files matching TYPE.",
  610. "Do not search files matching TYPE. Multiple type-not flags may \
  611. be provided. Use the --type-list flag to list all available \
  612. types."
  613. );
  614. doc!(
  615. h,
  616. "unrestricted",
  617. "Reduce the level of \"smart\" searching.",
  618. "Reduce the level of \"smart\" searching. A single -u \
  619. won't respect .gitignore (etc.) files. Two -u flags will \
  620. additionally search hidden files and directories. Three \
  621. -u flags will additionally search binary files. -uu is \
  622. roughly equivalent to grep -r and -uuu is roughly \
  623. equivalent to grep -a -r."
  624. );
  625. doc!(
  626. h,
  627. "invert-match",
  628. "Invert matching.",
  629. "Invert matching. Show lines that don't match given patterns."
  630. );
  631. doc!(
  632. h,
  633. "word-regexp",
  634. "Only show matches surrounded by word boundaries.",
  635. "Only show matches surrounded by word boundaries. This is \
  636. equivalent to putting \\b before and after all of the search \
  637. patterns."
  638. );
  639. doc!(h, "after-context", "Show NUM lines after each match.");
  640. doc!(h, "before-context", "Show NUM lines before each match.");
  641. doc!(h, "context", "Show NUM lines before and after each match.");
  642. doc!(
  643. h,
  644. "column",
  645. "Show column numbers",
  646. "Show column numbers (1-based). This only shows the column \
  647. numbers for the first match on each line. This does not try \
  648. to account for Unicode. One byte is equal to one column. This \
  649. implies --line-number."
  650. );
  651. doc!(
  652. h,
  653. "context-separator",
  654. "Set the context separator string. [default: --]",
  655. "The string used to separate non-contiguous context lines in the \
  656. output. Escape sequences like \\x7F or \\t may be used. The \
  657. default value is --."
  658. );
  659. doc!(
  660. h,
  661. "debug",
  662. "Show debug messages.",
  663. "Show debug messages. Please use this when filing a bug report."
  664. );
  665. doc!(
  666. h,
  667. "file",
  668. "Search for patterns from the given file.",
  669. "Search for patterns from the given file, with one pattern per \
  670. line. When this flag is used or multiple times or in \
  671. combination with the -e/--regexp flag, then all patterns \
  672. provided are searched. Empty pattern lines will match all input \
  673. lines, and the newline is not counted as part of the pattern."
  674. );
  675. doc!(
  676. h,
  677. "files-with-matches",
  678. "Only show the path of each file with at least one match."
  679. );
  680. doc!(
  681. h,
  682. "files-without-match",
  683. "Only show the path of each file that contains zero matches."
  684. );
  685. doc!(
  686. h,
  687. "with-filename",
  688. "Show file name for each match.",
  689. "Prefix each match with the file name that contains it. This is \
  690. the default when more than one file is searched."
  691. );
  692. doc!(
  693. h,
  694. "no-filename",
  695. "Never show the file name for a match.",
  696. "Never show the file name for a match. This is the default when \
  697. one file is searched."
  698. );
  699. doc!(
  700. h,
  701. "heading",
  702. "Show matches grouped by each file.",
  703. "This shows the file name above clusters of matches from each \
  704. file instead of showing the file name for every match. This is \
  705. the default mode at a tty."
  706. );
  707. doc!(
  708. h,
  709. "no-heading",
  710. "Don't group matches by each file.",
  711. "Don't group matches by each file. If -H/--with-filename is \
  712. enabled, then file names will be shown for every line matched. \
  713. This is the default mode when not at a tty."
  714. );
  715. doc!(
  716. h,
  717. "hidden",
  718. "Search hidden files and directories.",
  719. "Search hidden files and directories. By default, hidden files \
  720. and directories are skipped."
  721. );
  722. doc!(
  723. h,
  724. "ignore-file",
  725. "Specify additional ignore files.",
  726. "Specify additional ignore files for filtering file paths. \
  727. Ignore files should be in the gitignore format and are matched \
  728. relative to the current working directory. These ignore files \
  729. have lower precedence than all other ignore files. When \
  730. specifying multiple ignore files, earlier files have lower \
  731. precedence than later files."
  732. );
  733. doc!(h, "follow", "Follow symbolic links.");
  734. doc!(
  735. h,
  736. "max-count",
  737. "Limit the number of matches.",
  738. "Limit the number of matching lines per file searched to NUM."
  739. );
  740. doc!(
  741. h,
  742. "maxdepth",
  743. "Descend at most NUM directories.",
  744. "Limit the depth of directory traversal to NUM levels beyond \
  745. the paths given. A value of zero only searches the \
  746. starting-points themselves.\n\nFor example, \
  747. 'rg --maxdepth 0 dir/' is a no-op because dir/ will not be \
  748. descended into. 'rg --maxdepth 1 dir/' will search only the \
  749. direct children of dir/."
  750. );
  751. doc!(
  752. h,
  753. "mmap",
  754. "Searching using memory maps when possible.",
  755. "Search using memory maps when possible. This is enabled by \
  756. default when ripgrep thinks it will be faster. Note that memory \
  757. map searching doesn't currently support all options, so if an \
  758. incompatible option (e.g., --context) is given with --mmap, \
  759. then memory maps will not be used."
  760. );
  761. doc!(
  762. h,
  763. "no-messages",
  764. "Suppress all error messages.",
  765. "Suppress all error messages. This is equivalent to redirecting \
  766. stderr to /dev/null."
  767. );
  768. doc!(
  769. h,
  770. "no-mmap",
  771. "Never use memory maps.",
  772. "Never use memory maps, even when they might be faster."
  773. );
  774. doc!(
  775. h,
  776. "no-ignore",
  777. "Don't respect ignore files.",
  778. "Don't respect ignore files (.gitignore, .ignore, etc.). This \
  779. implies --no-ignore-parent and --no-ignore-vcs."
  780. );
  781. doc!(
  782. h,
  783. "no-ignore-parent",
  784. "Don't respect ignore files in parent directories.",
  785. "Don't respect ignore files (.gitignore, .ignore, etc.) in \
  786. parent directories."
  787. );
  788. doc!(
  789. h,
  790. "no-ignore-vcs",
  791. "Don't respect VCS ignore files",
  792. "Don't respect version control ignore files (.gitignore, etc.). \
  793. This implies --no-ignore-parent. Note that .ignore files will \
  794. continue to be respected."
  795. );
  796. doc!(
  797. h,
  798. "null",
  799. "Print NUL byte after file names",
  800. "Whenever a file name is printed, follow it with a NUL byte. \
  801. This includes printing file names before matches, and when \
  802. printing a list of matching files such as with --count, \
  803. --files-with-matches and --files. This option is useful for use \
  804. with xargs."
  805. );
  806. doc!(
  807. h,
  808. "path-separator",
  809. "Path separator to use when printing file paths.",
  810. "The path separator to use when printing file paths. This \
  811. defaults to your platform's path separator, which is / on Unix \
  812. and \\ on Windows. This flag is intended for overriding the \
  813. default when the environment demands it (e.g., cygwin). A path \
  814. separator is limited to a single byte."
  815. );
  816. doc!(h, "pretty", "Alias for --color always --heading -n.");
  817. doc!(
  818. h,
  819. "replace",
  820. "Replace matches with string given.",
  821. "Replace every match with the string given when printing \
  822. results. Neither this flag nor any other flag will modify your \
  823. files.\n\nCapture group indices (e.g., $5) and names \
  824. (e.g., $foo) are supported in the replacement string.\n\n\
  825. Note that the replacement by default replaces each match, and \
  826. NOT the entire line. To replace the entire line, you should \
  827. match the entire line."
  828. );
  829. doc!(
  830. h,
  831. "case-sensitive",
  832. "Search case sensitively.",
  833. "Search case sensitively. This overrides -i/--ignore-case and \
  834. -S/--smart-case."
  835. );
  836. doc!(
  837. h,
  838. "smart-case",
  839. "Smart case search.",
  840. "Searches case insensitively if the pattern is all lowercase. \
  841. Search case sensitively otherwise. This is overridden by \
  842. either -s/--case-sensitive or -i/--ignore-case."
  843. );
  844. doc!(
  845. h,
  846. "sort-files",
  847. "Sort results by file path. Implies --threads=1.",
  848. "Sort results by file path. Note that this currently \
  849. disables all parallelism and runs search in a single thread."
  850. );
  851. doc!(
  852. h,
  853. "threads",
  854. "The approximate number of threads to use.",
  855. "The approximate number of threads to use. A value of 0 (which \
  856. is the default) causes ripgrep to choose the thread count \
  857. using heuristics."
  858. );
  859. doc!(
  860. h,
  861. "vimgrep",
  862. "Show results in vim compatible format.",
  863. "Show results with every match on its own line, including \
  864. line numbers and column numbers. With this option, a line with \
  865. more than one match will be printed more than once."
  866. );
  867. doc!(
  868. h,
  869. "type-add",
  870. "Add a new glob for a file type.",
  871. "Add a new glob for a particular file type. Only one glob can be \
  872. added at a time. Multiple --type-add flags can be provided. \
  873. Unless --type-clear is used, globs are added to any existing \
  874. globs defined inside of ripgrep.\n\nNote that this MUST be \
  875. passed to every invocation of ripgrep. Type settings are NOT \
  876. persisted.\n\nExample: \
  877. rg --type-add 'foo:*.foo' -tfoo PATTERN.\n\n\
  878. --type-add can also be used to include rules from other types \
  879. with the special include directive. The include directive \
  880. permits specifying one or more other type names (separated by a \
  881. comma) that have been defined and its rules will automatically \
  882. be imported into the type specified. For example, to create a \
  883. type called src that matches C++, Python and Markdown files, one \
  884. can use:\n\n\
  885. --type-add 'src:include:cpp,py,md'\n\n\
  886. Additional glob rules can still be added to the src type by \
  887. using the --type-add flag again:\n\n\
  888. --type-add 'src:include:cpp,py,md' --type-add 'src:*.foo'\n\n\
  889. Note that type names must consist only of Unicode letters or \
  890. numbers. Punctuation characters are not allowed."
  891. );
  892. doc!(
  893. h,
  894. "type-clear",
  895. "Clear globs for given file type.",
  896. "Clear the file type globs previously defined for TYPE. This \
  897. only clears the default type definitions that are found inside \
  898. of ripgrep.\n\nNote that this MUST be passed to every \
  899. invocation of ripgrep. Type settings are NOT persisted."
  900. );
  901. h
  902. };
  903. }
  904. fn validate_number(s: &str) -> Result<(), String> {
  905. s.parse::<usize>()
  906. .map(|_| ())
  907. .map_err(|err| err.to_string())
  908. }
  909. criterion_group!(
  910. benches,
  911. build_rg_with_short_help,
  912. build_rg_with_long_help,
  913. write_rg_short_help,
  914. write_rg_long_help,
  915. parse_rg,
  916. parse_rg_with_complex,
  917. parse_rg_with_lots
  918. );
  919. criterion_main!(benches);