taplo_cli/
args.rs

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