Skip to main content

cargo_run/commands/
mod.rs

1//! CLI command definitions and module registry.
2
3use clap::{ArgAction, Subcommand, ValueEnum};
4
5/// Workspace execution flag accepted on the CLI. Mirrors
6/// [`crate::commands::script::WorkspaceMode`] but lives in this module so
7/// `clap` can derive a `ValueEnum` without making the script types depend on
8/// clap.
9#[derive(ValueEnum, Clone, Debug, Copy)]
10#[value(rename_all = "lowercase")]
11pub enum WorkspaceFlag {
12    All,
13    Parallel,
14    Root,
15}
16
17impl From<WorkspaceFlag> for crate::commands::script::WorkspaceMode {
18    fn from(f: WorkspaceFlag) -> Self {
19        match f {
20            WorkspaceFlag::All => Self::All,
21            WorkspaceFlag::Parallel => Self::Parallel,
22            WorkspaceFlag::Root => Self::Root,
23        }
24    }
25}
26
27/// Top-level subcommands.
28#[derive(Subcommand, Debug, Clone)]
29pub enum Commands {
30    #[command(about = "Run a script by name defined in Scripts.toml", visible_alias = "r")]
31    Run {
32        #[arg(value_name = "SCRIPT_NAME")]
33        script: Option<String>,
34        #[arg(short, long, value_name = "KEY=VALUE", action = ArgAction::Append)]
35        env: Vec<String>,
36        /// Preview what would be executed without actually running it
37        #[arg(long)]
38        dry_run: bool,
39        /// Suppress all output except errors
40        #[arg(short, long)]
41        quiet: bool,
42        /// Show detailed output
43        #[arg(short = 'v', long)]
44        verbose: bool,
45        /// Don't show performance metrics after execution
46        #[arg(long)]
47        no_metrics: bool,
48        /// Interactive script selection
49        #[arg(short, long)]
50        interactive: bool,
51        /// Run the script across workspace members (all|parallel|root)
52        #[arg(long, value_enum, value_name = "MODE")]
53        workspace: Option<WorkspaceFlag>,
54        /// Force execution at the workspace root, ignoring any workspace
55        /// mode declared in Scripts.toml.
56        #[arg(long)]
57        no_workspace: bool,
58        /// Re-run the script when watched files change (requires the `watch`
59        /// feature, enabled by default).
60        #[arg(long)]
61        watch: bool,
62        /// Paths to watch in `--watch` mode (repeatable). Defaults to `.`.
63        #[arg(long, value_name = "PATH", action = ArgAction::Append)]
64        watch_path: Vec<std::path::PathBuf>,
65        /// Substring patterns to ignore in `--watch` mode (repeatable).
66        #[arg(long, value_name = "PATTERN", action = ArgAction::Append)]
67        watch_exclude: Vec<String>,
68        /// Emit a JSON document describing the execution.
69        #[arg(long)]
70        json: bool,
71        /// Run several scripts in parallel (requires the `parallel` feature).
72        /// Use this multiple times for additional scripts.
73        #[arg(long, value_name = "SCRIPT", action = ArgAction::Append)]
74        parallel: Vec<String>,
75        /// Trailing arguments forwarded to the script (`{{name}}` substitution).
76        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
77        script_args: Vec<String>,
78    },
79    #[command(about = "Initialize a Scripts.toml file in the current directory")]
80    Init {
81        /// Apply a named template (use --list-templates to see options).
82        #[arg(short, long, value_name = "NAME")]
83        template: Option<String>,
84        /// List the available templates and exit.
85        #[arg(long)]
86        list_templates: bool,
87        /// Overwrite existing files when applying a template.
88        #[arg(long)]
89        force: bool,
90    },
91    #[command(about = "Show all script names and descriptions defined in Scripts.toml")]
92    Show {
93        /// Suppress all output except errors
94        #[arg(short, long)]
95        quiet: bool,
96        /// Show detailed output
97        #[arg(short = 'v', long)]
98        verbose: bool,
99        /// Filter scripts by name or description
100        #[arg(short, long, value_name = "PATTERN")]
101        filter: Option<String>,
102    },
103    #[command(about = "Generate shell completion scripts")]
104    Completions {
105        /// Shell to generate completions for
106        #[arg(value_enum)]
107        shell: Shell,
108    },
109    #[command(about = "Validate Scripts.toml syntax, script references, and tool requirements")]
110    Validate {
111        /// Suppress all output except errors
112        #[arg(short, long)]
113        quiet: bool,
114        /// Show detailed output
115        #[arg(short = 'v', long)]
116        verbose: bool,
117    },
118    #[command(about = "Workspace operations: list members, run a script across them, etc.")]
119    Workspace {
120        #[command(subcommand)]
121        cmd: WorkspaceCmd,
122    },
123}
124
125/// Workspace subcommands.
126#[derive(Subcommand, Debug, Clone)]
127pub enum WorkspaceCmd {
128    /// List discovered workspace members.
129    #[command(about = "List discovered workspace members")]
130    List {
131        /// Emit JSON.
132        #[arg(long)]
133        json: bool,
134    },
135    /// Run a script in every member.
136    #[command(about = "Run a script in every workspace member")]
137    Run {
138        #[arg(value_name = "SCRIPT_NAME")]
139        script: String,
140        /// Run in parallel rather than sequentially.
141        #[arg(long)]
142        parallel: bool,
143        /// Emit JSON.
144        #[arg(long)]
145        json: bool,
146    },
147}
148
149/// Supported shells for completion generation.
150#[derive(ValueEnum, Clone, Debug)]
151#[value(rename_all = "kebab-case")]
152pub enum Shell {
153    Bash,
154    Zsh,
155    Fish,
156    #[value(name = "power-shell")]
157    PowerShell,
158}
159
160pub mod init;
161pub mod script;
162pub mod show;
163pub mod completions;
164pub mod validate;
165pub mod workspace;
166pub mod cargo_script;
167pub mod templates;
168
169#[cfg(feature = "parallel")]
170pub mod parallel;
171
172#[cfg(feature = "watch")]
173pub mod watch;