Skip to content

Commit 4b5ae3d

Browse files
authored
Improve CLI argument handling for task runner with trailing_var_arg (#290)
## Summary This PR improves the command-line argument handling for the `vp run` command by properly configuring clap's `trailing_var_arg` feature at the command level and enhancing documentation around argument parsing behavior. ## Key Changes - **Moved `trailing_var_arg` to command level**: Moved the `trailing_var_arg = true` attribute from the individual `task_and_args` field to the `RunCommand` struct itself, which is the correct way to configure this behavior in clap - **Enhanced documentation**: Added comprehensive comments explaining that flags intended for `vp` itself must appear before the task name, while all tokens after the task name are passed through verbatim to the task - **Improved field naming and help text**: Updated the `task_and_args` field with clearer value names (`TASK_SPECIFIER` and `ADDITIONAL_ARGS`) and a more descriptive long help message that explains the interactive task selector fallback - **Refactored `into_resolved` method**: Improved code clarity by explicitly binding intermediate values before constructing the `ResolvedRunCommand` - **Updated test snapshots**: Adjusted expected CLI usage output to reflect the new value names ## Implementation Details The change ensures that clap stops matching flags once the trailing positional argument (task name) starts being filled. This prevents flags like `-v` from being intercepted when they're intended for the task process. Flags for `vp` itself (e.g., `--verbose`, `-r`) must now appear before the task name. Relates to: #285 https://claude.ai/code/session_018viAL2UGVNfLuUjTsi44HQ
1 parent 62dbcab commit 4b5ae3d

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

crates/vite_task/src/cli/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ impl RunFlags {
7474
///
7575
/// Contains the `--last-details` flag which is resolved into a separate
7676
/// `ResolvedCommand::RunLastDetails` variant internally.
77+
///
78+
/// `trailing_var_arg` at the command level makes clap stop matching flags once
79+
/// the trailing positional starts being filled. This means all tokens after the
80+
/// task name are passed through to the task verbatim, preventing flags like `-v`
81+
/// from being intercepted. Flags intended for `vp` itself (e.g. `--verbose`,
82+
/// `-r`) must appear **before** the task name.
83+
///
84+
/// See <https://github.com/voidzero-dev/vite-task/issues/285>.
7785
#[derive(Debug, clap::Parser)]
86+
#[command(trailing_var_arg = true)]
7887
pub struct RunCommand {
7988
#[clap(flatten)]
8089
pub(crate) flags: RunFlags,
@@ -83,11 +92,11 @@ pub struct RunCommand {
8392
#[clap(long, exclusive = true)]
8493
pub(crate) last_details: bool,
8594

86-
/// The task name and all arguments to pass to the task process
87-
/// Prevent flags after the task name to be consumed by Vite Task with `trailing_var_arg`
88-
///
89-
/// <https://github.com/voidzero-dev/vite-task/issues/285>
90-
#[clap(trailing_var_arg = true, allow_hyphen_values = true)]
95+
#[clap(
96+
allow_hyphen_values = true,
97+
value_names = ["TASK_SPECIFIER", "ADDITIONAL_ARGS"],
98+
long_help = "Task to run, as `packageName#taskName` or just `taskName`.\nAny arguments after the task name are forwarded to the task process.\nRunning `vp run` without a task name shows an interactive task selector."
99+
)]
91100
pub(crate) task_and_args: Vec<Str>,
92101
}
93102

@@ -160,14 +169,15 @@ pub struct ResolvedRunCommand {
160169

161170
impl RunCommand {
162171
/// Convert to the resolved run command, stripping the `last_details` flag.
172+
///
173+
/// Splits `task_and_args` into `task_specifier` (the first element) and
174+
/// `additional_args` (everything that follows).
163175
#[must_use]
164176
pub(crate) fn into_resolved(self) -> ResolvedRunCommand {
165177
let mut iter = self.task_and_args.into_iter();
166-
ResolvedRunCommand {
167-
task_specifier: iter.next(),
168-
flags: self.flags,
169-
additional_args: iter.collect(),
170-
}
178+
let task_specifier = iter.next();
179+
let additional_args: Vec<Str> = iter.collect();
180+
ResolvedRunCommand { task_specifier, flags: self.flags, additional_args }
171181
}
172182
}
173183

crates/vite_task_plan/tests/plan_snapshots/fixtures/cache-cli-override/snapshots/query - --cache and --no-cache conflict.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ input_file: crates/vite_task_plan/tests/plan_snapshots/fixtures/cache-cli-overri
1111
---
1212
error: the argument '--cache' cannot be used with '--no-cache'
1313

14-
Usage: vt run --cache <TASK_AND_ARGS>...
14+
Usage: vt run --cache <TASK_SPECIFIER> <ADDITIONAL_ARGS>...
1515

1616
For more information, try '--help'.

0 commit comments

Comments
 (0)