Skip to content

Commit 5285eba

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.
1 parent a4a1982 commit 5285eba

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-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: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::process::ExitCode;
66
use anstream::eprintln;
77
use anyhow::Result;
88
use clap::error::{ContextKind, ContextValue};
9-
use clap::{CommandFactory, Parser};
9+
use clap::{Args, CommandFactory, Parser};
1010
use owo_colors::OwoColorize;
1111
use settings::PipTreeSettings;
1212
use tracing::{debug, instrument};
@@ -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,12 +58,12 @@ 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

@@ -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,
@@ -778,6 +778,18 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
778778
}
779779
Commands::GenerateShellCompletion(args) => {
780780
args.shell.generate(&mut Cli::command(), &mut stdout());
781+
782+
// Create uvx as a combination of top level arguments and the uv tool uvx subcommand
783+
let mut uvx = Cli::command()
784+
.find_subcommand("tool")
785+
.unwrap()
786+
.find_subcommand("uvx")
787+
.unwrap()
788+
.clone();
789+
uvx = TopLevelArgs::augment_args(uvx);
790+
uvx = uvx.version("dummy_for_completion");
791+
args.shell.generate(&mut uvx, &mut stdout());
792+
781793
Ok(ExitStatus::Success)
782794
}
783795
Commands::Tool(ToolNamespace {
@@ -974,7 +986,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
974986
globals.python_downloads,
975987
globals.native_tls,
976988
globals.connectivity,
977-
cli.no_config,
989+
cli.top_level.no_config,
978990
printer,
979991
)
980992
.await
@@ -1000,7 +1012,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
10001012
commands::python_find(
10011013
args.request,
10021014
args.no_project,
1003-
cli.no_config,
1015+
cli.top_level.no_config,
10041016
args.system,
10051017
globals.python_preference,
10061018
&cache,

0 commit comments

Comments
 (0)