Skip to content

Commit 2929efd

Browse files
committed
add --examples subcommand
1 parent 51d48e9 commit 2929efd

File tree

5 files changed

+121
-6
lines changed

5 files changed

+121
-6
lines changed

src/bin/cargo/commands/doc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ pub fn cli() -> App {
2121
.arg(opt("no-deps", "Don't build documentation for dependencies"))
2222
.arg(opt("document-private-items", "Document private items"))
2323
.arg_jobs()
24-
.arg_targets_lib_bin(
24+
.arg_targets_lib_bin_example(
2525
"Document only this package's library",
2626
"Document only the specified binary",
2727
"Document all binaries",
28+
"Document only the specified binary",
29+
"Document all examples",
2830
)
2931
.arg_release("Build artifacts in release mode, with optimizations")
3032
.arg_profile("Build artifacts with the specified profile")

src/cargo/util/command_prelude.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ 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(self, lib: &'static str, bin: &'static str, bins: &'static str, example: &'static str, examples: &'static str) -> Self {
10199
self._arg(opt("lib", lib))
102100
._arg(optional_multi_opt("bin", "NAME", bin))
103101
._arg(opt("bins", bins))
102+
._arg(optional_multi_opt("example", "NAME", example))
103+
._arg(opt("examples", examples))
104104
}
105105

106106
fn arg_targets_bins_examples(

src/doc/man/cargo-doc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ the manifest settings. Using target selection options will ignore the `doc`
5151
flag and will always document the given target.
5252

5353
{{#options}}
54-
{{> options-targets-lib-bin }}
54+
{{> options-targets-lib-bin-example }}
5555
{{/options}}
5656

5757
{{> section-features }}

src/doc/man/includes/options-targets-lib-bin.md renamed to src/doc/man/includes/options-targets-lib-bin-example.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ and supports common Unix glob patterns.
1010
{{#option "`--bins`" }}
1111
{{actionverb}} all binary targets.
1212
{{/option}}
13+
14+
{{#option "`--example` _name_..." }}
15+
{{actionverb}} the specified example. This flag may be specified multiple times
16+
and supports common Unix glob patterns.
17+
{{/option}}
18+
19+
{{#option "`--examples`" }}
20+
{{actionverb}} all examples targets.
21+
{{/option}}
22+

tests/testsuite/doc.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,109 @@ 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+
618+
516619
#[cargo_test]
517620
fn doc_dash_p() {
518621
let p = project()

0 commit comments

Comments
 (0)