-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
uv help
manually instead of using Clap default
- Loading branch information
Showing
6 changed files
with
293 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::fmt::Write; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use clap::CommandFactory; | ||
use itertools::Itertools; | ||
|
||
use super::ExitStatus; | ||
use crate::printer::Printer; | ||
use uv_cli::Cli; | ||
|
||
pub(crate) fn help(query: &[String], printer: Printer) -> Result<ExitStatus> { | ||
let uv = Cli::command(); | ||
let command = find_command(query, &uv).map_err(|(unmatched, nearest)| { | ||
let missing = if unmatched.len() == query.len() { | ||
format!("`{}` for `uv`", query.join(" ")) | ||
} else { | ||
format!("`{}` for `uv {}`", unmatched.join(" "), nearest.get_name()) | ||
}; | ||
anyhow!( | ||
"There is no command {}. Did you mean one of:\n {}", | ||
missing, | ||
nearest | ||
.get_subcommands() | ||
.filter(|cmd| !cmd.is_hide_set()) | ||
.map(clap::Command::get_name) | ||
.filter(|name| *name != "help") | ||
.join("\n "), | ||
) | ||
})?; | ||
|
||
let mut command = command.clone(); | ||
let help = command.render_long_help(); | ||
writeln!(printer.stderr(), "{}", help.ansi())?; | ||
|
||
Ok(ExitStatus::Success) | ||
} | ||
|
||
/// Find the command corresponding to a set of arguments, e.g., `["uv", "pip", "install"]`. | ||
/// | ||
/// If the command cannot be found, the nearest command is returned. | ||
fn find_command<'a>( | ||
query: &'a [String], | ||
cmd: &'a clap::Command, | ||
) -> Result<&'a clap::Command, (&'a [String], &'a clap::Command)> { | ||
let Some(next) = query.first() else { | ||
return Ok(cmd); | ||
}; | ||
|
||
let subcommand = cmd.find_subcommand(next).ok_or((query, cmd))?; | ||
find_command(&query[1..], subcommand) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.