Skip to content

Commit

Permalink
Upgrade to clap 3.2 (#431)
Browse files Browse the repository at this point in the history
* chore: Upgrade to clap 3.2

* chore(cli): Resolve clap deprecations
  • Loading branch information
epage committed Jun 15, 2022
1 parent d8168c6 commit ecdd823
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 82 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bitvec = { version = "1.0", features = ["alloc"] }
# Allows us to do eg cargo metadata operations without relying on an external cargo
cargo = { version = "0.61", optional = true }
# Argument parsing
clap = { version = "3.1", features = ["derive", "env"] }
clap = { version = "3.2.1", features = ["derive", "env"] }
# Used for diagnostic reporting
codespan = "0.11"
codespan-reporting = "0.11"
Expand Down
16 changes: 8 additions & 8 deletions src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use log::error;
use serde::Deserialize;
use std::{path::PathBuf, time::Instant};

#[derive(clap::ArgEnum, Debug, PartialEq, Copy, Clone)]
#[derive(clap::ValueEnum, Debug, PartialEq, Copy, Clone)]
pub enum WhichCheck {
Advisories,
Ban,
Expand All @@ -25,33 +25,33 @@ pub struct Args {
/// Path to the config to use
///
/// Defaults to <cwd>/deny.toml if not specified
#[clap(short, long, parse(from_os_str))]
#[clap(short, long, action)]
pub config: Option<PathBuf>,
/// Path to graph_output root directory
///
/// If set, a dotviz graph will be created for whenever multiple versions of the same crate are detected.
///
/// Each file will be created at <dir>/graph_output/<crate_name>.dot. <dir>/graph_output/* is deleted and recreated each run.
#[clap(short, long, parse(from_os_str))]
#[clap(short, long, action)]
pub graph: Option<PathBuf>,
/// Hides the inclusion graph when printing out info for a crate
#[clap(long)]
#[clap(long, action)]
pub hide_inclusion_graph: bool,
/// Disable fetching of the advisory database
///
/// When running the `advisories` check, the configured advisory database will be fetched and opened. If this flag is passed, the database won't be fetched, but an error will occur if it doesn't already exist locally.
#[clap(short, long)]
#[clap(short, long, action)]
pub disable_fetch: bool,
/// To ease transition from cargo-audit to cargo-deny, this flag will tell cargo-deny to output the exact same output as cargo-audit would, to `stdout` instead of `stderr`, just as with cargo-audit.
///
/// Note that this flag only applies when the output format is JSON, and note that since cargo-deny supports multiple advisory databases, instead of a single JSON object, there will be 1 for each unique advisory database.
#[clap(long)]
#[clap(long, action)]
pub audit_compatible_output: bool,
/// Show stats for all the checks, regardless of the log-level
#[clap(short, long = "show-stats")]
#[clap(short, long, action)]
pub show_stats: bool,
/// The check(s) to perform
#[clap(arg_enum)]
#[clap(value_enum, action)]
pub which: Vec<WhichCheck>,
}

Expand Down
6 changes: 3 additions & 3 deletions src/cargo-deny/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cargo_deny::{
};
use std::path::PathBuf;

#[derive(clap::ArgEnum, Debug, PartialEq, Copy, Clone)]
#[derive(clap::ValueEnum, Debug, PartialEq, Copy, Clone)]
pub enum FetchSource {
Db,
Index,
Expand All @@ -17,10 +17,10 @@ pub struct Args {
/// Path to the config to use
///
/// Defaults to <cwd>/deny.toml if not specified
#[clap(short, long, parse(from_os_str))]
#[clap(short, long, action)]
config: Option<PathBuf>,
/// The sources to fetch
#[clap(arg_enum)]
#[clap(value_enum, action)]
sources: Vec<FetchSource>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo-deny/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Args {
/// The path to create
///
/// Defaults to <cwd>/deny.toml
#[clap(parse(from_os_str))]
#[clap(action)]
config: Option<PathBuf>,
}

Expand Down
12 changes: 6 additions & 6 deletions src/cargo-deny/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use cargo_deny::{diag::Files, licenses, Kid};
use serde::Serialize;
use std::path::PathBuf;

#[derive(clap::ArgEnum, Copy, Clone, Debug)]
#[derive(clap::ValueEnum, Copy, Clone, Debug)]
pub enum Layout {
Crate,
License,
}

#[derive(clap::ArgEnum, Copy, Clone, Debug)]
#[derive(clap::ValueEnum, Copy, Clone, Debug)]
pub enum OutputFormat {
Human,
Json,
Expand All @@ -22,20 +22,20 @@ pub struct Args {
/// Path to the config to use
///
/// Defaults to a deny.toml in the same folder as the manifest path, or a deny.toml in a parent directory.
#[clap(short, long, parse(from_os_str))]
#[clap(short, long, action)]
config: Option<PathBuf>,
/// Minimum confidence threshold for license text
///
/// When determining the license from file contents, a confidence score is assigned according to how close the contents are to the canonical license text. If the confidence score is below this threshold, they license text will ignored, which might mean the crate is treated as unlicensed.
///
/// [possible values: 0.0 - 1.0]
#[clap(short, long, default_value = "0.8")]
#[clap(short, long, default_value = "0.8", action)]
threshold: f32,
/// The format of the output
#[clap(short, long, default_value = "human", arg_enum)]
#[clap(short, long, default_value = "human", value_enum, action)]
format: OutputFormat,
/// The layout for the output, does not apply to TSV
#[clap(short, long, default_value = "license", arg_enum)]
#[clap(short, long, default_value = "license", value_enum, action)]
layout: Layout,
}

Expand Down
80 changes: 23 additions & 57 deletions src/cargo-deny/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
#![allow(clippy::exit, clippy::single_match_else)]

use anyhow::{bail, Context, Error};
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

mod check;
Expand All @@ -106,60 +106,19 @@ enum Command {
List(list::Args),
}

#[derive(Parser, Copy, Clone, Debug, PartialEq)]
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq)]
pub enum Format {
Human,
Json,
}

impl Format {
fn variants() -> &'static [&'static str] {
&["human", "json"]
}
}

impl std::str::FromStr for Format {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let lower = s.to_ascii_lowercase();

Ok(match lower.as_str() {
"human" => Self::Human,
"json" => Self::Json,
_ => bail!("unknown output format '{}' specified", s),
})
}
}

#[derive(Parser, Copy, Clone, Debug)]
#[derive(ValueEnum, Copy, Clone, Debug)]
pub enum Color {
Auto,
Always,
Never,
}

impl Color {
fn variants() -> &'static [&'static str] {
&["auto", "always", "never"]
}
}

