on 3446fdf help sign in
bat/src/clap_app.rs
289 lines 12 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::NextLineHelp)
21 .setting(AppSettings::InferSubcommands)
22 .setting(AppSettings::ArgsNegateSubcommands)
23 .setting(AppSettings::DisableHelpSubcommand)
24 .setting(AppSettings::VersionlessSubcommands)
25 .max_term_width(90)
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("theme")
64 .long("theme")
65 .overrides_with("theme")
66 .takes_value(true)
67 .help("Set the color theme for syntax highlighting.")
68 .long_help(
69 "Set the theme for syntax highlighting. Use '--list-themes' to \
70 see all available themes. To set a default theme, export the \
71 BAT_THEME environment variable (e.g.: export \
72 BAT_THEME=\"TwoDark\").",
73 ),
74 )
75 .arg(
76 Arg::with_name("list-themes")
77 .long("list-themes")
78 .help("Display all supported highlighting themes.")
79 .long_help("Display a list of supported themes for syntax highlighting."),
80 )
81 .arg(
82 Arg::with_name("style")
83 .long("style")
84 .value_name("style-components")
85 .use_delimiter(true)
86 .takes_value(true)
87 .possible_values(&[
88 "auto", "full", "plain", "changes", "header", "grid", "numbers",
89 ])
90 .help("Comma-separated list of style elements to display.")
91 .long_help(
92 "Configure which elements (line numbers, file headers, grid \
93 borders, Git modifications, ..) to display in addition to the \
94 file contents. The argument is a comma-separated list of \
95 components to display (e.g. 'numbers,changes,grid') or a \
96 pre-defined style ('full'). To set a default theme, export the \
97 BAT_STYLE environment variable (e.g.: export BAT_STYLE=\"numbers\").",
98 ),
99 )
100 .arg(
101 Arg::with_name("plain")
102 .overrides_with("plain")
103 .short("p")
104 .long("plain")
105 .conflicts_with("style")
106 .conflicts_with("number")
107 .help("Show plain style (alias for '--style=plain').")
108 .long_help(
109 "Only show plain style, no decorations. This is an alias for \
110 '--style=plain'",
111 ),
112 )
113 .arg(
114 Arg::with_name("number")
115 .long("number")
116 .overrides_with("number")
117 .short("n")
118 .conflicts_with("style")
119 .help("Show line numbers (alias for '--style=numbers').")
120 .long_help(
121 "Only show line numbers, no other decorations. This is an alias for \
122 '--style=numbers'",
123 ),
124 )
125 .arg(
126 Arg::with_name("line-range")
127 .long("line-range")
128 .overrides_with("line-range")
129 .takes_value(true)
130 .value_name("N:M")
131 .help("Only print the lines from N to M.")
132 .long_help(
133 "Only print the specified range of lines for each file. \
134 For example:\n \
135 '--line-range 30:40' prints lines 30 to 40\n \
136 '--line-range :40' prints lines 1 to 40\n \
137 '--line-range 40:' prints lines 40 to the end of the file",
138 ),
139 )
140 .arg(
141 Arg::with_name("color")
142 .long("color")
143 .overrides_with("color")
144 .takes_value(true)
145 .value_name("when")
146 .possible_values(&["auto", "never", "always"])
147 .default_value("auto")
148 .help("When to use colors.")
149 .long_help(
150 "Specify when to use colored output. The automatic mode \
151 only enables colors if an interactive terminal is detected.",
152 ),
153 )
154 .arg(
155 Arg::with_name("decorations")
156 .long("decorations")
157 .overrides_with("decorations")
158 .takes_value(true)
159 .value_name("when")
160 .possible_values(&["auto", "never", "always"])
161 .default_value("auto")
162 .help("When to show the decorations specified by '--style'.")
163 .long_help(
164 "Specify when to use the decorations that have been specified \
165 via '--style'. The automatic mode only enables decorations if \
166 an interactive terminal is detected.",
167 ),
168 )
169 .arg(
170 Arg::with_name("paging")
171 .long("paging")
172 .overrides_with("paging")
173 .takes_value(true)
174 .value_name("when")
175 .possible_values(&["auto", "never", "always"])
176 .default_value("auto")
177 .help("Specify when to use the pager.")
178 .long_help(
179 "Specify when to use the pager. To control which pager \
180 is used, set the PAGER or BAT_PAGER environment \
181 variables (the latter takes precedence). The default \
182 pager is 'less'. To disable the pager permanently, set \
183 BAT_PAGER to an empty string.",
184 ),
185 )
186 .arg(
187 Arg::with_name("wrap")
188 .long("wrap")
189 .overrides_with("wrap")
190 .takes_value(true)
191 .value_name("mode")
192 .possible_values(&["auto", "never", "character"])
193 .default_value("auto")
194 .help("Specify the text-wrapping mode.")
195 .long_help("Specify the text-wrapping mode."),
196 )
197 .arg(
198 Arg::with_name("unbuffered")
199 .short("u")
200 .hidden_short_help(true)
201 .long_help(
202 "This option exists for POSIX-compliance reasons ('u' is for \
203 'unbuffered'). The output is always unbuffered - this option \
204 is simply ignored.",
205 ),
206 )
207 .arg(
208 Arg::with_name("tabs")
209 .long("tabs")
210 .takes_value(true)
211 .value_name("tabs")
212 .validator(
213 |t| {
214 t.parse::<u32>()
215 .map_err(|_t| "must be a number")
216 .map(|_t| ()) // Convert to Result<(), &str>
217 .map_err(|e| e.to_string())
218 }, // Convert to Result<(), String>
219 )
220 .help("Sets the tab width.")
221 .long_help(
222 "Sets the tab width. Use a width of 0 to pass tabs through \
223 directly",
224 ),
225 )
226 .arg(
227 Arg::with_name("terminal-width")
228 .long("terminal-width")
229 .takes_value(true)
230 .value_name("width")
231 .hidden(true)
232 .help("Set the width of the terminal"),
233 )
234 .subcommand(
235 SubCommand::with_name("cache")
236 .about("Modify the syntax-definition and theme cache")
237 .arg(
238 Arg::with_name("init")
239 .long("init")
240 .short("i")
241 .help("Initialize the syntax/theme cache.")
242 .long_help(
243 "Initialize the syntax/theme cache by loading from the \
244 source directory (default: the configuration directory).",
245 ),
246 )
247 .arg(
248 Arg::with_name("clear")
249 .long("clear")
250 .short("c")
251 .help("Remove the cached syntax definitions and themes."),
252 )
253 .arg(
254 Arg::with_name("config-dir")
255 .long("config-dir")
256 .short("d")
257 .help("Show bat's configuration directory."),
258 )
259 .group(
260 ArgGroup::with_name("cache-actions")
261 .args(&["init", "clear", "config-dir"])
262 .required(arg_group_required),
263 )
264 .arg(
265 Arg::with_name("source")
266 .long("source")
267 .requires("init")
268 .takes_value(true)
269 .value_name("dir")
270 .help("Use a different directory to load syntaxes and themes from."),
271 )
272 .arg(
273 Arg::with_name("target")
274 .long("target")
275 .requires("init")
276 .takes_value(true)
277 .value_name("dir")
278 .help(
279 "Use a different directory to store the cached syntax and theme set.",
280 ),
281 )
282 .arg(Arg::with_name("blank").long("blank").requires("init").help(
283 "Create completely new syntax and theme sets \
284 (instead of appending to the default sets).",
285 )),
286 )
287 .help_message("Print this help message.")
288 .version_message("Show version information.")