Skip to content

Commit 54c341d

Browse files
committed
Auto merge of #9808 - QiangHeisenberg:examples, r=ehuss
Adding the cargo doc --examples subcommand Adding the cargo doc --examples subcommand #4508
2 parents dd474f8 + 92c35f2 commit 54c341d

File tree

7 files changed

+154
-5
lines changed

7 files changed

+154
-5
lines changed

src/bin/cargo/commands/doc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ pub fn cli() -> App {
2020
.arg(opt("no-deps", "Don't build documentation for dependencies"))
2121
.arg(opt("document-private-items", "Document private items"))
2222
.arg_jobs()
23-
.arg_targets_lib_bin(
23+
.arg_targets_lib_bin_example(
2424
"Document only this package's library",
2525
"Document only the specified binary",
2626
"Document all binaries",
27+
"Document only the specified example",
28+
"Document all examples",
2729
)
2830
.arg_release("Build artifacts in release mode, with optimizations")
2931
.arg_profile("Build artifacts with the specified profile")

src/cargo/util/command_prelude.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,27 @@ pub trait AppExt: Sized {
8787
benches: &'static str,
8888
all: &'static str,
8989
) -> Self {
90-
self.arg_targets_lib_bin(lib, bin, bins)
91-
._arg(optional_multi_opt("example", "NAME", example))
92-
._arg(opt("examples", examples))
90+
self.arg_targets_lib_bin_example(lib, bin, bins, example, examples)
9391
._arg(optional_multi_opt("test", "NAME", test))
9492
._arg(opt("tests", tests))
9593
._arg(optional_multi_opt("bench", "NAME", bench))
9694
._arg(opt("benches", benches))
9795
._arg(opt("all-targets", all))
9896
}
9997

100-
fn arg_targets_lib_bin(self, lib: &'static str, bin: &'static str, bins: &'static str) -> Self {
98+
fn arg_targets_lib_bin_example(
99+
self,
100+
lib: &'static str,
101+
bin: &'static str,
102+
bins: &'static str,
103+
example: &'static str,
104+
examples: &'static str,
105+
) -> Self {
101106
self._arg(opt("lib", lib))
102107
._arg(optional_multi_opt("bin", "NAME", bin))
103108
._arg(opt("bins", bins))
109+
._arg(optional_multi_opt("example", "NAME", example))
110+
._arg(opt("examples", examples))
104111
}
105112

106113
fn arg_targets_bins_examples(

src/doc/man/cargo-doc.md

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ flag and will always document the given target.
5252

5353
{{#options}}
5454
{{> options-targets-lib-bin }}
55+
56+
{{#option "`--example` _name_..." }}
57+
{{actionverb}} the specified example. This flag may be specified multiple times
58+
and supports common Unix glob patterns.
59+
{{/option}}
60+
61+
{{#option "`--examples`" }}
62+
{{actionverb}} all example targets.
63+
{{/option}}
64+
5565
{{/options}}
5666

5767
{{> section-features }}

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

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ OPTIONS
8282
--bins
8383
Document all binary targets.
8484

85+
--example name...
86+
Document the specified example. This flag may be specified multiple
87+
times and supports common Unix glob patterns.
88+
89+
--examples
90+
Document all example targets.
91+
8592
Feature Selection
8693
The feature flags allow you to control which features are enabled. When
8794
no feature options are given, the default feature is activated for every

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

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ and supports common Unix glob patterns.</dd>
107107
<dd class="option-desc">Document all binary targets.</dd>
108108

109109

110+
111+
<dt class="option-term" id="option-cargo-doc---example"><a class="option-anchor" href="#option-cargo-doc---example"></a><code>--example</code> <em>name</em>...</dt>
112+
<dd class="option-desc">Document the specified example. This flag may be specified multiple times
113+
and supports common Unix glob patterns.</dd>
114+
115+
116+
<dt class="option-term" id="option-cargo-doc---examples"><a class="option-anchor" href="#option-cargo-doc---examples"></a><code>--examples</code></dt>
117+
<dd class="option-desc">Document all example targets.</dd>
118+
119+
110120
</dl>
111121

112122
### Feature Selection

src/etc/man/cargo-doc.1

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ and supports common Unix glob patterns.
9595
.RS 4
9696
Document all binary targets.
9797
.RE
98+
.sp
99+
\fB\-\-example\fR \fIname\fR\&...
100+
.RS 4
101+
Document the specified example. This flag may be specified multiple times
102+
and supports common Unix glob patterns.
103+
.RE
104+
.sp
105+
\fB\-\-examples\fR
106+
.RS 4
107+
Document all example targets.
108+
.RE
98109
.SS "Feature Selection"
99110
The feature flags allow you to control which features are enabled. When no
100111
feature options are given, the \fBdefault\fR feature is activated for every

tests/testsuite/doc.rs

+102
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,108 @@ fn doc_lib_bin_same_name_documents_bins_when_requested() {
513513
assert!(doc_html.contains("Binary"));
514514
}
515515

516+
#[cargo_test]
517+
fn doc_lib_bin_example_same_name_documents_named_example_when_requested() {
518+
let p = project()
519+
.file(
520+
"src/main.rs",
521+
r#"
522+
//! Binary documentation
523+
extern crate foo;
524+
fn main() {
525+
foo::foo();
526+
}
527+
"#,
528+
)
529+
.file(
530+
"src/lib.rs",
531+
r#"
532+
//! Library documentation
533+
pub fn foo() {}
534+
"#,
535+
)
536+
.file(
537+
"examples/ex1.rs",
538+
r#"
539+
//! Example1 documentation
540+
pub fn x() { f(); }
541+
"#,
542+
)
543+
.build();
544+
545+
p.cargo("doc --example ex1")
546+
.with_stderr(
547+
"\
548+
[CHECKING] foo v0.0.1 ([CWD])
549+
[DOCUMENTING] foo v0.0.1 ([CWD])
550+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
551+
)
552+
.run();
553+
554+
let doc_html = p.read_file("target/doc/ex1/index.html");
555+
assert!(!doc_html.contains("Library"));
556+
assert!(!doc_html.contains("Binary"));
557+
assert!(doc_html.contains("Example1"));
558+
}
559+
560+
#[cargo_test]
561+
fn doc_lib_bin_example_same_name_documents_examples_when_requested() {
562+
let p = project()
563+
.file(
564+
"src/main.rs",
565+
r#"
566+
//! Binary documentation
567+
extern crate foo;
568+
fn main() {
569+
foo::foo();
570+
}
571+
"#,
572+
)
573+
.file(
574+
"src/lib.rs",
575+
r#"
576+
//! Library documentation
577+
pub fn foo() {}
578+
"#,
579+
)
580+
.file(
581+
"examples/ex1.rs",
582+
r#"
583+
//! Example1 documentation
584+
pub fn example1() { f(); }
585+
"#,
586+
)
587+
.file(
588+
"examples/ex2.rs",
589+
r#"
590+
//! Example2 documentation
591+
pub fn example2() { f(); }
592+
"#,
593+
)
594+
.build();
595+
596+
p.cargo("doc --examples")
597+
.with_stderr(
598+
"\
599+
[CHECKING] foo v0.0.1 ([CWD])
600+
[DOCUMENTING] foo v0.0.1 ([CWD])
601+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
602+
)
603+
.run();
604+
605+
let example_doc_html_1 = p.read_file("target/doc/ex1/index.html");
606+
let example_doc_html_2 = p.read_file("target/doc/ex2/index.html");
607+
608+
assert!(!example_doc_html_1.contains("Library"));
609+
assert!(!example_doc_html_1.contains("Binary"));
610+
611+
assert!(!example_doc_html_2.contains("Library"));
612+
assert!(!example_doc_html_2.contains("Binary"));
613+
614+
assert!(example_doc_html_1.contains("Example1"));
615+
assert!(example_doc_html_2.contains("Example2"));
616+
}
617+
516618
#[cargo_test]
517619
fn doc_dash_p() {
518620
let p = project()

0 commit comments

Comments
 (0)