Remove check, --explain, --clean, --generate-shell-completion aliases (#12011)

This commit is contained in:
Micha Reiser 2024-06-25 09:23:20 +02:00
parent d6a2cad9c2
commit 36a9efdb48
4 changed files with 34 additions and 31 deletions

View file

@ -95,7 +95,6 @@ pub enum Command {
/// Run Ruff on the given files or directories (default). /// Run Ruff on the given files or directories (default).
Check(CheckCommand), Check(CheckCommand),
/// Explain a rule (or all rules). /// Explain a rule (or all rules).
#[clap(alias = "--explain")]
#[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))] #[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))]
Rule { Rule {
/// Rule to explain /// Rule to explain
@ -125,10 +124,9 @@ pub enum Command {
output_format: HelpFormat, output_format: HelpFormat,
}, },
/// Clear any caches in the current directory and any subdirectories. /// Clear any caches in the current directory and any subdirectories.
#[clap(alias = "--clean")]
Clean, Clean,
/// Generate shell completion. /// Generate shell completion.
#[clap(alias = "--generate-shell-completion", hide = true)] #[clap(hide = true)]
GenerateShellCompletion { shell: clap_complete_command::Shell }, GenerateShellCompletion { shell: clap_complete_command::Shell },
/// Run the Ruff formatter on the given files or directories. /// Run the Ruff formatter on the given files or directories.
Format(FormatCommand), Format(FormatCommand),

View file

@ -8,12 +8,12 @@ use std::process::ExitCode;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use anyhow::Result; use anyhow::Result;
use args::{GlobalConfigArgs, ServerCommand};
use clap::CommandFactory; use clap::CommandFactory;
use colored::Colorize; use colored::Colorize;
use log::warn; use log::warn;
use notify::{recommended_watcher, RecursiveMode, Watcher}; use notify::{recommended_watcher, RecursiveMode, Watcher};
use args::{GlobalConfigArgs, ServerCommand};
use ruff_linter::logging::{set_up_logging, LogLevel}; use ruff_linter::logging::{set_up_logging, LogLevel};
use ruff_linter::settings::flags::FixMode; use ruff_linter::settings::flags::FixMode;
use ruff_linter::settings::types::OutputFormat; use ruff_linter::settings::types::OutputFormat;
@ -121,7 +121,6 @@ pub fn run(
command, command,
global_options, global_options,
}: Args, }: Args,
deprecated_alias_warning: Option<&'static str>,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
{ {
let default_panic_hook = std::panic::take_hook(); let default_panic_hook = std::panic::take_hook();
@ -145,23 +144,8 @@ pub fn run(
})); }));
} }
// Enabled ANSI colors on Windows 10.
#[cfg(windows)]
assert!(colored::control::set_virtual_terminal(true).is_ok());
// support FORCE_COLOR env var
if let Some(force_color) = std::env::var_os("FORCE_COLOR") {
if force_color.len() > 0 {
colored::control::set_override(true);
}
}
set_up_logging(global_options.log_level())?; set_up_logging(global_options.log_level())?;
if let Some(deprecated_alias_warning) = deprecated_alias_warning {
warn_user!("{}", deprecated_alias_warning);
}
match command { match command {
Command::Version { output_format } => { Command::Version { output_format } => {
commands::version::version(output_format)?; commands::version::version(output_format)?;

View file

@ -2,9 +2,11 @@ use std::process::ExitCode;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use colored::Colorize; use colored::Colorize;
use log::error;
use ruff::args::{Args, Command}; use ruff::args::{Args, Command};
use ruff::{run, ExitStatus}; use ruff::{run, ExitStatus};
use ruff_linter::logging::{set_up_logging, LogLevel};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
#[global_allocator] #[global_allocator]
@ -23,23 +25,33 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
pub fn main() -> ExitCode { pub fn main() -> ExitCode {
// Enabled ANSI colors on Windows 10.
#[cfg(windows)]
assert!(colored::control::set_virtual_terminal(true).is_ok());
// support FORCE_COLOR env var
if let Some(force_color) = std::env::var_os("FORCE_COLOR") {
if force_color.len() > 0 {
colored::control::set_override(true);
}
}
let args = wild::args_os(); let args = wild::args_os();
let mut args = let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap();
argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap();
// We can't use `warn_user` here because logging isn't set up at this point // We can't use `warn_user` here because logging isn't set up at this point
// and we also don't know if the user runs ruff with quiet. // and we also don't know if the user runs ruff with quiet.
// Keep the message and pass it to `run` that is responsible for emitting the warning. // Keep the message and pass it to `run` that is responsible for emitting the warning.
let deprecated_alias_warning = match args.get(1).and_then(|arg| arg.to_str()) { let deprecated_alias_error = match args.get(1).and_then(|arg| arg.to_str()) {
// Deprecated aliases that are handled by clap // Deprecated aliases that are handled by clap
Some("--explain") => { Some("--explain") => {
Some("`ruff --explain <RULE>` is deprecated. Use `ruff rule <RULE>` instead.") Some("`ruff --explain <RULE>` has been removed. Use `ruff rule <RULE>` instead.")
} }
Some("--clean") => { Some("--clean") => {
Some("`ruff --clean` is deprecated. Use `ruff clean` instead.") Some("`ruff --clean` has been removed. Use `ruff clean` instead.")
} }
Some("--generate-shell-completion") => { Some("--generate-shell-completion") => {
Some("`ruff --generate-shell-completion <SHELL>` is deprecated. Use `ruff generate-shell-completion <SHELL>` instead.") Some("`ruff --generate-shell-completion <SHELL>` has been removed. Use `ruff generate-shell-completion <SHELL>` instead.")
} }
// Deprecated `ruff` alias to `ruff check` // Deprecated `ruff` alias to `ruff check`
// Clap doesn't support default subcommands but we want to run `check` by // Clap doesn't support default subcommands but we want to run `check` by
@ -51,18 +63,26 @@ pub fn main() -> ExitCode {
&& arg != "-V" && arg != "-V"
&& arg != "--version" && arg != "--version"
&& arg != "help" => { && arg != "help" => {
{ {
args.insert(1, "check".into()); Some("`ruff <path>` has been removed. Use `ruff check <path>` instead.")
Some("`ruff <path>` is deprecated. Use `ruff check <path>` instead.")
} }
}, },
_ => None _ => None
}; };
if let Some(error) = deprecated_alias_error {
#[allow(clippy::print_stderr)]
if set_up_logging(LogLevel::Default).is_ok() {
error!("{}", error);
} else {
eprintln!("{}", error.red().bold());
}
return ExitCode::FAILURE;
}
let args = Args::parse_from(args); let args = Args::parse_from(args);
match run(args, deprecated_alias_warning) { match run(args) {
Ok(code) => code.into(), Ok(code) => code.into(),
Err(err) => { Err(err) => {
#[allow(clippy::print_stderr)] #[allow(clippy::print_stderr)]

View file

@ -1298,7 +1298,8 @@ fn unreadable_pyproject_toml() -> Result<()> {
// Don't `--isolated` since the configuration discovery is where the error happens // Don't `--isolated` since the configuration discovery is where the error happens
let args = Args::parse_from(["", "check", "--no-cache", tempdir.path().to_str().unwrap()]); let args = Args::parse_from(["", "check", "--no-cache", tempdir.path().to_str().unwrap()]);
let err = run(args, None).err().context("Unexpected success")?; let err = run(args).err().context("Unexpected success")?;
assert_eq!( assert_eq!(
err.chain() err.chain()
.map(std::string::ToString::to_string) .map(std::string::ToString::to_string)