Skip to content

Commit

Permalink
Implement config subcommand
Browse files Browse the repository at this point in the history
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
    ```
  • Loading branch information
not-my-profile authored and charliermarsh committed Feb 12, 2023
1 parent bbe4436 commit 0e4d5ee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ Usage: ruff [OPTIONS] <COMMAND>
Commands:
check Run Ruff on the given files or directories (default)
rule Explain a rule
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
help Print this message or the help of the given subcommand(s)
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum Command {
#[arg(long, value_enum, default_value = "pretty")]
format: HelpFormat,
},
/// List or describe the available configuration options
Config { option: Option<String> },
/// List all supported upstream linters
Linter {
/// Output format
Expand Down
48 changes: 48 additions & 0 deletions crates/ruff_cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions crates/ruff_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use show_settings::show_settings;

mod add_noqa;
mod clean;
pub mod config;
mod linter;
mod rule;
mod run;
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ quoting the executed command, along with the relevant file contents and `pyproje

match command {
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::Clean => commands::clean(log_level)?,
Command::GenerateShellCompletion { shell } => {
Expand Down

0 comments on commit 0e4d5ee

Please sign in to comment.