Skip to content

Commit c43c1b1

Browse files
committed
Allow --document-private-items to accept =yes|no
Fixes rust-lang#7963. Previously, --document-private-items was only a 'truthy' flag, and was enabled in rust-lang#7593 for binary targets by default. However, this prevented any means of *disabling* this functionality for binary targets, hence rust-lang#7963. This change does a few things. It first adds a new argument parser type called `yesno_flag()` that is a wrapper around a few `clap::Arg` settings to use the built-in `BoolishValueParser` in a way that allows for the `--flag` to return `true`, as well as an optional `=no` or `=yes` (or `=1`, `=off`, etc.) to explicitly set it to `true` or `false`. Then, the internal field for passing the private member documentation flag to rustdoc was changed to an `Option<bool>` to treat `None` as the automatic project type detection, and a `Some` value to override the behavior outright. This change should be entirely backwards compatible.
1 parent 1ac43cf commit c43c1b1

File tree

10 files changed

+94
-15
lines changed

10 files changed

+94
-15
lines changed

src/bin/cargo/commands/doc.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub fn cli() -> App {
77
// subcommand aliases are handled in aliased_command()
88
// .alias("d")
99
.about("Build a package's documentation")
10+
.term_width(250)
11+
.max_term_width(250)
1012
.arg_quiet()
1113
.arg(flag(
1214
"open",
@@ -21,7 +23,10 @@ pub fn cli() -> App {
2123
"no-deps",
2224
"Don't build documentation for dependencies",
2325
))
24-
.arg(flag("document-private-items", "Document private items"))
26+
.arg(yesno_flag(
27+
"document-private-items",
28+
"Document private items (the default for binaries)",
29+
))
2530
.arg_jobs()
2631
.arg_targets_lib_bin_example(
2732
"Document only this package's library",
@@ -50,7 +55,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5055
};
5156
let mut compile_opts =
5257
args.compile_options(config, mode, Some(&ws), ProfileChecking::Custom)?;
53-
compile_opts.rustdoc_document_private_items = args.flag("document-private-items");
58+
59+
compile_opts.rustdoc_document_private_items =
60+
args.get_one::<bool>("document-private-items").copied();
5461

5562
let doc_opts = DocOptions {
5663
open_result: args.flag("open"),

src/cargo/ops/cargo_compile.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ pub struct CompileOptions {
7575
pub target_rustc_crate_types: Option<Vec<String>>,
7676
/// Extra arguments passed to all selected targets for rustdoc.
7777
pub local_rustdoc_args: Option<Vec<String>>,
78-
/// Whether the `--document-private-items` flags was specified and should
79-
/// be forwarded to `rustdoc`.
80-
pub rustdoc_document_private_items: bool,
78+
/// Whether either the `--document-private-items` or `--document-only-public-items`
79+
/// flags were specified and should be forwarded to `rustdoc`. `None` indicates
80+
/// that the behavior should be decided based on the target type - either public-only
81+
/// for libraries, or private items for binaries.
82+
pub rustdoc_document_private_items: Option<bool>,
8183
/// Whether the build process should check the minimum Rust version
8284
/// defined in the cargo metadata for a crate.
8385
pub honor_rust_version: bool,
@@ -98,7 +100,7 @@ impl CompileOptions {
98100
target_rustc_args: None,
99101
target_rustc_crate_types: None,
100102
local_rustdoc_args: None,
101-
rustdoc_document_private_items: false,
103+
rustdoc_document_private_items: None,
102104
honor_rust_version: true,
103105
})
104106
}
@@ -620,7 +622,7 @@ pub fn create_bcx<'a, 'cfg>(
620622
// Add `--document-private-items` rustdoc flag if requested or if
621623
// the target is a binary. Binary crates get their private items
622624
// documented by default.
623-
if rustdoc_document_private_items || unit.target.is_bin() {
625+
if rustdoc_document_private_items.unwrap_or(unit.target.is_bin()) {
624626
let mut args = extra_args.take().unwrap_or_default();
625627
args.push("--document-private-items".into());
626628
if unit.target.is_bin() {

src/cargo/ops/cargo_package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ fn run_verify(
830830
target_rustc_args: rustc_args,
831831
target_rustc_crate_types: None,
832832
local_rustdoc_args: None,
833-
rustdoc_document_private_items: false,
833+
rustdoc_document_private_items: None,
834834
honor_rust_version: true,
835835
},
836836
&exec,

src/cargo/util/command_prelude.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ pub fn flag(name: &'static str, help: &'static str) -> Arg<'static> {
268268
.action(ArgAction::SetTrue)
269269
}
270270

271+
pub fn yesno_flag(name: &'static str, help: &'static str) -> Arg<'static> {
272+
Arg::new(name)
273+
.long(name)
274+
.help(help)
275+
.value_name("yes|no")
276+
.value_parser(clap::builder::BoolishValueParser::new())
277+
.hide_possible_values(true)
278+
.min_values(0)
279+
.takes_value(true)
280+
.multiple_values(false)
281+
.require_equals(true)
282+
.default_missing_value("yes")
283+
}
284+
271285
pub fn opt(name: &'static str, help: &'static str) -> Arg<'static> {
272286
Arg::new(name).long(name).help(help)
273287
}
@@ -610,7 +624,7 @@ pub trait ArgMatchesExt {
610624
target_rustc_args: None,
611625
target_rustc_crate_types: None,
612626
local_rustdoc_args: None,
613-
rustdoc_document_private_items: false,
627+
rustdoc_document_private_items: None,
614628
honor_rust_version: !self.flag("ignore-rust-version"),
615629
};
616630

src/doc/man/cargo-doc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ option.
3232
Do not build documentation for dependencies.
3333
{{/option}}
3434

35-
{{#option "`--document-private-items`" }}
35+
{{#option "`--document-private-items`[=_yes|no_]" }}
3636
Include non-public items in the documentation. This will be enabled by default if documenting a binary target.
3737
{{/option}}
3838

src/doc/man/generated_txt/cargo-doc.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ OPTIONS
2222
--no-deps
2323
Do not build documentation for dependencies.
2424

25-
--document-private-items
25+
--document-private-items[=yes|no]
2626
Include non-public items in the documentation. This will be enabled
2727
by default if documenting a binary target.
2828

src/doc/src/commands/cargo-doc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ option.</dd>
3232
<dd class="option-desc">Do not build documentation for dependencies.</dd>
3333

3434

35-
<dt class="option-term" id="option-cargo-doc---document-private-items"><a class="option-anchor" href="#option-cargo-doc---document-private-items"></a><code>--document-private-items</code></dt>
35+
<dt class="option-term" id="option-cargo-doc---document-private-items[=yes|no]"><a class="option-anchor" href="#option-cargo-doc---document-private-items[=yes|no]"></a><code>--document-private-items</code>[=<em>yes|no</em>]</dt>
3636
<dd class="option-desc">Include non-public items in the documentation. This will be enabled by default if documenting a binary target.</dd>
3737

3838

src/etc/_cargo

+6-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ _cargo() {
125125
doc | d)
126126
_arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
127127
'--no-deps[do not build docs for dependencies]' \
128-
'--document-private-items[include non-public items in the documentation]' \
128+
'--document-private-items'{--document-private-items=}'[include non-public items in the documentation]:bool:_cargo_yesno' \
129129
'--open[open docs in browser after the build]' \
130130
'(-p --package)'{-p+,--package=}'[specify package to document]:package:_cargo_package_names' \
131131
'--release[build artifacts in release mode, with optimizations]' \
@@ -261,7 +261,7 @@ _cargo() {
261261

262262
rustdoc)
263263
_arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
264-
'--document-private-items[include non-public items in the documentation]' \
264+
'--document-private-items'{--document-private-items=}'[include non-public items in the documentation]:bool:_cargo_yesno' \
265265
'--open[open the docs in a browser after the operation]' \
266266
'(-p --package)'{-p+,--package=}'[specify package to document]:package:_cargo_package_names' \
267267
'--release[build artifacts in release mode, with optimizations]' \
@@ -444,4 +444,8 @@ _cargo_example_names() {
444444
fi
445445
}
446446

447+
_cargo_yesno() {
448+
_values 'yes|no' yes no
449+
}
450+
447451
_cargo

src/etc/man/cargo-doc.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ option.
2626
Do not build documentation for dependencies.
2727
.RE
2828
.sp
29-
\fB\-\-document\-private\-items\fR
29+
\fB\-\-document\-private\-items\fR[=\fIyes|no\fR]
3030
.RS 4
3131
Include non\-public items in the documentation. This will be enabled by default if documenting a binary target.
3232
.RE

tests/testsuite/doc.rs

+52
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,58 @@ fn bin_private_items() {
19221922
assert!(p.root().join("target/doc/foo/foo_mod/index.html").is_file());
19231923
}
19241924

1925+
#[cargo_test]
1926+
fn bin_private_items_disable() {
1927+
let p = project()
1928+
.file(
1929+
"Cargo.toml",
1930+
r#"
1931+
[package]
1932+
name = "foo"
1933+
version = "0.0.1"
1934+
authors = []
1935+
"#,
1936+
)
1937+
.file(
1938+
"src/main.rs",
1939+
"
1940+
pub fn foo_pub() {}
1941+
fn foo_priv() {}
1942+
struct FooStruct;
1943+
enum FooEnum {}
1944+
trait FooTrait {}
1945+
type FooType = u32;
1946+
mod foo_mod {}
1947+
1948+
",
1949+
)
1950+
.build();
1951+
1952+
p.cargo("doc --document-private-items=no")
1953+
.with_stderr(
1954+
"\
1955+
[DOCUMENTING] foo v0.0.1 ([CWD])
1956+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1957+
",
1958+
)
1959+
.run();
1960+
1961+
assert!(p.root().join("target/doc/foo/index.html").is_file());
1962+
assert!(p.root().join("target/doc/foo/fn.foo_pub.html").is_file());
1963+
assert!(!p.root().join("target/doc/foo/fn.foo_priv.html").is_file());
1964+
assert!(!p
1965+
.root()
1966+
.join("target/doc/foo/struct.FooStruct.html")
1967+
.is_file());
1968+
assert!(!p.root().join("target/doc/foo/enum.FooEnum.html").is_file());
1969+
assert!(!p
1970+
.root()
1971+
.join("target/doc/foo/trait.FooTrait.html")
1972+
.is_file());
1973+
assert!(!p.root().join("target/doc/foo/type.FooType.html").is_file());
1974+
assert!(!p.root().join("target/doc/foo/foo_mod/index.html").is_file());
1975+
}
1976+
19251977
#[cargo_test]
19261978
fn bin_private_items_deps() {
19271979
let p = project()

0 commit comments

Comments
 (0)