Skip to content

Commit 2e8cacb

Browse files
committed
Add error message instead of simply removing the command/options
1 parent 41d9c6e commit 2e8cacb

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

crates/ruff/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub enum Command {
9595
/// Run Ruff on the given files or directories (default).
9696
Check(CheckCommand),
9797
/// Explain a rule (or all rules).
98+
#[clap(alias = "--explain")]
9899
#[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))]
99100
Rule {
100101
/// Rule to explain
@@ -124,8 +125,10 @@ pub enum Command {
124125
output_format: HelpFormat,
125126
},
126127
/// Clear any caches in the current directory and any subdirectories.
128+
#[clap(alias = "--clean")]
127129
Clean,
128130
/// Generate shell completion.
131+
#[clap(alias = "--generate-shell-completion", hide = true)]
129132
GenerateShellCompletion { shell: clap_complete_command::Shell },
130133
/// Run the Ruff formatter on the given files or directories.
131134
Format(FormatCommand),

crates/ruff/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use std::process::ExitCode;
88
use std::sync::mpsc::channel;
99

1010
use anyhow::Result;
11-
use args::{GlobalConfigArgs, ServerCommand};
1211
use clap::CommandFactory;
1312
use colored::Colorize;
1413
use log::warn;
1514
use notify::{recommended_watcher, RecursiveMode, Watcher};
1615

16+
use args::{GlobalConfigArgs, ServerCommand};
1717
use ruff_linter::logging::{set_up_logging, LogLevel};
1818
use ruff_linter::settings::flags::FixMode;
1919
use ruff_linter::settings::types::SerializationFormat;
@@ -144,17 +144,6 @@ pub fn run(
144144
}));
145145
}
146146

147-
// Enabled ANSI colors on Windows 10.
148-
#[cfg(windows)]
149-
assert!(colored::control::set_virtual_terminal(true).is_ok());
150-
151-
// support FORCE_COLOR env var
152-
if let Some(force_color) = std::env::var_os("FORCE_COLOR") {
153-
if force_color.len() > 0 {
154-
colored::control::set_override(true);
155-
}
156-
}
157-
158147
set_up_logging(global_options.log_level())?;
159148

160149
match command {

crates/ruff/src/main.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::process::ExitCode;
22

3-
use clap::Parser;
3+
use clap::{Parser, Subcommand};
44
use colored::Colorize;
5+
use log::error;
56

6-
use ruff::args::Args;
7+
use ruff::args::{Args, Command};
78
use ruff::{run, ExitStatus};
9+
use ruff_linter::logging::{set_up_logging, LogLevel};
810

911
#[cfg(target_os = "windows")]
1012
#[global_allocator]
@@ -23,8 +25,61 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
2325
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
2426

2527
pub fn main() -> ExitCode {
28+
// Enabled ANSI colors on Windows 10.
29+
#[cfg(windows)]
30+
assert!(colored::control::set_virtual_terminal(true).is_ok());
31+
32+
// support FORCE_COLOR env var
33+
if let Some(force_color) = std::env::var_os("FORCE_COLOR") {
34+
if force_color.len() > 0 {
35+
colored::control::set_override(true);
36+
}
37+
}
38+
2639
let args = wild::args_os();
2740
let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap();
41+
42+
// We can't use `warn_user` here because logging isn't set up at this point
43+
// and we also don't know if the user runs ruff with quiet.
44+
// Keep the message and pass it to `run` that is responsible for emitting the warning.
45+
let deprecated_alias_error = match args.get(1).and_then(|arg| arg.to_str()) {
46+
// Deprecated aliases that are handled by clap
47+
Some("--explain") => {
48+
Some("`ruff --explain <RULE>` has been removed. Use `ruff rule <RULE>` instead.")
49+
}
50+
Some("--clean") => {
51+
Some("`ruff --clean` has been removed. Use `ruff clean` instead.")
52+
}
53+
Some("--generate-shell-completion") => {
54+
Some("`ruff --generate-shell-completion <SHELL>` has been removed. Use `ruff generate-shell-completion <SHELL>` instead.")
55+
}
56+
// Deprecated `ruff` alias to `ruff check`
57+
// Clap doesn't support default subcommands but we want to run `check` by
58+
// default for convenience and backwards-compatibility, so we just
59+
// preprocess the arguments accordingly before passing them to Clap.
60+
Some(arg) if !Command::has_subcommand(arg)
61+
&& arg != "-h"
62+
&& arg != "--help"
63+
&& arg != "-V"
64+
&& arg != "--version"
65+
&& arg != "help" => {
66+
{
67+
Some("`ruff <path>` has been removed. Use `ruff check <path>` instead.")
68+
}
69+
},
70+
_ => None
71+
};
72+
73+
if let Some(error) = deprecated_alias_error {
74+
#[allow(clippy::print_stderr)]
75+
if set_up_logging(LogLevel::Default).is_ok() {
76+
error!("{}", error);
77+
} else {
78+
eprintln!("{}", error.red().bold());
79+
}
80+
return ExitCode::FAILURE;
81+
}
82+
2883
let args = Args::parse_from(args);
2984

3085
match run(args) {

crates/ruff/tests/integration_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ fn unreadable_pyproject_toml() -> Result<()> {
13781378
// Don't `--isolated` since the configuration discovery is where the error happens
13791379
let args = Args::parse_from(["", "check", "--no-cache", tempdir.path().to_str().unwrap()]);
13801380
let err = run(args).err().context("Unexpected success")?;
1381+
13811382
assert_eq!(
13821383
err.chain()
13831384
.map(std::string::ToString::to_string)

docs/configuration.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -521,21 +521,15 @@ Ruff: An extremely fast Python linter.
521521
Usage: ruff [OPTIONS] <COMMAND>
522522
523523
Commands:
524-
check Run Ruff on the given files or directories
525-
(default)
526-
rule Explain a rule (or all rules)
527-
config List or describe the available configuration
528-
options
529-
linter List all supported upstream linters
530-
clean Clear any caches in the current directory and
531-
any subdirectories
532-
generate-shell-completion Generate shell completion
533-
format Run the Ruff formatter on the given files or
534-
directories
535-
server Run the language server
536-
version Display Ruff's version
537-
help Print this message or the help of the given
538-
subcommand(s)
524+
check Run Ruff on the given files or directories (default)
525+
rule Explain a rule (or all rules)
526+
config List or describe the available configuration options
527+
linter List all supported upstream linters
528+
clean Clear any caches in the current directory and any subdirectories
529+
format Run the Ruff formatter on the given files or directories
530+
server Run the language server
531+
version Display Ruff's version
532+
help Print this message or the help of the given subcommand(s)
539533
540534
Options:
541535
-h, --help Print help

0 commit comments

Comments
 (0)