on 89fcbb2 help sign in
bat/src/clap_app.rs
433 lines 19 kB history raw
1use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, SubCommand};
2use std::path::Path;
4pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
5 let clap_color_setting = if interactive_output {
6 AppSettings::ColoredHelp
7 } else {
8 AppSettings::ColorNever
9 };
11 // Check if the current directory contains a file name cache, if it does
12 // do not make the arguements for subcommand 'cache' required.
13 let arg_group_required = !Path::new("cache").exists();
15 ClapApp::new(crate_name!())
16 .version(crate_version!())
17 .global_setting(clap_color_setting)
18 .global_setting(AppSettings::DeriveDisplayOrder)
19 .global_setting(AppSettings::UnifiedHelpMessage)
20 .global_setting(AppSettings::HidePossibleValuesInHelp)
21 .setting(AppSettings::InferSubcommands)
22 .setting(AppSettings::ArgsNegateSubcommands)
23 .setting(AppSettings::DisableHelpSubcommand)
24 .setting(AppSettings::VersionlessSubcommands)
25 .max_term_width(100)
26 .about(
27 "A cat(1) clone with wings.\n\n\
28 Use '--help' instead of '-h' to see a more detailed version of the help text.",
29 )
30 .long_about("A cat(1) clone with syntax highlighting and Git integration.")
31 .arg(
32 Arg::with_name("FILE")
33 .help("File(s) to print / concatenate. Use '-' for standard input.")
34 .long_help(
35 "File(s) to print / concatenate. Use a dash ('-') or no argument at all \
36 to read from standard input.",
37 )
38 .multiple(true)
39 .empty_values(false),
40 )
41 .arg(
42 Arg::with_name("language")
43 .short("l")
44 .long("language")
45 .overrides_with("language")
46 .help("Set the language for syntax highlighting.")
47 .long_help(
48 "Explicitly set the language for syntax highlighting. The language can be \
49 specified as a name (like 'C++' or 'LaTeX') or possible file extension \
50 (like 'cpp', 'hpp' or 'md'). Use '--list-languages' to show all supported \
51 language names and file extensions.",
52 )
53 .takes_value(true),
54 )
55 .arg(
56 Arg::with_name("list-languages")
57 .long("list-languages")
58 .conflicts_with("list-themes")
59 .help("Display all supported languages.")
60 .long_help("Display a list of supported languages for syntax highlighting."),
61 )
62 .arg(
63 Arg::with_name("map-syntax")
64 .short("m")
65 .long("map-syntax")
66 .multiple(true)
67 .takes_value(true)
68 .number_of_values(1)
69 .value_name("from:to")
70 .help("Map a file extension or name to an existing syntax.")
71 .long_help(
72 "Map a file extension or file name to an existing syntax. For example, \
73 to highlight *.conf files with the INI syntax, use '-m conf:ini'. \
74 To highlight files named '.myignore' with the Git Ignore syntax, use \
75 '-m .myignore:gitignore'.",
76 )
77 .takes_value(true),
78 )
79 .arg(
80 Arg::with_name("theme")
81 .long("theme")
82 .overrides_with("theme")
83 .takes_value(true)
84 .help("Set the color theme for syntax highlighting.")
85 .long_help(
86 "Set the theme for syntax highlighting. Use '--list-themes' to \
87 see all available themes. To set a default theme, add the \
88 '--theme=\"...\"' option to the configuration file or export the \
89 BAT_THEME environment variable (e.g.: export \
90 BAT_THEME=\"...\").",
91 ),
92 )
93 .arg(
94 Arg::with_name("list-themes")
95 .long("list-themes")
96 .help("Display all supported highlighting themes.")
97 .long_help("Display a list of supported themes for syntax highlighting."),
98 )
99 .arg(
100 Arg::with_name("style")
101 .long("style")
102 .value_name("style-components")
103 // Need to turn this off for overrides_with to work as we want. See the bottom most
104 // example at https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.overrides_with
105 .use_delimiter(false)
106 .takes_value(true)
107 .overrides_with("style")
108 .overrides_with("plain")
109 .overrides_with("number")
110 // Cannot use clap's built in validation because we have to turn off clap's delimiters
111 .validator(|val| {
112 let mut invalid_vals = val.split(',').filter(|style| {
113 !&[
114 "auto", "full", "plain", "changes", "header", "grid", "numbers",
115 ]
116 .contains(style)
117 });
119 if let Some(invalid) = invalid_vals.next() {
120 Err(format!("Unknown style, '{}'", invalid))
121 } else {
122 Ok(())
123 }
124 })
125 .help(
126 "Comma-separated list of style elements to display \
127 (*auto*, full, plain, changes, header, grid, numbers).",
128 )
129 .long_help(
130 "Configure which elements (line numbers, file headers, grid \
131 borders, Git modifications, ..) to display in addition to the \
132 file contents. The argument is a comma-separated list of \
133 components to display (e.g. 'numbers,changes,grid') or a \
134 pre-defined style ('full'). To set a default style, add the \
135 '--style=\"..\"' option to the configuration file or export the \
136 BAT_STYLE environment variable (e.g.: export BAT_STYLE=\"..\"). \
137 Possible values: *auto*, full, plain, changes, header, grid, numbers.",
138 ),
139 )
140 .arg(
141 Arg::with_name("plain")
142 .overrides_with("plain")
143 .overrides_with("number")
144 .short("p")
145 .long("plain")
146 .multiple(true)
147 .help("Show plain style (alias for '--style=plain').")
148 .long_help(
149 "Only show plain style, no decorations. This is an alias for \
150 '--style=plain'. When '-p' is used twice ('-pp'), it also disables \
151 automatic paging (alias for '--style=plain --pager=never').",
152 ),
153 )
154 .arg(
155 Arg::with_name("number")
156 .long("number")
157 .overrides_with("number")
158 .short("n")
159 .help("Show line numbers (alias for '--style=numbers').")
160 .long_help(
161 "Only show line numbers, no other decorations. This is an alias for \
162 '--style=numbers'",
163 ),
164 )
165 .arg(
166 Arg::with_name("show-all")
167 .long("show-all")
168 .alias("show-nonprintable")
169 .short("A")
170 .conflicts_with("language")
171 .help("Show non-printable characters (space, tab, newline, ..).")
172 .long_help(
173 "Show non-printable characters like space, tab or newline. \
174 Use '--tabs' to control the width of the tab-placeholders.",
175 ),
176 )
177 .arg(
178 Arg::with_name("line-range")
179 .long("line-range")
180 .short("r")
181 .multiple(true)
182 .takes_value(true)
183 .number_of_values(1)
184 .value_name("N:M")
185 .help("Only print the lines from N to M.")
186 .long_help(
187 "Only print the specified range of lines for each file. \
188 For example:\n \
189 '--line-range 30:40' prints lines 30 to 40\n \
190 '--line-range :40' prints lines 1 to 40\n \
191 '--line-range 40:' prints lines 40 to the end of the file",
192 ),
193 )
194 .arg(
195 Arg::with_name("highlight-line")
196 .long("highlight-line")
197 .short("H")
198 .takes_value(true)
199 .number_of_values(1)
200 .multiple(true)
201 .value_name("N")
202 .help("Highlight the given line.")
203 .long_help(
204 "Highlight the N-th line with a different background color",
205 ),
206 )
207 .arg(
208 Arg::with_name("color")
209 .long("color")
210 .overrides_with("color")
211 .takes_value(true)
212 .value_name("when")
213 .possible_values(&["auto", "never", "always"])
214 .hide_default_value(true)
215 .default_value("auto")
216 .help("When to use colors (*auto*, never, always).")
217 .long_help(
218 "Specify when to use colored output. The automatic mode \
219 only enables colors if an interactive terminal is detected. \
220 Possible values: *auto*, never, always.",
221 ),
222 )
223 .arg(
224 Arg::with_name("italic-text")
225 .long("italic-text")
226 .takes_value(true)
227 .value_name("when")
228 .possible_values(&["always", "never"])
229 .default_value("never")
230 .hide_default_value(true)
231 .help("Use italics in output (always, *never*)")
232 .long_help("Specify when to use ANSI sequences for italic text in the output. Possible values: always, *never*."),
233 )
234 .arg(
235 Arg::with_name("decorations")
236 .long("decorations")
237 .overrides_with("decorations")
238 .takes_value(true)
239 .value_name("when")
240 .possible_values(&["auto", "never", "always"])
241 .default_value("auto")
242 .hide_default_value(true)
243 .help("When to show the decorations (*auto*, never, always).")
244 .long_help(
245 "Specify when to use the decorations that have been specified \
246 via '--style'. The automatic mode only enables decorations if \
247 an interactive terminal is detected. Possible values: *auto*, never, always.",
248 ),
249 )
250 .arg(
251 Arg::with_name("paging")
252 .long("paging")
253 .overrides_with("paging")
254 .takes_value(true)
255 .value_name("when")
256 .possible_values(&["auto", "never", "always"])
257 .default_value("auto")
258 .hide_default_value(true)
259 .help("Specify when to use the pager (*auto*, never, always).")
260 .long_help(
261 "Specify when to use the pager. To control which pager \
262 is used, set the PAGER or BAT_PAGER environment \
263 variables (the latter takes precedence) or use the '--pager' option. \
264 To disable the pager permanently, set BAT_PAGER to an empty string \
265 or set '--paging=never' in the configuration file. \
266 Possible values: *auto*, never, always.",
267 ),
268 )
269 .arg(
270 Arg::with_name("pager")
271 .long("pager")
272 .overrides_with("pager")
273 .takes_value(true)
274 .value_name("command")
275 .hidden_short_help(true)
276 .help("Determine which pager to use.")
277 .long_help(
278 "Determine which pager is used. This option will overwrite \
279 the PAGER and BAT_PAGER environment variables. The default \
280 pager is 'less'. To disable the pager completely, use the \
281 '--paging' option. \
282 Example: '--pager \"less -RF\"'.",
283 ),
284 )
285 .arg(
286 Arg::with_name("wrap")
287 .long("wrap")
288 .overrides_with("wrap")
289 .takes_value(true)
290 .value_name("mode")
291 .possible_values(&["auto", "never", "character"])
292 .default_value("auto")
293 .hide_default_value(true)
294 .help("Specify the text-wrapping mode (*auto*, never, character).")
295 .long_help("Specify the text-wrapping mode (*auto*, never, character)."),
296 )
297 .arg(
298 Arg::with_name("tabs")
299 .long("tabs")
300 .overrides_with("tabs")
301 .takes_value(true)
302 .value_name("T")
303 .validator(
304 |t| {
305 t.parse::<u32>()
306 .map_err(|_t| "must be a number")
307 .map(|_t| ()) // Convert to Result<(), &str>
308 .map_err(|e| e.to_string())
309 }, // Convert to Result<(), String>
310 )
311 .help("Set the tab width to T spaces.")
312 .long_help(
313 "Set the tab width to T spaces. Use a width of 0 to pass tabs through \
314 directly",
315 ),
316 )
317 .arg(
318 Arg::with_name("unbuffered")
319 .short("u")
320 .long("unbuffered")
321 .hidden_short_help(true)
322 .long_help(
323 "This option exists for POSIX-compliance reasons ('u' is for \
324 'unbuffered'). The output is always unbuffered - this option \
325 is simply ignored.",
326 ),
327 )
328 .arg(
329 Arg::with_name("terminal-width")
330 .long("terminal-width")
331 .takes_value(true)
332 .value_name("width")
333 .hidden_short_help(true)
334 .validator(
335 |t| {
336 let is_offset = t.starts_with('+') || t.starts_with('-');
337 let num = if is_offset {
338 t.chars().skip(1).collect()
339 } else {
340 t
341 };
343 num.parse::<u32>()
344 .map_err(|_e| "must be an offset or number")
345 .and_then(|v| if v == 0 && !is_offset {
346 Err("terminal width cannot be zero".into())
347 } else {
348 Ok(())
349 })
350 .map_err(|e| e.to_string())
351 })
352 .help(
353 "Explicitly set the width of the terminal instead of determining it \
354 automatically. If prefixed with '+' or '-', the value will be treated \
355 as an offset to the actual terminal width.",
356 ),
357 )
358 .arg(
359 Arg::with_name("no-config")
360 .long("no-config")
361 .hidden(true)
362 .help("Do not use the configuration file"),
363 )
364 .arg(
365 Arg::with_name("config-file")
366 .long("config-file")
367 .conflicts_with("list-languages")
368 .conflicts_with("list-themes")
369 .hidden(true)
370 .help("Show path to the configuration file."),
371 )
372 .arg(
373 Arg::with_name("config-dir")
374 .long("config-dir")
375 .hidden(true)
376 .help("Show bat's configuration directory."),
377 )
378 .arg(
379 Arg::with_name("cache-dir")
380 .long("cache-dir")
381 .hidden(true)
382 .help("Show bat's cache directory."),
383 )
384 .subcommand(
385 SubCommand::with_name("cache")
386 .about("Modify the syntax-definition and theme cache")
387 .arg(
388 Arg::with_name("build")
389 .long("build")
390 .short("b")
391 .help("Initialize (or update) the syntax/theme cache.")
392 .long_help(
393 "Initialize (or update) the syntax/theme cache by loading from \
394 the source directory (default: the configuration directory).",
395 ),
396 )
397 .arg(
398 Arg::with_name("clear")
399 .long("clear")
400 .short("c")
401 .help("Remove the cached syntax definitions and themes."),
402 )
403 .group(
404 ArgGroup::with_name("cache-actions")
405 .args(&["build", "clear"])
406 .required(arg_group_required),
407 )
408 .arg(
409 Arg::with_name("source")
410 .long("source")
411 .requires("build")
412 .takes_value(true)
413 .value_name("dir")
414 .help("Use a different directory to load syntaxes and themes from."),
415 )
416 .arg(
417 Arg::with_name("target")
418 .long("target")
419 .requires("build")
420 .takes_value(true)
421 .value_name("dir")
422 .help(
423 "Use a different directory to store the cached syntax and theme set.",
424 ),
425 )
426 .arg(Arg::with_name("blank").long("blank").requires("build").help(
427 "Create completely new syntax and theme sets \
428 (instead of appending to the default sets).",
429 )),
430 )
431 .help_message("Print this help message.")
432 .version_message("Show version information.")