libdiffsitter/cli.rs
1use crate::console_utils::ColorOutputPolicy;
2use clap::Parser;
3use std::path::PathBuf;
4use strum_macros::EnumString;
5
6#[derive(Debug, Eq, PartialEq, Clone, Parser)]
7#[clap(author, version, about)]
8pub struct Args {
9 /// Print debug output
10 ///
11 /// This will print debug logs at the trace level. This is useful for debugging and bug reports
12 /// should contain debug logging info.
13 #[clap(short, long)]
14 pub debug: bool,
15 /// Run a subcommand that doesn't perform a diff. Valid options are: "list",
16 /// "dump_default_config", and "build_info".
17 ///
18 /// * "list" lists all of the filetypes/languages that this program was compiled with support
19 /// for
20 ///
21 /// * "dump_default_config" will dump the default configuration to stdout
22 ///
23 /// * "build_info" prints extended build information
24 #[clap(subcommand)]
25 pub cmd: Option<Command>,
26 /// The first file to compare against
27 ///
28 /// Text that is in this file but is not in the new file is considered a deletion
29 // #[clap(name = "OLD", parse(from_os_str), required_unless_present = "cmd")]
30 #[clap(name = "OLD")]
31 pub old: Option<PathBuf>,
32 /// The file that the old file is compared against
33 ///
34 /// Text that is in this file but is not in the old file is considered an addition
35 // #[clap(name = "NEW", parse(from_os_str), required_unless_present = "cmd")]
36 #[clap(name = "NEW")]
37 pub new: Option<PathBuf>,
38 /// Manually set the file type for the given files
39 ///
40 /// This will dictate which parser is used with the difftool. You can list all of the valid
41 /// file type strings with `diffsitter --cmd list`
42 #[clap(short = 't', long)]
43 pub file_type: Option<String>,
44 /// Use the config provided at the given path
45 ///
46 /// By default, diffsitter attempts to find the config at `$XDG_CONFIG_HOME/diffsitter.json5`.
47 /// On Windows the app will look in the standard config path.
48 // #[clap(short, long, env = "DIFFSITTER_CONFIG")]
49 #[clap(short, long)]
50 pub config: Option<PathBuf>,
51 /// Set the color output policy. Valid values are: "auto", "on", "off".
52 ///
53 /// "auto" will automatically detect whether colors should be applied by trying to determine
54 /// whether the process is outputting to a TTY. "on" will enable output and "off" will
55 /// disable color output regardless of whether the process detects a TTY.
56 #[clap(long = "color", default_value_t)]
57 pub color_output: ColorOutputPolicy,
58 /// Ignore any config files and use the default config
59 ///
60 /// This will cause the app to ignore any configs and all config values will use the their
61 /// default settings.
62 #[clap(short, long)]
63 pub no_config: bool,
64
65 /// Specify which renderer tag to use.
66 ///
67 /// If no option is supplied then this will fall back to the default renderer.
68 #[clap(short, long)]
69 pub renderer: Option<String>,
70}
71
72/// A wrapper struct for `clap_complete::Shell`.
73///
74/// We need this wrapper so we can automatically serialize strings using `EnumString` and use the
75/// enums as a clap argument.
76#[derive(Copy, Clone, EnumString, PartialEq, Eq, Debug)]
77#[strum(serialize_all = "snake_case")]
78pub enum ShellWrapper {
79 Bash,
80 Zsh,
81 Fish,
82 Elvish,
83
84 #[strum(serialize = "powershell")]
85 PowerShell,
86}
87
88impl Default for ShellWrapper {
89 fn default() -> Self {
90 Self::Bash
91 }
92}
93
94impl From<ShellWrapper> for clap_complete::Shell {
95 fn from(wrapper: ShellWrapper) -> Self {
96 use clap_complete as cc;
97
98 match wrapper {
99 ShellWrapper::Bash => cc::Shell::Bash,
100 ShellWrapper::Zsh => cc::Shell::Zsh,
101 ShellWrapper::Fish => cc::Shell::Fish,
102 ShellWrapper::Elvish => cc::Shell::Elvish,
103 ShellWrapper::PowerShell => cc::Shell::PowerShell,
104 }
105 }
106}
107
108/// Commands related to the configuration
109#[derive(Debug, Eq, PartialEq, Clone, Copy, Parser, EnumString)]
110#[strum(serialize_all = "snake_case")]
111pub enum Command {
112 /// List the languages that this program was compiled for
113 List,
114
115 /// Dump the default config to stdout
116 DumpDefaultConfig,
117
118 /// Generate shell completion scripts for diffsitter
119 GenCompletion {
120 /// The shell to generate completion scripts for.
121 ///
122 /// This will print the shell completion script to stdout. bash, zsh, and fish are supported.
123 shell: ShellWrapper,
124 },
125}