Skip to content

Commit

Permalink
Harmonize help commands' --format to --output-format with depreca…
Browse files Browse the repository at this point in the history
…tion warnings

Warnings borrowed from astral-sh#7514

Fixes astral-sh#7990
  • Loading branch information
akx committed Oct 25, 2023
1 parent 8304c41 commit c341ed2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
40 changes: 35 additions & 5 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,36 @@ pub enum Command {
all: bool,

/// Output format
#[arg(long, value_enum, default_value = "text")]
format: HelpFormat,
#[arg(long, value_enum, default_value = "text", env = "RUFF_OUTPUT_FORMAT")]
output_format: HelpFormat,

/// Output format (Deprecated: Use `--output-format` instead).
#[arg(
long,
value_enum,
conflicts_with = "output_format",
hide = true,
env = "RUFF_FORMAT"
)]
format: Option<HelpFormat>,
},
/// List or describe the available configuration options.
Config { option: Option<String> },
/// List all supported upstream linters.
Linter {
/// Output format
#[arg(long, value_enum, default_value = "text")]
format: HelpFormat,
#[arg(long, value_enum, default_value = "text", env = "RUFF_OUTPUT_FORMAT")]
output_format: HelpFormat,

/// Output format (Deprecated: Use `--output-format` instead).
#[arg(
long,
value_enum,
conflicts_with = "output_format",
hide = true,
env = "RUFF_FORMAT"
)]
format: Option<HelpFormat>,
},
/// Clear any caches in the current directory and any subdirectories.
#[clap(alias = "--clean")]
Expand All @@ -70,8 +90,18 @@ pub enum Command {
Format(FormatCommand),
/// Display Ruff's version
Version {
#[arg(long, value_enum, default_value = "text")]
#[arg(long, value_enum, default_value = "text", env = "RUFF_OUTPUT_FORMAT")]
output_format: HelpFormat,

/// Output format (Deprecated: Use `--output-format` instead).
#[arg(
long,
value_enum,
conflicts_with = "output_format",
hide = true,
env = "RUFF_FORMAT"
)]
format: Option<HelpFormat>,
},
}

Expand Down
44 changes: 37 additions & 7 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ruff_linter::settings::types::SerializationFormat;
use ruff_linter::{fs, warn_user, warn_user_once};
use ruff_workspace::Settings;

use crate::args::{Args, CheckCommand, Command, FormatCommand};
use crate::args::{Args, CheckCommand, Command, FormatCommand, HelpFormat};
use crate::printer::{Flags as PrinterFlags, Printer};

pub mod args;
Expand Down Expand Up @@ -101,6 +101,22 @@ fn is_stdin(files: &[PathBuf], stdin_filename: Option<&Path>) -> bool {
file == Path::new("-")
}

/// Get the actual value of the `format` desired from either `output_format`
/// or `format`, and warn the user if they're using the deprecated form.
fn warn_about_deprecated_help_format(
output_format: HelpFormat,
format: Option<HelpFormat>,
) -> HelpFormat {
if format.is_some() {
if std::env::var("RUFF_FORMAT").is_ok() {
warn_user!("The environment variable `RUFF_FORMAT` is deprecated. Use `RUFF_OUTPUT_FORMAT` instead.");
} else {
warn_user!("The argument `--format=<FORMAT>` is deprecated. Use `--output-format=<FORMAT>` instead.");
}
}
return format.unwrap_or(output_format);
}

pub fn run(
Args {
command,
Expand Down Expand Up @@ -137,25 +153,39 @@ pub fn run(
set_up_logging(&log_level)?;

match command {
Command::Version { output_format } => {
Command::Version {
format,
mut output_format,
} => {
output_format = warn_about_deprecated_help_format(output_format, format);
commands::version::version(output_format)?;
Ok(ExitStatus::Success)
}
Command::Rule { rule, all, format } => {
Command::Rule {
rule,
all,
format,
mut output_format,
} => {
output_format = warn_about_deprecated_help_format(output_format, format);
if all {
commands::rule::rules(format)?;
commands::rule::rules(output_format)?;
}
if let Some(rule) = rule {
commands::rule::rule(rule, format)?;
commands::rule::rule(rule, output_format)?;
}
Ok(ExitStatus::Success)
}
Command::Config { option } => {
commands::config::config(option.as_deref())?;
Ok(ExitStatus::Success)
}
Command::Linter { format } => {
commands::linter::linter(format)?;
Command::Linter {
format,
mut output_format,
} => {
output_format = warn_about_deprecated_help_format(output_format, format);
commands::linter::linter(output_format)?;
Ok(ExitStatus::Success)
}
Command::Clean => {
Expand Down

0 comments on commit c341ed2

Please sign in to comment.