Skip to content

Commit

Permalink
Add ruff rule --all subcommand (with JSON output support)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jun 28, 2023
1 parent 51c2934 commit 3883d60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ pub struct Args {
pub enum Command {
/// Run Ruff on the given files or directories (default).
Check(CheckArgs),
/// Explain a rule.
/// Explain a rule (or all rules).
#[clap(alias = "--explain")]
Rule {
#[arg(value_parser=Rule::from_code)]
rule: Rule,
rule: Option<Rule>,

/// Explain all rules
#[arg(long, conflicts_with = "rule")]
all: bool,

/// Output format
#[arg(long, value_enum, default_value = "text")]
Expand Down
26 changes: 25 additions & 1 deletion crates/ruff_cli/src/commands/rule.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::{self, BufWriter, Write};

use anyhow::Result;
use serde::Serialize;
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
use strum::IntoEnumIterator;

use ruff::registry::{Linter, Rule, RuleNamespace};
use ruff_diagnostics::AutofixKind;
Expand Down Expand Up @@ -88,3 +90,25 @@ pub(crate) fn rule(rule: Rule, format: HelpFormat) -> Result<()> {
};
Ok(())
}

/// Explain all rules to the user.
pub(crate) fn rules(format: HelpFormat) -> Result<()> {
let mut stdout = BufWriter::new(io::stdout().lock());
match format {
HelpFormat::Text => {
for rule in Rule::iter() {
writeln!(stdout, "{}", format_rule_text(rule))?;
writeln!(stdout)?;
}
}
HelpFormat::Json => {
let mut serializer = serde_json::Serializer::pretty(stdout);
let mut seq = serializer.serialize_seq(None)?;
for rule in Rule::iter() {
seq.serialize_element(&rule_to_explanation(&rule))?;
}
seq.end()?;
}
}
Ok(())
}
13 changes: 12 additions & 1 deletion crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,18 @@ quoting the executed command, along with the relevant file contents and `pyproje
set_up_logging(&log_level)?;

match command {
Command::Rule { rule, format } => commands::rule::rule(rule, format)?,
Command::Rule { rule, all, format } => {
if all {
commands::rule::rules(format)?
} else {
match rule {
Some(rule) => commands::rule::rule(rule, format)?,
None => {
return Err(anyhow::anyhow!("No rule or --all specified"));
}
}
}
}
Command::Config { option } => return Ok(commands::config::config(option.as_deref())),
Command::Linter { format } => commands::linter::linter(format)?,
Command::Clean => commands::clean::clean(log_level)?,
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Usage: ruff [OPTIONS] <COMMAND>
Commands:
check Run Ruff on the given files or directories (default)
rule Explain a rule
rules Explain all rules known to Ruff
config List or describe the available configuration options
linter List all supported upstream linters
clean Clear any caches in the current directory and any subdirectories
Expand Down

0 comments on commit 3883d60

Please sign in to comment.