Skip to content

Commit 0328980

Browse files
committed
Add cargo doc --lib/--bin
Allows documenting a specific target. Closes #2557
1 parent 88e3081 commit 0328980

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/bin/doc.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct Options {
1616
flag_quiet: Option<bool>,
1717
flag_color: Option<String>,
1818
flag_package: Vec<String>,
19+
flag_lib: bool,
20+
flag_bin: Vec<String>,
1921
}
2022

2123
pub const USAGE: &'static str = "
@@ -30,6 +32,8 @@ Options:
3032
-p SPEC, --package SPEC ... Package to document
3133
--no-deps Don't build documentation for dependencies
3234
-j N, --jobs N The number of jobs to run in parallel
35+
--lib Document only this package's library
36+
--bin NAME Document only the specified binary
3337
--release Build artifacts in release mode, with optimizations
3438
--features FEATURES Space-separated list of features to also build
3539
--no-default-features Do not build the `default` feature
@@ -55,6 +59,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
5559

5660
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
5761

62+
let empty = Vec::new();
5863
let doc_opts = ops::DocOptions {
5964
open_result: options.flag_open,
6065
compile_opts: ops::CompileOptions {
@@ -65,7 +70,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
6570
no_default_features: options.flag_no_default_features,
6671
spec: &options.flag_package,
6772
exec_engine: None,
68-
filter: ops::CompileFilter::Everything,
73+
filter: ops::CompileFilter::new(options.flag_lib,
74+
&options.flag_bin,
75+
&empty,
76+
&empty,
77+
&empty),
6978
release: options.flag_release,
7079
mode: ops::CompileMode::Doc {
7180
deps: !options.flag_no_deps,

tests/test_cargo_doc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,27 @@ test!(rerun_when_dir_removed {
550550
execs().with_status(0));
551551
assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
552552
});
553+
554+
test!(document_only_lib {
555+
let p = project("foo")
556+
.file("Cargo.toml", r#"
557+
[package]
558+
name = "foo"
559+
version = "0.0.1"
560+
authors = []
561+
"#)
562+
.file("src/lib.rs", r#"
563+
/// dox
564+
pub fn foo() {}
565+
"#)
566+
.file("src/bin/bar.rs", r#"
567+
/// ```
568+
/// ☃
569+
/// ```
570+
pub fn foo() {}
571+
fn main() { foo(); }
572+
"#);
573+
assert_that(p.cargo_process("doc").arg("--lib"),
574+
execs().with_status(0));
575+
assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
576+
});

0 commit comments

Comments
 (0)