mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Implement config subcommand
The synopsis is as follows. List all top-level config keys: $ ruff config allowed-confusables builtins cache-dir ... etc. List all config keys in a specific section: $ ruff config mccabe max-complexity Describe a specific config option: $ ruff config mccabe.max-complexity The maximum McCabe complexity to allow before triggering `C901` errors. Default value: 10 Type: int Example usage: ```toml # Flag errors (`C901`) whenever the complexity level exceeds 5. max-complexity = 5 ```
This commit is contained in:
parent
bbe44360e8
commit
0e4d5eeea7
5 changed files with 53 additions and 0 deletions
|
@ -393,6 +393,7 @@ Usage: ruff [OPTIONS] <COMMAND>
|
||||||
Commands:
|
Commands:
|
||||||
check Run Ruff on the given files or directories (default)
|
check Run Ruff on the given files or directories (default)
|
||||||
rule Explain a rule
|
rule Explain a rule
|
||||||
|
config List or describe the available configuration options
|
||||||
linter List all supported upstream linters
|
linter List all supported upstream linters
|
||||||
clean Clear any caches in the current directory and any subdirectories
|
clean Clear any caches in the current directory and any subdirectories
|
||||||
help Print this message or the help of the given subcommand(s)
|
help Print this message or the help of the given subcommand(s)
|
||||||
|
|
|
@ -44,6 +44,8 @@ pub enum Command {
|
||||||
#[arg(long, value_enum, default_value = "pretty")]
|
#[arg(long, value_enum, default_value = "pretty")]
|
||||||
format: HelpFormat,
|
format: HelpFormat,
|
||||||
},
|
},
|
||||||
|
/// List or describe the available configuration options
|
||||||
|
Config { option: Option<String> },
|
||||||
/// List all supported upstream linters
|
/// List all supported upstream linters
|
||||||
Linter {
|
Linter {
|
||||||
/// Output format
|
/// Output format
|
||||||
|
|
48
crates/ruff_cli/src/commands/config.rs
Normal file
48
crates/ruff_cli/src/commands/config.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
use ruff::settings::{
|
||||||
|
options::Options,
|
||||||
|
options_base::{ConfigurationOptions, OptionEntry, OptionField},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::ExitStatus;
|
||||||
|
|
||||||
|
#[allow(clippy::print_stdout)]
|
||||||
|
pub(crate) fn config(option: Option<&str>) -> ExitStatus {
|
||||||
|
let entries = Options::get_available_options();
|
||||||
|
let mut entries = &entries;
|
||||||
|
|
||||||
|
let mut parts_iter = option.iter().flat_map(|s| s.split('.'));
|
||||||
|
|
||||||
|
while let Some(part) = parts_iter.next() {
|
||||||
|
let Some((_, field)) = entries.iter().find(|(name, _)| *name == part) else {
|
||||||
|
println!("Unknown option");
|
||||||
|
return ExitStatus::Error;
|
||||||
|
};
|
||||||
|
match field {
|
||||||
|
OptionEntry::Field(OptionField {
|
||||||
|
doc,
|
||||||
|
default,
|
||||||
|
value_type,
|
||||||
|
example,
|
||||||
|
}) => {
|
||||||
|
if parts_iter.next().is_some() {
|
||||||
|
println!("Unknown option");
|
||||||
|
return ExitStatus::Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{doc}");
|
||||||
|
println!();
|
||||||
|
println!("Default value: {default}");
|
||||||
|
println!("Type: {value_type}");
|
||||||
|
println!("Example usage:\n```toml\n{example}\n```");
|
||||||
|
return ExitStatus::Success;
|
||||||
|
}
|
||||||
|
OptionEntry::Group(fields) => {
|
||||||
|
entries = fields;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (name, _) in entries {
|
||||||
|
println!("{name}");
|
||||||
|
}
|
||||||
|
ExitStatus::Success
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ pub use show_settings::show_settings;
|
||||||
|
|
||||||
mod add_noqa;
|
mod add_noqa;
|
||||||
mod clean;
|
mod clean;
|
||||||
|
pub mod config;
|
||||||
mod linter;
|
mod linter;
|
||||||
mod rule;
|
mod rule;
|
||||||
mod run;
|
mod run;
|
||||||
|
|
|
@ -101,6 +101,7 @@ quoting the executed command, along with the relevant file contents and `pyproje
|
||||||
|
|
||||||
match command {
|
match command {
|
||||||
Command::Rule { rule, format } => commands::rule(&rule, format)?,
|
Command::Rule { rule, format } => commands::rule(&rule, format)?,
|
||||||
|
Command::Config { option } => return Ok(commands::config::config(option.as_deref())),
|
||||||
Command::Linter { format } => commands::linter(format)?,
|
Command::Linter { format } => commands::linter(format)?,
|
||||||
Command::Clean => commands::clean(log_level)?,
|
Command::Clean => commands::clean(log_level)?,
|
||||||
Command::GenerateShellCompletion { shell } => {
|
Command::GenerateShellCompletion { shell } => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue