Skip to content

Commit

Permalink
Auto merge of #4637 - kivikakk:vers-version, r=alexcrichton
Browse files Browse the repository at this point in the history
Allow cargo install --version as well (preferred)

Fixes #4590.

The usage looks pretty ugly with this extra line in it, but it's not really avoidable with our docopt use.

We fail if both are provided; accepting both feels like a path to demons.
  • Loading branch information
bors committed Oct 19, 2017
2 parents 0d2b5a6 + 0cbfb67 commit 75f8541
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/bin/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo::ops;
use cargo::core::{SourceId, GitReference};
use cargo::util::{CliResult, Config, ToUrl};
use cargo::util::{CargoError, CliResult, Config, ToUrl};

#[derive(Deserialize)]
pub struct Options {
Expand All @@ -24,6 +24,7 @@ pub struct Options {

arg_crate: Vec<String>,
flag_vers: Option<String>,
flag_version: Option<String>,

flag_git: Option<String>,
flag_branch: Option<String>,
Expand All @@ -43,7 +44,8 @@ Usage:
cargo install [options] --list
Specifying what crate to install:
--vers VERS Specify a version to install from crates.io
--vers VERSION Specify a version to install from crates.io
--version VERSION
--git URL Git URL to install the specified crate from
--branch BRANCH Branch to use when installing from git
--tag TAG Tag to use when installing from git
Expand Down Expand Up @@ -151,7 +153,11 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
};

let krates = options.arg_crate.iter().map(|s| &s[..]).collect::<Vec<_>>();
let vers = options.flag_vers.as_ref().map(|s| &s[..]);
let vers = match (&options.flag_vers, &options.flag_version) {
(&Some(_), &Some(_)) => return Err(CargoError::from("Invalid arguments.").into()),
(&Some(ref v), _) | (_, &Some(ref v)) => Some(v.as_ref()),
_ => None,
};
let root = options.flag_root.as_ref().map(|s| &s[..]);

if options.flag_list {
Expand Down
22 changes: 22 additions & 0 deletions tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,28 @@ fn vers_precise() {
"));
}

#[test]
fn version_too() {
pkg("foo", "0.1.1");
pkg("foo", "0.1.2");

assert_that(cargo_process("install").arg("foo").arg("--version").arg("0.1.1"),
execs().with_status(0).with_stderr_contains("\
[DOWNLOADING] foo v0.1.1 (registry [..])
"));
}

#[test]
fn not_both_vers_and_version() {
pkg("foo", "0.1.1");
pkg("foo", "0.1.2");

assert_that(cargo_process("install").arg("foo").arg("--version").arg("0.1.1").arg("--vers").arg("0.1.2"),
execs().with_status(101).with_stderr_contains("\
error: Invalid arguments.
"));
}

#[test]
fn legacy_version_requirement() {
pkg("foo", "0.1.1");
Expand Down

0 comments on commit 75f8541

Please sign in to comment.