Skip to content

Commit aa5dfc7

Browse files
committed
Port to clap-cargo
1 parent 28ee3f7 commit aa5dfc7

File tree

8 files changed

+78
-120
lines changed

8 files changed

+78
-120
lines changed

Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ toml_edit = { version = "0.4.0", features = ["easy"] }
6969
url = "2.1.1"
7070
ureq = { version = "1.5.1", default-features = false, features = ["tls", "json", "socks"] }
7171
pathdiff = "0.2"
72+
clap-cargo = "0.6.1"
7273

7374
[dependencies.semver]
7475
features = ["serde"]

src/bin/add/args.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Args {
3535
pub crates: Vec<String>,
3636

3737
/// Rename a dependency in Cargo.toml,
38-
/// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml.
38+
/// <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml>.
3939
/// Only works when specifying a single dependency.
4040
#[structopt(long = "rename", short = "r")]
4141
pub rename: Option<String>,
@@ -85,17 +85,11 @@ pub struct Args {
8585
pub optional: bool,
8686

8787
/// Path to the manifest to add a dependency to.
88-
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
89-
pub manifest_path: Option<PathBuf>,
88+
#[structopt(flatten)]
89+
pub manifest: clap_cargo::Manifest,
9090

91-
/// Package id of the crate to add this dependency to.
92-
#[structopt(
93-
long = "package",
94-
short = "p",
95-
value_name = "pkgid",
96-
conflicts_with = "path"
97-
)]
98-
pub pkgid: Option<String>,
91+
#[structopt(flatten)]
92+
pub workspace: clap_cargo::Workspace,
9993

