diff --git a/src/input.rs b/src/input.rs index e15798f..97b2abc 100644 --- a/src/input.rs +++ b/src/input.rs @@ -232,6 +232,7 @@ impl CrateCode { pub fn read_expansion

( manifest_path: Option

, + package: Option, target: &Target, features: Option, no_default_features: bool, @@ -245,6 +246,9 @@ impl CrateCode { if let Some(manifest_path) = manifest_path { cmd.arg("--manifest-path").arg(manifest_path.as_ref()); } + if let Some(package) = package.as_deref() { + cmd.arg("-p").arg(package); + } if let Some(features) = features { cmd.arg("--features").arg(features); } diff --git a/src/lib.rs b/src/lib.rs index 5662d17..2aed3b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,12 +26,14 @@ use diagnostic::Diagnostic; use input::{CrateCode, InputFile, TargetType}; #[doc(hidden)] -/// Read input. The manifest path, if present, will be passed to `cargo metadata`. If you set -/// expand_macros to true, the input will be passed to the rust compiler to expand macros. This -/// will only work on a nightly compiler. The template doesn't have to exist, a default will -/// be used if it does not exist. +#[allow(clippy::too_many_arguments)] // TODO +/// Read input. The manifest path options, if present, will be passed to +/// `cargo metadata`. If you set expand_macros to true, the input will be passed to the +/// rust compiler to expand macros. This will only work on a nightly compiler. The +/// template doesn't have to exist, a default will be used if it does not exist. pub fn read_input( manifest_path: Option, + package: Option, prefer_bin: bool, expand_macros: bool, template: PathBuf, @@ -109,10 +111,19 @@ pub fn read_input( cmd.manifest_path(path); } let metadata = unwrap!(cmd.exec(), "Failed to get cargo metadata"); - let pkg = unwrap!( - metadata.root_package(), - "Missing package. Please make sure there is a package here, workspace roots don't contain any documentation." - ); + let pkg = match package.as_deref() { + Some(package) => unwrap!( + metadata.packages.iter().find(|pkg| pkg.name == package), + "Cannot find requested package" + ), + None => unwrap!( + metadata.root_package(), + // TODO this could be a real "help" message from ariadne + r#"Missing package. Please make sure there is a package here, workspace roots don't contain any documentation. + +Help: You can use --manifest-path and/or -p to specify the package to use."# + ) + }; // find the target whose rustdoc comment we'll use. // this uses a library target if exists, otherwise a binary target with the same name as the @@ -171,6 +182,7 @@ pub fn read_input( unwrap!( CrateCode::read_expansion( manifest_path.as_ref(), + package, target, features, no_default_features, diff --git a/src/main.rs b/src/main.rs index dfc8546..7de2ca6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,55 +96,60 @@ enum Subcommand { } #[derive(Parser)] +#[command(about, version)] struct Args { /// Path to Cargo.toml. - #[clap(long)] + #[arg(long)] manifest_path: Option, + /// Package to read. + #[arg(short, long)] + package: Option, + /// Output File. - #[clap(short, long, default_value = "README.md")] + #[arg(short, long, default_value = "README.md")] out: PathBuf, /// Template File. This is processed by minijinja. Look at the source code for /// cargo-doc2readme for an example. - #[clap(short, long, default_value = "README.j2")] + #[arg(short, long, default_value = "README.j2")] template: PathBuf, /// Use nightly rustc to expand macros prior to reading the source. This is necessary /// if you use function-like macros in doc attributes, as introduced in Rust 1.54. - #[clap(long)] + #[arg(long)] expand_macros: bool, /// Space or comma separated list of features to activate. This will be ignored unless /// `--expand-macros` is enabled, in which case it is being passed to cargo. - #[clap(short = 'F', long)] + #[arg(short = 'F', long)] features: Option, /// Activate all available features. This will be ignored unless `--expand-macros` is /// enabled, in which case it is being passed to cargo. - #[clap(long)] + #[arg(long)] all_features: bool, /// Do not activate the `default` feature. This will be ignored unless /// `--expand-macros` is enabled, in which case it is being passed to cargo. - #[clap(long)] + #[arg(long)] no_default_features: bool, /// Prefer binary targets over library targets for rustdoc source. - #[clap(long, conflicts_with = "lib")] + #[arg(long, conflicts_with = "lib")] bin: bool, /// Prefer library targets over binary targets for rustdoc source. This is the default. - #[clap(long, conflicts_with = "bin")] + #[arg(long, conflicts_with = "bin")] lib: bool, /// Verify that the output file is (reasonably) up to date, and fail /// if it needs updating. The output file will not be changed. - #[clap(long)] + #[arg(long)] check: bool, /// Enable verbose output. - #[clap(short, long)] + #[arg(short, long)] verbose: bool } @@ -194,6 +199,7 @@ fn main() -> ExitCode { let (input_file, template, diagnostics) = read_input( args.manifest_path, + args.package, args.bin, args.expand_macros, args.template, @@ -245,3 +251,14 @@ fn main() -> ExitCode { ExitCode::SUCCESS } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Args::command().debug_assert() + } +} diff --git a/tests/fail/workspace-root/stderr.log b/tests/fail/workspace-root/stderr.log index 605f25f..3fc9e9b 100644 --- a/tests/fail/workspace-root/stderr.log +++ b/tests/fail/workspace-root/stderr.log @@ -1 +1,3 @@ Error: Missing package. Please make sure there is a package here, workspace roots don't contain any documentation. + +Help: You can use --manifest-path and/or -p to specify the package to use. diff --git a/tests/tests.rs b/tests/tests.rs index d8dadb1..3127750 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -65,6 +65,7 @@ fn run_test(data: &TestData) -> Result<(), Failed> { let (input_file, template, diagnostic) = read_input( Some(manifest_path), + None, false, data.config.expand_macros, template_path,