Skip to content

Commit b5f3e25

Browse files
committed
Generate shell completion for uvx
Create a dummy uvx top level command by grabbing the `uv tool uvx` subcommand (hidden alias of `uv tool run`). Global arguments need to be added to this dummy top level command; this is accomplished by splitting Cli into subcommands and top level arguments: then top level arguments can be added onto the dummy uvx top level. There is one conflict between global --isolated and uv tool run --isolated; this must be handled (as in the code) to avoid debug asserts when generating completion.
1 parent 3060fd2 commit b5f3e25

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

crates/uv-cli/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ pub struct Cli {
7676
#[command(subcommand)]
7777
pub command: Box<Commands>,
7878

79+
#[command(flatten)]
80+
pub top_level: TopLevelArgs,
81+
}
82+
83+
#[derive(Parser)]
84+
#[command(disable_help_flag = true, disable_version_flag = true)]
85+
pub struct TopLevelArgs {
7986
#[command(flatten)]
8087
pub cache_args: Box<CacheArgs>,
8188

crates/uv/src/lib.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use uv_cli::{
1616
compat::CompatArgs, CacheCommand, CacheNamespace, Cli, Commands, PipCommand, PipNamespace,
1717
ProjectCommand,
1818
};
19-
use uv_cli::{PythonCommand, PythonNamespace, ToolCommand, ToolNamespace};
19+
use uv_cli::{PythonCommand, PythonNamespace, ToolCommand, ToolNamespace, TopLevelArgs};
2020
#[cfg(feature = "self-update")]
2121
use uv_cli::{SelfCommand, SelfNamespace, SelfUpdateArgs};
2222
use uv_fs::CWD;
@@ -58,17 +58,17 @@ pub(crate) mod version;
5858
#[instrument(skip_all)]
5959
async fn run(cli: Cli) -> Result<ExitStatus> {
6060
// Enable flag to pick up warnings generated by workspace loading.
61-
if !cli.global_args.quiet {
61+
if !cli.top_level.global_args.quiet {
6262
uv_warnings::enable();
6363
}
6464

6565
// Switch directories as early as possible.
66-
if let Some(directory) = cli.global_args.directory.as_ref() {
66+
if let Some(directory) = cli.top_level.global_args.directory.as_ref() {
6767
std::env::set_current_dir(directory)?;
6868
}
6969

7070
// The `--isolated` argument is deprecated on preview APIs, and warns on non-preview APIs.
71-
let deprecated_isolated = if cli.global_args.isolated {
71+
let deprecated_isolated = if cli.top_level.global_args.isolated {
7272
match &*cli.command {
7373
// Supports `--isolated` as its own argument, so we can't warn either way.
7474
Commands::Tool(ToolNamespace {
@@ -106,15 +106,15 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
106106
// If found, this file is combined with the user configuration file.
107107
// 3. The nearest configuration file (`uv.toml` or `pyproject.toml`) in the directory tree,
108108
// starting from the current directory.
109-
let filesystem = if let Some(config_file) = cli.config_file.as_ref() {
109+
let filesystem = if let Some(config_file) = cli.top_level.config_file.as_ref() {
110110
if config_file
111111
.file_name()
112112
.is_some_and(|file_name| file_name == "pyproject.toml")
113113
{
114114
warn_user!("The `--config-file` argument expects to receive a `uv.toml` file, not a `pyproject.toml`. If you're trying to run a command from another project, use the `--directory` argument instead.");
115115
}
116116
Some(FilesystemOptions::from_file(config_file)?)
117-
} else if deprecated_isolated || cli.no_config {
117+
} else if deprecated_isolated || cli.top_level.no_config {
118118
None
119119
} else if matches!(&*cli.command, Commands::Tool(_)) {
120120
// For commands that operate at the user-level, ignore local configuration.
@@ -175,10 +175,10 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
175175
.combine(filesystem);
176176

177177
// Resolve the global settings.
178-
let globals = GlobalSettings::resolve(&cli.global_args, filesystem.as_ref());
178+
let globals = GlobalSettings::resolve(&cli.top_level.global_args, filesystem.as_ref());
179179

180180
// Resolve the cache settings.
181-
let cache_settings = CacheSettings::resolve(*cli.cache_args, filesystem.as_ref());
181+
let cache_settings = CacheSettings::resolve(*cli.top_level.cache_args, filesystem.as_ref());
182182

183183
// Configure the `tracing` crate, which controls internal logging.
184184
#[cfg(feature = "tracing-durations-export")]
@@ -687,7 +687,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
687687
args.hash_checking,
688688
args.python,
689689
args.settings,
690-
cli.no_config,
690+
cli.top_level.no_config,
691691
globals.python_preference,
692692
globals.python_downloads,
693693
globals.connectivity,
@@ -743,7 +743,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
743743
args.settings.exclude_newer,
744744
globals.concurrency,
745745
globals.native_tls,
746-
cli.no_config,
746+
cli.top_level.no_config,
747747
args.no_project,
748748
&cache,
749749
printer,
@@ -757,7 +757,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
757757
run_command,
758758
script,
759759
globals,
760-
cli.no_config,
760+
cli.top_level.no_config,
761761
filesystem,
762762
cache,
763763
printer,
@@ -777,7 +777,28 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
777777
Ok(ExitStatus::Success)
778778
}
779779
Commands::GenerateShellCompletion(args) => {
780+
// uv
780781
args.shell.generate(&mut Cli::command(), &mut stdout());
782+
783+
// uvx: combine `uv tool uvx` with top level arguments
784+
let mut uvx = Cli::command()
785+
.find_subcommand("tool")
786+
.unwrap()
787+
.find_subcommand("uvx")
788+
.unwrap()
789+
.clone()
790+
.disable_help_flag(true)
791+
.disable_version_flag(true)
792+
.version("dummy_for_completion");
793+
794+
// like Args::augment_args but open coded to skip collisions
795+
for arg in TopLevelArgs::command().get_arguments() {
796+
if arg.get_id() != "isolated" {
797+
uvx = uvx.arg(arg);
798+
}
799+
}
800+
args.shell.generate(&mut uvx, &mut stdout());
801+
781802
Ok(ExitStatus::Success)
782803
}
783804
Commands::Tool(ToolNamespace {
@@ -974,7 +995,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
974995
globals.python_downloads,
975996
globals.native_tls,
976997
globals.connectivity,
977-
cli.no_config,
998+
cli.top_level.no_config,
978999
printer,
9791000
)
9801001
.await
@@ -1000,7 +1021,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
10001021
commands::python_find(
10011022
args.request,
10021023
args.no_project,
1003-
cli.no_config,
1024+
cli.top_level.no_config,
10041025
args.system,
10051026
globals.python_preference,
10061027
&cache,

0 commit comments

Comments
 (0)