Skip to content

Commit

Permalink
Add -V and -p flags/options (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
msrd0 authored Nov 28, 2022
1 parent 07350d3 commit 737de1b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl CrateCode {

pub fn read_expansion<P>(
manifest_path: Option<P>,
package: Option<String>,
target: &Target,
features: Option<String>,
no_default_features: bool,
Expand All @@ -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);
}
Expand Down
28 changes: 20 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
package: Option<String>,
prefer_bin: bool,
expand_macros: bool,
template: PathBuf,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -171,6 +182,7 @@ pub fn read_input(
unwrap!(
CrateCode::read_expansion(
manifest_path.as_ref(),
package,
target,
features,
no_default_features,
Expand Down
39 changes: 28 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,55 +96,60 @@ enum Subcommand {
}

#[derive(Parser)]
#[command(about, version)]
struct Args {
/// Path to Cargo.toml.
#[clap(long)]
#[arg(long)]
manifest_path: Option<PathBuf>,

/// Package to read.
#[arg(short, long)]
package: Option<String>,

/// 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<String>,

/// 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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
2 changes: 2 additions & 0 deletions tests/fail/workspace-root/stderr.log
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 737de1b

Please sign in to comment.