impl std::str::FromStr for Color {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let lower = s.to_ascii_lowercase();

Ok(match lower.as_str() {
"auto" => Self::Auto,
"always" => Self::Always,
"never" => Self::Never,
_ => bail!("unknown color option '{}' specified", s),
})
}
}

fn parse_level(s: &str) -> Result<log::LevelFilter, Error> {
s.parse::<log::LevelFilter>()
.with_context(|| format!("failed to parse level '{}'", s))
Expand All @@ -171,42 +130,42 @@ pub(crate) struct GraphContext {
/// The path of a Cargo.toml to use as the context for the operation.
///
/// By default, the Cargo.toml in the current working directory is used.
#[clap(long, parse(from_os_str))]
#[clap(long, action)]
pub(crate) manifest_path: Option<PathBuf>,
/// If passed, all workspace packages are used as roots for the crate graph.
///
/// Automatically assumed if the manifest path points to a virtual manifest.
///
/// Normally, if you specify a manifest path that is a member of a workspace, that crate will be the sole root of the crate graph, meaning only other workspace members that are dependencies of that workspace crate will be included in the graph. This overrides that behavior to include all workspace members.
#[clap(long)]
#[clap(long, action)]
pub(crate) workspace: bool,
/// One or more crates to exclude from the crate graph that is used.
///
/// NOTE: Unlike cargo, this does not have to be used with the `--workspace` flag.
#[clap(long)]
#[clap(long, action)]
pub(crate) exclude: Vec<String>,
/// One or more platforms to filter crates by
///
/// If a dependency is target specific, it will be ignored if it does not match 1 or more of the specified targets. This option overrides the top-level `targets = []` configuration value.
#[clap(short, long)]
#[clap(short, long, action)]
pub(crate) target: Vec<String>,
/// Activate all available features
#[clap(long)]
#[clap(long, action)]
pub(crate) all_features: bool,
/// Do not activate the `default` feature
#[clap(long)]
#[clap(long, action)]
pub(crate) no_default_features: bool,
/// Space or comma separated list of features to activate
#[clap(long, use_value_delimiter = true)]
#[clap(long, use_value_delimiter = true, action)]
pub(crate) features: Vec<String>,
/// Require Cargo.lock and cache are up to date
#[clap(long)]
#[clap(long, action)]
pub(crate) frozen: bool,
/// Require Cargo.lock is up to date
#[clap(long)]
#[clap(long, action)]
pub(crate) locked: bool,
/// Run without accessing the network. If used with the `check` subcommand, this also disables advisory database fetching.
#[clap(long)]
#[clap(long, action)]
pub(crate) offline: bool,
}

Expand All @@ -219,7 +178,7 @@ struct Opts {
short = 'L',
long = "log-level",
default_value = "warn",
parse(try_from_str = parse_level),
value_parser = parse_level,
long_help = "The log level for messages
Only log messages at or above the level will be emitted.
Expand All @@ -234,9 +193,16 @@ Possible values:
")]
log_level: log::LevelFilter,
/// Specify the format of cargo-deny's output
#[clap(short, long, default_value = "human", possible_values = Format::variants())]
#[clap(short, long, default_value = "human", value_enum, action)]
format: Format,
#[clap(short, long, default_value = "auto", possible_values = Color::variants(), env = "CARGO_TERM_COLOR")]
#[clap(
short,
long,
default_value = "auto",
value_enum,
env = "CARGO_TERM_COLOR",
action
)]
color: Color,
#[clap(flatten)]
ctx: GraphContext,
Expand Down

0 comments on commit ecdd823

Please sign in to comment.