taplo_cli/args.rs
1use clap::{crate_version, ArgEnum, Args, Parser, Subcommand};
2use std::path::PathBuf;
3use url::Url;
4
5#[derive(Clone, Parser)]
6#[clap(name = "taplo")]
7#[clap(bin_name = "taplo")]
8#[clap(version = crate_version!())]
9pub struct TaploArgs {
10 #[clap(long, arg_enum, global = true, default_value = "auto")]
11 pub colors: Colors,
12 /// Enable a verbose logging format.
13 #[clap(long, global = true)]
14 pub verbose: bool,
15 /// Enable logging spans.
16 #[clap(long, global = true)]
17 pub log_spans: bool,
18 #[clap(subcommand)]
19 pub cmd: TaploCommand,
20}
21
22#[derive(Clone, Args)]
23pub struct GeneralArgs {
24 /// Path to the Taplo configuration file.
25 #[clap(long, short, env = "TAPLO_CONFIG")]
26 pub config: Option<PathBuf>,
27
28 /// Set a cache path.
29 #[clap(long)]
30 pub cache_path: Option<PathBuf>,
31
32 /// Do not search for a configuration file.
33 #[clap(long)]
34 pub no_auto_config: bool,
35}
36
37#[derive(Clone, Copy, ArgEnum)]
38pub enum Colors {
39 /// Determine whether to colorize output automatically.
40 Auto,
41 /// Always colorize output.
42 Always,
43 /// Never colorize output.
44 Never,
45}
46
47#[derive(Clone, Subcommand)]
48pub enum TaploCommand {
49 /// Lint TOML documents.
50 #[clap(visible_aliases = &["check", "validate"])]
51 Lint(LintCommand),
52 /// Format TOML documents.
53 ///
54 /// Files are modified in-place unless the input comes from the standard input, in which case the formatted result is printed to the standard output.
55 #[clap(visible_aliases = &["fmt"])]
56 Format(FormatCommand),
57 /// Language server operations.
58 Lsp {
59 #[clap(flatten)]
60 cmd: LspCommand,
61 },
62 /// Operations with the Taplo config file.
63 #[clap(visible_aliases = &["cfg"])]
64 Config {
65 #[clap(subcommand)]
66 cmd: ConfigCommand,
67 },
68 /// Extract a value from the given TOML document.
69 Get(GetCommand),
70 /// Start a decoder for `toml-test` (https://github.com/BurntSushi/toml-test).
71 #[cfg(feature = "toml-test")]
72 TomlTest {},
73}
74
75#[derive(Clone, Args)]
76pub struct FormatCommand {
77 #[clap(flatten)]
78 pub general: GeneralArgs,
79
80 /// A formatter option given as a "key=value", can be set multiple times.
81 ///
82 /// The valid options and values are available here: https://taplo.tamasfe.dev/configuration/formatter-options.html
83 #[clap(long = "option", short)]
84 pub options: Vec<String>,
85
86 /// Ignore syntax errors and force formatting.
87 #[clap(long, short)]
88 pub force: bool,
89
90 /// Dry-run and report any files that are not correctly formatted.
91 #[clap(long)]
92 pub check: bool,
93
94 /// Print the differences in patch formatting to `stdout`
95 #[clap(long)]
96 pub diff: bool,
97
98 /// Paths or glob patterns to TOML documents.
99 ///
100 /// If the only argument is "-", the standard input will be used.
101 pub files: Vec<String>,
102
103 /// A path to the file that the Taplo CLI will treat like stdin.
104 ///
105 /// This option does not change the file input source. This option should be used only when the
106 /// source input arises from the stdin.
107 #[clap(long)]
108 pub stdin_filepath: Option<String>,
109}
110
111#[derive(Clone, Args)]
112pub struct LspCommand {
113 #[clap(flatten)]
114 pub general: GeneralArgs,
115
116 #[clap(subcommand)]
117 pub io: LspCommandIo,
118}
119
120#[derive(Clone, Subcommand)]
121pub enum LspCommandIo {
122 /// Run the language server and listen on a TCP address.
123 Tcp {
124 /// The address to listen on.
125 #[clap(long, default_value = "0.0.0.0:9181")]
126 address: String,
127 },
128 /// Run the language server over the standard input and output.
129 Stdio {},
130}
131
132#[derive(Clone, Subcommand)]
133pub enum ConfigCommand {
134 /// Print the default `.taplo.toml` configuration file.
135 Default,
136 /// Print the JSON schema of the `.taplo.toml` configuration file.
137 Schema,
138}
139
140#[derive(Clone, Args)]
141pub struct LintCommand {
142 #[clap(flatten)]
143 pub general: GeneralArgs,
144
145 /// URL to the schema to be used for validation.
146 #[clap(long)]
147 pub schema: Option<Url>,
148
149 /// URL to a schema catalog (index) that is compatible with Schema Store or Taplo catalogs.
150 ///
151 /// Can be specified multiple times.
152 #[clap(long)]
153 pub schema_catalog: Vec<Url>,
154
155 /// Use the default online catalogs for schemas.
156 #[clap(long)]
157 pub default_schema_catalogs: bool,
158
159 /// Disable all schema validations.
160 #[clap(long)]
161 pub no_schema: bool,
162
163 /// Paths or glob patterns to TOML documents.
164 ///
165 /// If the only argument is "-", the standard input will be used.
166 pub files: Vec<String>,
167}
168
169#[derive(Clone, Args)]
170pub struct GetCommand {
171 /// The format specifying how the output is printed.
172 ///
173 /// All newlines are in the output are LF.
174 ///
175 /// Format-specific remarks:
176 ///
177 /// --- value:
178 ///
179 /// If the value is a string, all surrounding quotes will be stripped and
180 /// all escape sequences will be unescaped.
181 ///
182 /// If the value is an integer or float, it will be output in a decimal format without any rounding.
183 ///
184 /// If the value is an array, all of its items will be output on separate lines.
185 ///
186 /// If the value is a table or an array that contains tables, the operation will fail.
187 ///
188 /// --- toml:
189 ///
190 /// Comments and formatting will not be preserved.
191 /// It is possible to select arrays and individual values that are not tables,
192 /// in this case the output will not be a valid TOML document.
193 #[clap(short, long, arg_enum, default_value = "value")]
194 pub output_format: OutputFormat,
195
196 /// Strip the trailing newline from the output.
197 ///
198 /// If this is not provided, all output will end with a line-feed character.
199 #[clap(short, long)]
200 pub strip_newline: bool,
201
202 /// Path to the TOML document, if omitted the standard input will be used.
203 #[clap(short, long)]
204 pub file_path: Option<PathBuf>,
205
206 /// A dotted key pattern to the value within the TOML document.
207 ///
208 /// If omitted, the entire document will be printed.
209 ///
210 /// If the pattern yielded no values, the operation will fail.
211 ///
212 /// The pattern supports `jq`-like syntax and glob patterns as well:
213 ///
214 /// Examples:
215 ///
216 /// - table.array[1].foo
217 /// - table.array.1.foo
218 /// - table.array[*].foo
219 /// - table.array.*.foo
220 /// - dependencies.tokio-*.version
221 ///
222 pub pattern: Option<String>,
223
224 /// A string that separates array values when printing to stdout.
225 ///
226 /// If `--separator` is specified with a non text output format,
227 /// the operation will fail.
228 #[clap(long)]
229 pub separator: Option<String>,
230}
231
232#[derive(Clone, Copy, ArgEnum)]
233pub enum OutputFormat {
234 /// Extract the value outputting it in a text format.
235 Value,
236 /// Output format in JSON.
237 Json,
238 /// Output format in TOML.
239 Toml,
240}