10094
/// Choose method of semantic version upgrade. Must be one of "none" (exact version, `=`
10195
/// modifier), "patch" (`~` modifier), "minor" (`^` modifier), "all" (`>=`), or "default" (no
@@ -117,14 +111,9 @@ pub struct Args {
117111
#[structopt(long = "allow-prerelease")]
118112
pub allow_prerelease: bool,
119113

120-
/// Space-separated list of features to add. For an alternative approach to
121-
/// enabling features, consider installing the `cargo-feature` utility.
122-
#[structopt(long = "features", number_of_values = 1)]
123-
pub features: Option<Vec<String>>,
124-
125-
/// Set `default-features = false` for the added dependency.
126-
#[structopt(long = "no-default-features")]
127-
pub no_default_features: bool,
114+
/// For an alternative approach to enabling features, consider installing `cargo-feature`.
115+
#[structopt(flatten)]
116+
pub features: clap_cargo::Features,
128117

129118
/// Do not print any output in case of success.
130119
#[structopt(long = "quiet", short = "q")]
@@ -229,7 +218,10 @@ impl Args {
229218
dependency = dependency.set_version(parse_version_req(version)?);
230219
}
231220
let registry_url = if let Some(registry) = &self.registry {
232-
Some(registry_url(&find(&self.manifest_path)?, Some(registry))?)
221+
Some(registry_url(
222+
&find(&self.manifest.manifest_path)?,
223+
Some(registry),
224+
)?)
233225
} else {
234226
None
235227
};
@@ -262,7 +254,7 @@ impl Args {
262254
dependency = get_latest_dependency(
263255
crate_name.name(),
264256
self.allow_prerelease,
265-
&find(&self.manifest_path)?,
257+
&find(&self.manifest.manifest_path)?,
266258
&registry_url,
267259
)?;
268260
let v = format!(
@@ -287,7 +279,7 @@ impl Args {
287279

288280
/// Build dependencies from arguments
289281
pub fn parse_dependencies(&self) -> Result<Vec<Dependency>> {
290-
let workspace_members = workspace_members(self.manifest_path.as_deref())?;
282+
let workspace_members = workspace_members(self.manifest.manifest_path.as_deref())?;
291283

292284
if self.crates.len() > 1
293285
&& (self.git.is_some() || self.path.is_some() || self.vers.is_some())
@@ -299,7 +291,7 @@ impl Args {
299291
return Err(ErrorKind::MultipleCratesWithRename.into());
300292
}
301293

302-
if self.crates.len() > 1 && self.features.is_some() {
294+
if self.crates.len() > 1 && !self.features.features.is_empty() {
303295
return Err(ErrorKind::MultipleCratesWithFeatures.into());
304296
}
305297

@@ -310,8 +302,12 @@ impl Args {
310302
.map(|x| {
311303
let mut x = x
312304
.set_optional(self.optional)
313-
.set_features(self.features.clone())
314-
.set_default_features(!self.no_default_features);
305+
.set_features(if self.features.features.is_empty() {
306+
None
307+
} else {
308+
Some(self.features.features.clone())
309+
})
310+
.set_default_features(!self.features.no_default_features);
315311
if let Some(ref rename) = self.rename {
316312
x = x.set_rename(rename);
317313
}
@@ -347,16 +343,15 @@ impl Default for Args {
347343
path: None,
348344
target: None,
349345
optional: false,
350-
manifest_path: None,
351-
pkgid: None,
352346
upgrade: "minor".to_string(),
353347
allow_prerelease: false,
354-
features: None,
355-
no_default_features: false,
356348
quiet: false,
357349
offline: true,
358350
sort: false,
359351
registry: None,
352+
manifest: Default::default(),
353+
workspace: Default::default(),
354+
features: Default::default(),
360355
}
361356
}
362357
}

src/bin/add/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
120120
}
121121

122122
fn handle_add(args: &Args) -> Result<()> {
123-
let manifest_path = if let Some(ref pkgid) = args.pkgid {
124-
let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?;
123+
let manifest_path = if let Some(pkgid) = args.workspace.package.get(0) {
124+
let pkg = manifest_from_pkgid(args.manifest.manifest_path.as_deref(), pkgid)?;
125125
Cow::Owned(Some(pkg.manifest_path.into_std_path_buf()))
126126
} else {
127-
Cow::Borrowed(&args.manifest_path)
127+
Cow::Borrowed(&args.manifest.manifest_path)
128128
};
129129
let mut manifest = LocalManifest::find(&manifest_path)?;
130130

src/bin/rm/main.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern crate error_chain;
1717
use cargo_edit::{manifest_from_pkgid, LocalManifest};
1818
use std::borrow::Cow;
1919
use std::io::Write;
20-
use std::path::PathBuf;
20+
2121
use std::process;
2222
use structopt::{clap::AppSettings, StructOpt};
2323
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
@@ -58,17 +58,11 @@ struct Args {
5858
build: bool,
5959

6060
/// Path to the manifest to remove a dependency from.
61-
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
62-
manifest_path: Option<PathBuf>,
63-
64-
/// Package id of the crate to remove this dependency from.
65-
#[structopt(
66-
long = "package",
67-
short = "p",
68-
value_name = "pkgid",
69-
conflicts_with = "path"
70-
)]
71-
pkgid: Option<String>,
61+
#[structopt(flatten)]
62+
manifest: clap_cargo::Manifest,
63+
64+
#[structopt(flatten)]
65+
workspace: clap_cargo::Workspace,
7266

7367
/// Do not print any output in case of success.
7468
#[structopt(long = "quiet", short = "q")]
@@ -103,11 +97,11 @@ fn print_msg(name: &str, section: &str) -> Result<()> {
10397
}
10498

10599
fn handle_rm(args: &Args) -> Result<()> {
106-
let manifest_path = if let Some(ref pkgid) = args.pkgid {
107-
let pkg = manifest_from_pkgid(args.manifest_path.as_deref(), pkgid)?;
100+
let manifest_path = if !args.workspace.package.is_empty() {
101+
let pkg = manifest_from_pkgid(args.manifest.manifest_path.as_deref(), &args.workspace.package[0])?;
108102
Cow::Owned(Some(pkg.manifest_path.into_std_path_buf()))
109103
} else {
110-
Cow::Borrowed(&args.manifest_path)
104+
Cow::Borrowed(&args.manifest.manifest_path)
111105
};
112106
let mut manifest = LocalManifest::find(&manifest_path)?;
113107
let deps = &args.crates;

src/bin/set-version/args.rs

+4-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::PathBuf;
2-
31
use structopt::{clap::AppSettings, StructOpt};
42

53
#[derive(Debug, StructOpt)]
@@ -27,38 +25,13 @@ pub(crate) struct Args {
2725
pub metadata: Option<String>,
2826

2927
/// Path to the manifest to upgrade
30-
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
31-
pub(crate) manifest_path: Option<PathBuf>,
32-
33-
/// Package id of the crate to change the version of.
34-
#[structopt(
35-
long = "package",
36-
short = "p",
37-
value_name = "pkgid",
38-
conflicts_with = "path",
39-
conflicts_with = "all",
40-
conflicts_with = "workspace"
41-
)]
42-
pub(crate) pkgid: Option<String>,
28+
#[structopt(flatten)]
29+
pub(crate) manifest: clap_cargo::Manifest,
4330

44-
/// Modify all packages in the workspace.
45-
#[structopt(
46-
long = "all",
47-
help = "[deprecated in favor of `--workspace`]",
48-
conflicts_with = "workspace",
49-
conflicts_with = "pkgid"
50-
)]
51-
pub(crate) all: bool,
52-
53-
/// Modify all packages in the workspace.
54-
#[structopt(long = "workspace", conflicts_with = "all", conflicts_with = "pkgid")]
55-
pub(crate) workspace: bool,
31+
#[structopt(flatten)]
32+
pub(crate) workspace: clap_cargo::Workspace,
5633

5734
/// Print changes to be made without making them.
5835
#[structopt(long = "dry-run")]
5936
pub(crate) dry_run: bool,
60-
61-
/// Crates to exclude and not modify.
62-
#[structopt(long)]
63-
pub(crate) exclude: Vec<String>,
6437
}

src/bin/set-version/main.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ fn process(args: Args) -> Result<()> {
5757
target,
5858
bump,
5959
metadata,
60-
manifest_path,
61-
pkgid,
62-
all,
60+
manifest: clap_cargo::Manifest { manifest_path, .. },
61+
workspace:
62+
clap_cargo::Workspace {
63+
package: pkgid,
64+
workspace,
65+
all,
66+
exclude,
67+
..
68+
},
6369
dry_run,
64-
workspace,
65-
exclude,
6670
} = args;
6771

6872
let target = match (target, bump) {
@@ -79,8 +83,8 @@ fn process(args: Args) -> Result<()> {
7983

8084
let manifests = if all {
8185
Manifests::get_all(&manifest_path)
82-
} else if let Some(ref pkgid) = pkgid {
83-
Manifests::get_pkgid(manifest_path.as_deref(), pkgid)
86+
} else if let Some(id) = pkgid.get(0) {
87+
Manifests::get_pkgid(manifest_path.as_deref(), id)
8488
} else {
8589
Manifests::get_local_one(&manifest_path)
8690
}?;

src/bin/upgrade/main.rs

+15-35
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,13 @@ struct Args {
7070
dependency: Vec<String>,
7171

7272
/// Path to the manifest to upgrade
73-
#[structopt(long = "manifest-path", value_name = "path", conflicts_with = "pkgid")]
74-
manifest_path: Option<PathBuf>,
75-
76-
/// Package id of the crate to add this dependency to.
77-
#[structopt(
78-
long = "package",
79-
short = "p",
80-
value_name = "pkgid",
81-
conflicts_with = "path",
82-
conflicts_with = "all",
83-
conflicts_with = "workspace"
84-
)]
85-
pkgid: Option<String>,
86-
87-
/// Upgrade all packages in the workspace.
88-
#[structopt(
89-
long = "all",
90-
help = "[deprecated in favor of `--workspace`]",
91-
conflicts_with = "workspace",
92-
conflicts_with = "pkgid"
93-
)]
94-
all: bool,
73+
#[structopt(flatten)]
74+
manifest: clap_cargo::Manifest,
9575

9676
/// Upgrade all packages in the workspace.
9777
/// Implied by default when running in a directory with virtual manifest.
98-
#[structopt(long = "workspace", conflicts_with = "all", conflicts_with = "pkgid")]
99-
workspace: bool,
78+
#[structopt(flatten)]
79+
workspace: clap_cargo::Workspace,
10080

10181
/// Include prerelease versions when fetching from crates.io (e.g. 0.6.0-alpha').
10282
#[structopt(long = "allow-prerelease")]
@@ -117,10 +97,6 @@ struct Args {
11797
/// Upgrade all packages to the version in the lockfile.
11898
#[structopt(long = "to-lockfile", conflicts_with = "dependency")]
11999
pub to_lockfile: bool,
120-
121-
/// Crates to exclude and not upgrade.
122-
#[structopt(long)]
123-
exclude: Vec<String>,
124100
}
125101

126102
/// A collection of manifests.
@@ -453,15 +429,19 @@ impl DesiredUpgrades {
453429
fn process(args: Args) -> Result<()> {
454430
let Args {
455431
dependency,
456-
manifest_path,
457-
pkgid,
458-
all,
432+
manifest: clap_cargo::Manifest { manifest_path, .. },
433+
workspace:
434+
clap_cargo::Workspace {
435+
package: pkgid,
436+
workspace,
437+
all,
438+
exclude,
439+
..
440+
},
459441
allow_prerelease,
460442
dry_run,
461443
skip_compatible,
462444
to_lockfile,
463-
workspace,
464-
exclude,
465445
..
466446
} = args;
467447

@@ -479,8 +459,8 @@ fn process(args: Args) -> Result<()> {
479459

480460
let manifests = if all {
481461
Manifests::get_all(&manifest_path)
482-
} else if let Some(ref pkgid) = pkgid {
483-
Manifests::get_pkgid(manifest_path.as_deref(), pkgid)
462+
} else if !pkgid.is_empty() {
463+
Manifests::get_pkgid(manifest_path.as_deref(), &pkgid[0])
484464
} else {
485465
Manifests::get_local_one(&manifest_path)
486466
}?;

0 commit comments

Comments
 (0)