Make Clap an optional feature for ruff crate (#3498)

This commit is contained in:
Charlie Marsh 2023-03-14 11:02:05 -04:00 committed by GitHub
parent 78c2b0ac47
commit 106a93eab0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 41 deletions

15
Cargo.lock generated
View file

@ -313,13 +313,14 @@ dependencies = [
[[package]]
name = "clap_complete_command"
version = "0.4.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4160b4a4f72ef58bd766bad27c09e6ef1cc9d82a22f6a0f55d152985a4a48e31"
checksum = "183495371ea78d4c9ff638bfc6497d46fed2396e4f9c50aebc1278a4a9919a3d"
dependencies = [
"clap 4.1.8",
"clap_complete",
"clap_complete_fig",
"clap_complete_nushell",
]
[[package]]
@ -332,6 +333,16 @@ dependencies = [
"clap_complete",
]
[[package]]
name = "clap_complete_nushell"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fa41f5e6aa83bd151b70fd0ceaee703d68cd669522795dc812df9edad1252c"
dependencies = [
"clap 4.1.8",
"clap_complete",
]
[[package]]
name = "clap_derive"
version = "4.1.8"

View file

@ -25,7 +25,7 @@ anyhow = { workspace = true }
bisection = { version = "0.1.0" }
bitflags = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true, features = ["derive", "env", "string"] }
clap = { workspace = true, features = ["derive", "string"], optional = true }
colored = { workspace = true }
dirs = { version = "4.0.0" }
fern = { version = "0.6.1" }
@ -72,7 +72,6 @@ insta = { workspace = true, features = ["yaml", "redactions"] }
pretty_assertions = "1.3.0"
test-case = { workspace = true }
[features]
default = []
logical_lines = []

View file

@ -277,6 +277,7 @@ pub(crate) enum Specificity {
Code5Chars,
}
#[cfg(feature = "clap")]
mod clap_completion {
use clap::builder::{PossibleValue, TypedValueParser, ValueParserFactory};
use strum::IntoEnumIterator;
@ -316,9 +317,7 @@ mod clap_completion {
.map_err(|e| clap::Error::raw(clap::error::ErrorKind::InvalidValue, e))
}
fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {
Some(Box::new(
std::iter::once(PossibleValue::new("ALL").help("all rules")).chain(
Linter::iter()

View file

@ -4,21 +4,21 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::string::ToString;
use anyhow::{anyhow, bail, Result};
use clap::ValueEnum;
use anyhow::{bail, Result};
use globset::{Glob, GlobSet, GlobSetBuilder};
use pep440_rs::{Version as Pep440Version, VersionSpecifiers};
use ruff_cache::{CacheKey, CacheKeyHasher};
use ruff_macros::CacheKey;
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::{de, Deserialize, Deserializer, Serialize};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use ruff_cache::{CacheKey, CacheKeyHasher};
use ruff_macros::CacheKey;
use crate::fs;
use crate::registry::Rule;
use crate::rule_selector::RuleSelector;
use crate::{fs, warn_user_once};
#[derive(
Clone,
@ -34,6 +34,7 @@ use crate::{fs, warn_user_once};
CacheKey,
EnumIter,
)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "lowercase")]
pub enum PythonVersion {
Py37,
@ -43,28 +44,6 @@ pub enum PythonVersion {
Py311,
}
impl FromStr for PythonVersion {
type Err = anyhow::Error;
fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
"py33" | "py34" | "py35" | "py36" => {
warn_user_once!(
"Specified a version below the minimum supported Python version. Defaulting \
to Python 3.7."
);
Ok(Self::Py37)
}
"py37" => Ok(Self::Py37),
"py38" => Ok(Self::Py38),
"py39" => Ok(Self::Py39),
"py310" => Ok(Self::Py310),
"py311" => Ok(Self::Py311),
_ => Err(anyhow!("Unknown version: {string} (try: \"py37\")")),
}
}
}
impl From<PythonVersion> for Pep440Version {
fn from(version: PythonVersion) -> Self {
let (major, minor) = version.as_tuple();
@ -240,9 +219,8 @@ impl FromStr for PatternPrefixPair {
}
}
#[derive(
Clone, Copy, ValueEnum, PartialEq, Eq, Serialize, Deserialize, Debug, JsonSchema, Hash,
)]
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Debug, JsonSchema, Hash)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[serde(rename_all = "kebab-case")]
pub enum SerializationFormat {
Text,

View file

@ -22,7 +22,7 @@ name = "ruff"
doc = false
[dependencies]
ruff = { path = "../ruff" }
ruff = { path = "../ruff", features = ["clap"] }
ruff_cache = { path = "../ruff_cache" }
ruff_diagnostics = { path = "../ruff_diagnostics" }
@ -34,7 +34,7 @@ bitflags = { workspace = true }
cachedir = { version = "0.3.0" }
chrono = { workspace = true }
clap = { workspace = true, features = ["derive", "env"] }
clap_complete_command = { version = "0.4.0" }
clap_complete_command = { version = "0.5.1" }
clearscreen = { version = "2.0.0" }
colored = { workspace = true }
filetime = { workspace = true }

View file

@ -99,7 +99,7 @@ pub struct CheckArgs {
#[arg(long, value_enum, env = "RUFF_FORMAT")]
pub format: Option<SerializationFormat>,
/// The minimum Python version that should be supported.
#[arg(long)]
#[arg(long, value_enum)]
pub target_version: Option<PythonVersion>,
/// Path to the `pyproject.toml` or `ruff.toml` file to use for
/// configuration.

View file

@ -211,7 +211,7 @@ Options:
--format <FORMAT>
Output serialization format for violations [env: RUFF_FORMAT=] [possible values: text, json, junit, grouped, github, gitlab, pylint, azure]
--target-version <TARGET_VERSION>
The minimum Python version that should be supported
The minimum Python version that should be supported [possible values: py37, py38, py39, py310, py311]
--config <CONFIG>
Path to the `pyproject.toml` or `ruff.toml` file to use for configuration
--statistics