From fbc91b94194d260a5ee5df1ff43aed68cb93ec1b Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Sat, 14 Sep 2024 11:22:09 +0200 Subject: [PATCH] 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. --- crates/uv-cli/src/lib.rs | 10 ++++++++++ crates/uv/src/lib.rs | 38 +++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 568e046aa90a1..b03f594ac3264 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -76,6 +76,16 @@ pub struct Cli { #[command(subcommand)] pub command: Box, + #[command(flatten)] + pub top_level: TopLevelArgs, +} + +#[derive(Parser)] +#[command( + disable_help_flag = true, + disable_version_flag = true +)] +pub struct TopLevelArgs { #[command(flatten)] pub cache_args: Box, diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index a935d6a1b0005..eb40a65ac5570 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -6,7 +6,7 @@ use std::process::ExitCode; use anstream::eprintln; use anyhow::Result; use clap::error::{ContextKind, ContextValue}; -use clap::{CommandFactory, Parser}; +use clap::{Args, CommandFactory, Parser}; use owo_colors::OwoColorize; use settings::PipTreeSettings; use tracing::{debug, instrument}; @@ -16,7 +16,7 @@ use uv_cli::{ compat::CompatArgs, CacheCommand, CacheNamespace, Cli, Commands, PipCommand, PipNamespace, ProjectCommand, }; -use uv_cli::{PythonCommand, PythonNamespace, ToolCommand, ToolNamespace}; +use uv_cli::{PythonCommand, PythonNamespace, ToolCommand, ToolNamespace, TopLevelArgs}; #[cfg(feature = "self-update")] use uv_cli::{SelfCommand, SelfNamespace, SelfUpdateArgs}; use uv_fs::CWD; @@ -58,12 +58,12 @@ pub(crate) mod version; #[instrument(skip_all)] async fn run(cli: Cli) -> Result { // Enable flag to pick up warnings generated by workspace loading. - if !cli.global_args.quiet { + if !cli.top_level.global_args.quiet { uv_warnings::enable(); } // Switch directories as early as possible. - if let Some(directory) = cli.global_args.directory.as_ref() { + if let Some(directory) = cli.top_level.global_args.directory.as_ref() { std::env::set_current_dir(directory)?; } @@ -106,7 +106,7 @@ async fn run(cli: Cli) -> Result { // If found, this file is combined with the user configuration file. // 3. The nearest configuration file (`uv.toml` or `pyproject.toml`) in the directory tree, // starting from the current directory. - let filesystem = if let Some(config_file) = cli.config_file.as_ref() { + let filesystem = if let Some(config_file) = cli.top_level.config_file.as_ref() { if config_file .file_name() .is_some_and(|file_name| file_name == "pyproject.toml") @@ -114,7 +114,7 @@ async fn run(cli: Cli) -> Result { 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."); } Some(FilesystemOptions::from_file(config_file)?) - } else if deprecated_isolated || cli.no_config { + } else if deprecated_isolated || cli.top_level.no_config { None } else if matches!(&*cli.command, Commands::Tool(_)) { // For commands that operate at the user-level, ignore local configuration. @@ -175,10 +175,10 @@ async fn run(cli: Cli) -> Result { .combine(filesystem); // Resolve the global settings. - let globals = GlobalSettings::resolve(&cli.global_args, filesystem.as_ref()); + let globals = GlobalSettings::resolve(&cli.top_level.global_args, filesystem.as_ref()); // Resolve the cache settings. - let cache_settings = CacheSettings::resolve(*cli.cache_args, filesystem.as_ref()); + let cache_settings = CacheSettings::resolve(*cli.top_level.cache_args, filesystem.as_ref()); // Configure the `tracing` crate, which controls internal logging. #[cfg(feature = "tracing-durations-export")] @@ -687,7 +687,7 @@ async fn run(cli: Cli) -> Result { args.hash_checking, args.python, args.settings, - cli.no_config, + cli.top_level.no_config, globals.python_preference, globals.python_downloads, globals.connectivity, @@ -743,7 +743,7 @@ async fn run(cli: Cli) -> Result { args.settings.exclude_newer, globals.concurrency, globals.native_tls, - cli.no_config, + cli.top_level.no_config, args.no_project, &cache, printer, @@ -757,7 +757,7 @@ async fn run(cli: Cli) -> Result { run_command, script, globals, - cli.no_config, + cli.top_level.no_config, filesystem, cache, printer, @@ -778,6 +778,18 @@ async fn run(cli: Cli) -> Result { } Commands::GenerateShellCompletion(args) => { args.shell.generate(&mut Cli::command(), &mut stdout()); + + // Create uvx as a combination of top level arguments and the uv tool uvx subcommand + let mut uvx = Cli::command() + .find_subcommand("tool") + .unwrap() + .find_subcommand("uvx") + .unwrap() + .clone(); + uvx = TopLevelArgs::augment_args(uvx); + uvx = uvx.version("dummy_for_completion"); + args.shell.generate(&mut uvx, &mut stdout()); + Ok(ExitStatus::Success) } Commands::Tool(ToolNamespace { @@ -974,7 +986,7 @@ async fn run(cli: Cli) -> Result { globals.python_downloads, globals.native_tls, globals.connectivity, - cli.no_config, + cli.top_level.no_config, printer, ) .await @@ -1000,7 +1012,7 @@ async fn run(cli: Cli) -> Result { commands::python_find( args.request, args.no_project, - cli.no_config, + cli.top_level.no_config, args.system, globals.python_preference, &cache,