Skip to content

Commit

Permalink
Add --exclude-features as an alias of --skip
Browse files Browse the repository at this point in the history
* Add `--exclude-features` option. This is an alias of `--skip` option.
* Rename `--skip-no-default-features` flag to `--exclude-no-default-features`.
  The old name can be used as an alias, but is deprecated.
* Rename `--skip-all-features` flag to `--exclude-all-features`.
  • Loading branch information
taiki-e committed Oct 17, 2020
1 parent 301df44 commit 1cd643d
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 98 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ This project adheres to [Semantic Versioning](https://semver.org).

* Treat `--all-features` flag as one of feature combinations. See [#42][42] for details.

* Add `--skip-all-features` flag. See [#42][42] for details.
* Add `--exclude-all-features` flag. See [#42][42] for details.

* Add `--exclude-features` option. This is an alias of `--skip` option.

* Rename `--skip-no-default-features` flag to `--exclude-no-default-features`.
The old name can be used as an alias, but is deprecated.

* Fix an issue where using `--features` with `--each-feature` or `--feature-powerset` together would result in the same feature combination being performed multiple times.

Expand Down
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,9 @@ The following flags can be used with `--each-feature` and `--feature-powerset`.

Use optional dependencies as features.

* **`--skip`**
* **`--exclude-features`**, **`--skip`**

Space-separated list of features to skip.

To skip run of default feature, using value `--skip default`.

* **`--skip-no-default-features`**

Skip run of just `--no-default-features` flag.
Space-separated list of features to exclude.

* **`--depth`**

Expand Down
93 changes: 63 additions & 30 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use termcolor::ColorChoice;
use crate::{ProcessBuilder, Result};

fn print_version() {
println!("{0} {1}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),)
println!("{0} {1}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
}

// (short flag, long flag, short descriptions, additional descriptions)
Expand All @@ -28,14 +28,15 @@ const HELP: &[(&str, &str, &str, &[&str])] = &[
"If DEPS are not specified, all optional dependencies are considered as features.",
"This flag can only be used together with either --each-feature flag or --feature-powerset flag.",
]),
("", "--skip <FEATURES>...", "Space-separated list of features to skip", &[
"To skip run of default feature, using value `--skip default`.",
("", "--skip <FEATURES>...", "Alias for --exclude-features", &[]),
("", "--exclude-features <FEATURES>...", "Space-separated list of features to exclude", &[
"To exclude run of default feature, using value `--exclude-features default`.",
"This flag can only be used together with either --each-feature flag or --feature-powerset flag.",
]),
("", "--skip-no-default-features", "Skip run of just --no-default-features flag", &[
("", "--exclude-no-default-features", "Exclude run of just --no-default-features flag", &[
"This flag can only be used together with either --each-feature flag or --feature-powerset flag.",
]),
("", "--skip-all-features", "Skip run of just --all-features flag", &[
("", "--exclude-all-features", "Exclude run of just --all-features flag", &[
"This flag can only be used together with either --each-feature flag or --feature-powerset flag.",
]),
(
Expand Down Expand Up @@ -143,8 +144,8 @@ OPTIONS:",
writeln!(f, "\n")?;
}
} else {
write!(f, "{:26} ", long)?;
write(f, 35, false, self.term_size, desc)?;
write!(f, "{:32} ", long)?;
write(f, 41, false, self.term_size, desc)?;
writeln!(f)?;
}
}
Expand Down Expand Up @@ -204,12 +205,12 @@ pub(crate) struct Args {

// Note: These values are not always exactly the same as the input.
// Error messages should not assume that these options have been specified.
/// --skip <FEATURES>...
pub(crate) skip: Vec<String>,
/// --skip-no-default-features
pub(crate) skip_no_default_features: bool,
/// --skip-all-features
pub(crate) skip_all_features: bool,
/// --exclude-features <FEATURES>..., --skip <FEATURES>...
pub(crate) exclude_features: Vec<String>,
/// --exclude-no-default-features, (--skip-no-default-features)
pub(crate) exclude_no_default_features: bool,
/// --exclude-all-features
pub(crate) exclude_all_features: bool,

// flags that will be propagated to cargo
/// --features <FEATURES>...
Expand Down Expand Up @@ -276,7 +277,6 @@ pub(crate) fn args(coloring: &mut Option<Coloring>) -> Result<Option<Args>> {
let mut package = Vec::new();
let mut exclude = Vec::new();
let mut features = Vec::new();
let mut skip = Vec::new();
let mut optional_deps = None;

let mut workspace = None;
Expand All @@ -286,11 +286,14 @@ pub(crate) fn args(coloring: &mut Option<Coloring>) -> Result<Option<Args>> {
let mut feature_powerset = false;
let mut ignore_private = false;
let mut ignore_unknown_features = false;
let mut skip_no_default_features = false;
let mut skip_all_features = false;
let mut clean_per_run = false;
let mut depth = None;

let mut exclude_features = Vec::new();
let mut exclude_no_default_features = false;
let mut exclude_all_features = false;
let mut skip_no_default_features = false;

let mut verbose = false;
let mut no_default_features = false;
let mut all_features = false;
Expand Down Expand Up @@ -397,7 +400,14 @@ pub(crate) fn args(coloring: &mut Option<Coloring>) -> Result<Option<Args>> {
parse_multi_opt!(package, false, true, "-p", "--package <SPEC>...");
parse_multi_opt!(exclude, false, true, "--exclude", "--exclude <SPEC>...");
parse_multi_opt!(features, true, true, "--features", "--features <FEATURES>...");
parse_multi_opt!(skip, true, true, "--skip", "--skip <FEATURES>...");
parse_multi_opt!(exclude_features, true, true, "--skip", "--skip <FEATURES>...");
parse_multi_opt!(
exclude_features,
true,
true,
"--exclude-features",
"--exclude-features <FEATURES>..."
);

if arg.starts_with("--optional-deps") {
if optional_deps.is_some() {
Expand Down Expand Up @@ -425,8 +435,24 @@ pub(crate) fn args(coloring: &mut Option<Coloring>) -> Result<Option<Args>> {
"--each-feature" => parse_flag!(each_feature),
"--feature-powerset" => parse_flag!(feature_powerset),
"--ignore-private" => parse_flag!(ignore_private),
"--skip-no-default-features" => parse_flag!(skip_no_default_features),
"--skip-all-features" => parse_flag!(skip_all_features),
"--exclude-no-default-features" => {
if exclude_no_default_features || skip_no_default_features {
return Err(multi_arg(&arg, subcommand.as_ref()));
}
exclude_no_default_features = true;
continue;
}
"--skip-no-default-features" => {
if exclude_no_default_features || skip_no_default_features {
return Err(multi_arg(
"--exclude-no-default-features",
subcommand.as_ref(),
));
}
skip_no_default_features = true;
continue;
}
"--exclude-all-features" => parse_flag!(exclude_all_features),
"--clean-per-run" => parse_flag!(clean_per_run),
"--ignore-unknown-features" => parse_flag!(ignore_unknown_features),
"--ignore-non-exist-features" => bail!(
Expand Down Expand Up @@ -481,17 +507,17 @@ pub(crate) fn args(coloring: &mut Option<Coloring>) -> Result<Option<Args>> {
bail!(
"--optional-deps can only be used together with either --each-feature or --feature-powerset"
);
} else if !skip.is_empty() {
} else if !exclude_features.is_empty() {
bail!(
"--skip can only be used together with either --each-feature or --feature-powerset"
"--exclude-features (--skip) can only be used together with either --each-feature or --feature-powerset"
);
} else if skip_no_default_features {
} else if exclude_no_default_features || skip_no_default_features {
bail!(
"--skip-no-default-features can only be used together with either --each-feature or --feature-powerset"
"--exclude-no-default-features can only be used together with either --each-feature or --feature-powerset"
);
} else if skip_all_features {
} else if exclude_all_features {
bail!(
"--skip-all-features can only be used together with either --each-feature or --feature-powerset"
"--exclude-all-features can only be used together with either --each-feature or --feature-powerset"
);
}
}
Expand Down Expand Up @@ -555,15 +581,22 @@ For more information try --help
}
}

if skip_no_default_features {
warn!(
color,
"--skip-no-default-features is deprecated, use --exclude-no-default-features flag instead"
);
exclude_no_default_features = true;
}
if no_dev_deps {
info!(
color,
"--no-dev-deps removes dev-dependencies from real `Cargo.toml` while cargo-hack is running and restores it when finished"
)
}

skip_no_default_features |= no_default_features;
skip.extend_from_slice(&features);
exclude_no_default_features |= no_default_features;
exclude_features.extend_from_slice(&features);

Ok(Some(Args {
leading_args: leading,
Expand All @@ -588,9 +621,9 @@ For more information try --help
no_default_features,
verbose,

skip,
skip_no_default_features,
skip_all_features,
exclude_features,
exclude_no_default_features,
exclude_all_features,

features,
color,
Expand Down
26 changes: 14 additions & 12 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ impl<'a> Kind<'a> {
return Kind::Nomal { show_progress: false };
}

let features =
package.features.keys().filter(|f| *f != "default" && !args.skip.contains(f));
let features = package
.features
.keys()
.filter(|f| *f != "default" && !args.exclude_features.contains(f));
let opt_deps = args.optional_deps.as_ref().map(|opt_deps| {
package.dependencies.iter().filter_map(Dependency::as_feature).filter(move |f| {
!args.skip.contains(f) && (opt_deps.is_empty() || opt_deps.contains(f))
!args.exclude_features.contains(f) && (opt_deps.is_empty() || opt_deps.contains(f))
})
});

Expand All @@ -82,9 +84,9 @@ impl<'a> Kind<'a> {
Kind::Nomal { show_progress: true }
} else {
*total += features.len()
+ (!args.skip.iter().any(|x| x == "default")) as usize
+ (!args.skip_no_default_features) as usize
+ (!args.skip_all_features) as usize;
+ (!args.exclude_features.iter().any(|x| x == "default")) as usize
+ (!args.exclude_no_default_features) as usize
+ (!args.exclude_all_features) as usize;
Kind::Each { features }
}
} else if args.feature_powerset {
Expand All @@ -100,9 +102,9 @@ impl<'a> Kind<'a> {
} else {
// -1: the first element of a powerset is `[]`
*total += features.len() - 1
+ (!args.skip.iter().any(|x| x == "default")) as usize
+ (!args.skip_no_default_features) as usize
+ (!args.skip_all_features) as usize;
+ (!args.exclude_features.iter().any(|x| x == "default")) as usize
+ (!args.exclude_no_default_features) as usize
+ (!args.exclude_all_features) as usize;
Kind::Powerset { features }
}
} else {
Expand All @@ -129,7 +131,7 @@ pub(crate) fn exec(

let mut line = line.clone();

if !args.skip.iter().any(|x| x == "default") {
if !args.exclude_features.iter().any(|x| x == "default") {
// run with default features
exec_cargo(args, package, &mut line, info, true)?;
}
Expand All @@ -138,7 +140,7 @@ pub(crate) fn exec(
line.arg("--no-default-features");
}

if !args.skip_no_default_features {
if !args.exclude_no_default_features {
// run with no default features if the package has other features
//
// `default` is not skipped because `cfg(feature = "default")` is work
Expand All @@ -162,7 +164,7 @@ pub(crate) fn exec(
_ => unreachable!(),
}

if !args.skip_all_features {
if !args.exclude_all_features {
// run with all features
line.arg("--all-features");
exec_cargo(args, package, &mut line, info, true)?;
Expand Down
15 changes: 9 additions & 6 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,22 @@ OPTIONS:
This flag can only be used together with either --each-feature flag or --feature-powerset flag.

--skip <FEATURES>...
Space-separated list of features to skip.
Alias for --exclude-features.

To skip run of default feature, using value `--skip default`.
--exclude-features <FEATURES>...
Space-separated list of features to exclude.

To exclude run of default feature, using value `--exclude-features default`.

This flag can only be used together with either --each-feature flag or --feature-powerset flag.

--skip-no-default-features
Skip run of just --no-default-features flag.
--exclude-no-default-features
Exclude run of just --no-default-features flag.

This flag can only be used together with either --each-feature flag or --feature-powerset flag.

--skip-all-features
Skip run of just --all-features flag.
--exclude-all-features
Exclude run of just --all-features flag.

This flag can only be used together with either --each-feature flag or --feature-powerset flag.

Expand Down
45 changes: 23 additions & 22 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ USAGE:
Use -h for short descriptions and --help for more details.

OPTIONS:
-p, --package <SPEC>... Package(s) to check
--all Alias for --workspace
--workspace Perform command for all packages in the workspace
--exclude <SPEC>... Exclude packages from the check
--manifest-path <PATH> Path to Cargo.toml
--features <FEATURES>... Space-separated list of features to activate
--each-feature Perform for each feature of the package
--feature-powerset Perform for the feature powerset of the package
--optional-deps [DEPS]... Use optional dependencies as features
--skip <FEATURES>... Space-separated list of features to skip
--skip-no-default-features Skip run of just --no-default-features flag
--skip-all-features Skip run of just --all-features flag
--depth <NUM> Specify a max number of simultaneous feature flags of --feature-powerset
--no-dev-deps Perform without dev-dependencies
--remove-dev-deps Equivalent to --no-dev-deps flag except for does not restore the original `Cargo.toml` after performed
--ignore-private Skip to perform on `publish = false` packages
--ignore-unknown-features Skip passing --features flag to `cargo` if that feature does not exist in the package
--clean-per-run Remove artifacts for that package before running the command
-v, --verbose Use verbose output
--color <WHEN> Coloring: auto, always, never
-h, --help Prints help information
-V, --version Prints version information
-p, --package <SPEC>... Package(s) to check
--all Alias for --workspace
--workspace Perform command for all packages in the workspace
--exclude <SPEC>... Exclude packages from the check
--manifest-path <PATH> Path to Cargo.toml
--features <FEATURES>... Space-separated list of features to activate
--each-feature Perform for each feature of the package
--feature-powerset Perform for the feature powerset of the package
--optional-deps [DEPS]... Use optional dependencies as features
--skip <FEATURES>... Alias for --exclude-features
--exclude-features <FEATURES>... Space-separated list of features to exclude
--exclude-no-default-features Exclude run of just --no-default-features flag
--exclude-all-features Exclude run of just --all-features flag
--depth <NUM> Specify a max number of simultaneous feature flags of --feature-powerset
--no-dev-deps Perform without dev-dependencies
--remove-dev-deps Equivalent to --no-dev-deps flag except for does not restore the original `Cargo.toml` after performed
--ignore-private Skip to perform on `publish = false` packages
--ignore-unknown-features Skip passing --features flag to `cargo` if that feature does not exist in the package
--clean-per-run Remove artifacts for that package before running the command
-v, --verbose Use verbose output
--color <WHEN> Coloring: auto, always, never
-h, --help Prints help information
-V, --version Prints version information

Some common cargo commands are (see all commands with --list):
build Compile the current package
Expand Down
Loading

0 comments on commit 1cd643d

Please sign in to comment.