Skip to content

Commit fccca68

Browse files
committed
Auto merge of #6801 - ehuss:install-flags, r=Eh2406
cargo install: Be more restrictive about cli flags. Several flags in `cargo install` are silently ignored depending on what is used. This adds some validation so that invalid combinations are rejected. I have been sorta confused by these in the past. - The 3 source flags (`--git`, `--path`, and `--registry`) are mutually exclusive. - The `--git` flags (branch, tag, rev) are only valid if `--git` is specified. - `--registry` requires a crate name (otherwise it would be ignored and treated as a path source). - `--version` is only used when a crate name is specified.
2 parents 8cf0c95 + b666a9f commit fccca68

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,36 @@ pub fn cli() -> App {
1010
.arg(opt("quiet", "No output printed to stdout").short("q"))
1111
.arg(Arg::with_name("crate").empty_values(false).multiple(true))
1212
.arg(
13-
opt("version", "Specify a version to install from crates.io")
13+
opt("version", "Specify a version to install")
1414
.alias("vers")
15-
.value_name("VERSION"),
15+
.value_name("VERSION")
16+
.requires("crate"),
17+
)
18+
.arg(
19+
opt("git", "Git URL to install the specified crate from")
20+
.value_name("URL")
21+
.conflicts_with_all(&["path", "registry"]),
22+
)
23+
.arg(
24+
opt("branch", "Branch to use when installing from git")
25+
.value_name("BRANCH")
26+
.requires("git"),
27+
)
28+
.arg(
29+
opt("tag", "Tag to use when installing from git")
30+
.value_name("TAG")
31+
.requires("git"),
32+
)
33+
.arg(
34+
opt("rev", "Specific commit to use when installing from git")
35+
.value_name("SHA")
36+
.requires("git"),
37+
)
38+
.arg(
39+
opt("path", "Filesystem path to local crate to install")
40+
.value_name("PATH")
41+
.conflicts_with_all(&["git", "registry"]),
1642
)
17-
.arg(opt("git", "Git URL to install the specified crate from").value_name("URL"))
18-
.arg(opt("branch", "Branch to use when installing from git").value_name("BRANCH"))
19-
.arg(opt("tag", "Tag to use when installing from git").value_name("TAG"))
20-
.arg(opt("rev", "Specific commit to use when installing from git").value_name("SHA"))
21-
.arg(opt("path", "Filesystem path to local crate to install").value_name("PATH"))
2243
.arg(opt(
2344
"list",
2445
"list all installed packages and their versions",
@@ -35,7 +56,12 @@ pub fn cli() -> App {
3556
)
3657
.arg_target_triple("Build for the target triple")
3758
.arg(opt("root", "Directory to install packages into").value_name("DIR"))
38-
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
59+
.arg(
60+
opt("registry", "Registry to use")
61+
.value_name("REGISTRY")
62+
.requires("crate")
63+
.conflicts_with_all(&["git", "path"]),
64+
)
3965
.after_help(
4066
"\
4167
This command manages Cargo's local set of installed binary crates. Only packages
@@ -46,10 +72,10 @@ configuration key, and finally the home directory (which is either
4672
`$CARGO_HOME` if set or `$HOME/.cargo` by default).
4773
4874
There are multiple sources from which a crate can be installed. The default
49-
location is crates.io but the `--git` and `--path` flags can change this source.
50-
If the source contains more than one package (such as crates.io or a git
51-
repository with multiple crates) the `<crate>` argument is required to indicate
52-
which crate should be installed.
75+
location is crates.io but the `--git`, `--path`, and `registry` flags can
76+
change this source. If the source contains more than one package (such as
77+
crates.io or a git repository with multiple crates) the `<crate>` argument is
78+
required to indicate which crate should be installed.
5379
5480
Crates from crates.io can optionally specify the version they wish to install
5581
via the `--version` flags, and similarly packages from git repositories can
@@ -62,10 +88,10 @@ By default cargo will refuse to overwrite existing binaries. The `--force` flag
6288
enables overwriting existing binaries. Thus you can reinstall a crate with
6389
`cargo install --force <crate>`.
6490
65-
Omitting the <crate> specification entirely will
66-
install the crate in the current directory. That is, `install` is equivalent to
67-
the more explicit `install --path .`. This behaviour is deprecated, and no
68-
longer supported as of the Rust 2018 edition.
91+
Omitting the <crate> specification entirely will install the crate in the
92+
current directory. That is, `install` is equivalent to the more explicit
93+
`install --path .`. This behaviour is deprecated, and no longer supported as
94+
of the Rust 2018 edition.
6995
7096
If the source is crates.io or `--git` then by default the crate will be built
7197
in a temporary target directory. To avoid this, the target directory can be

src/doc/man/cargo-install.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ the installation root's `bin` folder.
2424
include::description-install-root.adoc[]
2525

2626
There are multiple sources from which a crate can be installed. The default
27-
location is crates.io but the `--git` and `--path` flags can change this
28-
source. If the source contains more than one package (such as crates.io or a
29-
git repository with multiple crates) the _CRATE_ argument is required to
30-
indicate which crate should be installed.
27+
location is crates.io but the `--git`, `--path`, and `registry` flags can
28+
change this source. If the source contains more than one package (such as
29+
crates.io or a git repository with multiple crates) the _CRATE_ argument is
30+
required to indicate which crate should be installed.
3131

3232
Crates from crates.io can optionally specify the version they wish to install
3333
via the `--version` flags, and similarly packages from git repositories can
@@ -48,7 +48,7 @@ continuous integration systems.
4848

4949
*--vers* _VERSION_::
5050
*--version* _VERSION_::
51-
Specify a version to install from crates.io.
51+
Specify a version to install.
5252

5353
*--git* _URL_::
5454
Git URL to install the specified crate from.

src/doc/man/generated/cargo-install.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ <h2 id="cargo_install_description">DESCRIPTION</h2>
4545
</div>
4646
<div class="paragraph">
4747
<p>There are multiple sources from which a crate can be installed. The default
48-
location is crates.io but the <code>--git</code> and <code>--path</code> flags can change this
49-
source. If the source contains more than one package (such as crates.io or a
50-
git repository with multiple crates) the <em>CRATE</em> argument is required to
51-
indicate which crate should be installed.</p>
48+
location is crates.io but the <code>--git</code>, <code>--path</code>, and <code>registry</code> flags can
49+
change this source. If the source contains more than one package (such as
50+
crates.io or a git repository with multiple crates) the <em>CRATE</em> argument is
51+
required to indicate which crate should be installed.</p>
5252
</div>
5353
<div class="paragraph">
5454
<p>Crates from crates.io can optionally specify the version they wish to install
@@ -77,7 +77,7 @@ <h3 id="cargo_install_install_options">Install Options</h3>
7777
<dt class="hdlist1"><strong>--vers</strong> <em>VERSION</em></dt>
7878
<dt class="hdlist1"><strong>--version</strong> <em>VERSION</em></dt>
7979
<dd>
80-
<p>Specify a version to install from crates.io.</p>
80+
<p>Specify a version to install.</p>
8181
</dd>
8282
<dt class="hdlist1"><strong>--git</strong> <em>URL</em></dt>
8383
<dd>

src/etc/man/cargo-install.1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
.\" Title: cargo-install
33
.\" Author: [see the "AUTHOR(S)" section]
44
.\" Generator: Asciidoctor 1.5.8
5-
.\" Date: 2019-01-23
5+
.\" Date: 2019-03-31
66
.\" Manual: \ \&
77
.\" Source: \ \&
88
.\" Language: English
99
.\"
10-
.TH "CARGO\-INSTALL" "1" "2019-01-23" "\ \&" "\ \&"
10+
.TH "CARGO\-INSTALL" "1" "2019-03-31" "\ \&" "\ \&"
1111
.ie \n(.g .ds Aq \(aq
1212
.el .ds Aq '
1313
.ss \n[.ss] 0
@@ -103,10 +103,10 @@ The installation root is determined, in order of precedence:
103103
.RE
104104
.sp
105105
There are multiple sources from which a crate can be installed. The default
106-
location is crates.io but the \fB\-\-git\fP and \fB\-\-path\fP flags can change this
107-
source. If the source contains more than one package (such as crates.io or a
108-
git repository with multiple crates) the \fICRATE\fP argument is required to
109-
indicate which crate should be installed.
106+
location is crates.io but the \fB\-\-git\fP, \fB\-\-path\fP, and \fBregistry\fP flags can
107+
change this source. If the source contains more than one package (such as
108+
crates.io or a git repository with multiple crates) the \fICRATE\fP argument is
109+
required to indicate which crate should be installed.
110110
.sp
111111
Crates from crates.io can optionally specify the version they wish to install
112112
via the \fB\-\-version\fP flags, and similarly packages from git repositories can
@@ -125,7 +125,7 @@ continuous integration systems.
125125
.sp
126126
\fB\-\-vers\fP \fIVERSION\fP, \fB\-\-version\fP \fIVERSION\fP
127127
.RS 4
128-
Specify a version to install from crates.io.
128+
Specify a version to install.
129129
.RE
130130
.sp
131131
\fB\-\-git\fP \fIURL\fP

tests/testsuite/alt_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ Caused by:
620620
.run();
621621

622622
for cmd in &[
623-
"init", "install", "login", "owner", "publish", "search", "yank",
623+
"init", "install foo", "login", "owner", "publish", "search", "yank",
624624
] {
625625
p.cargo(cmd)
626626
.arg("--registry")

0 commit comments

Comments
 (0)