Skip to content

Commit

Permalink
Merge pull request #194 from dtolnay/helpheading
Browse files Browse the repository at this point in the history
Add help headings matching `cargo rustc --help`
  • Loading branch information
dtolnay authored Sep 23, 2023
2 parents 5cdce7e + 905b808 commit a4a2dca
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 108 deletions.
98 changes: 49 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,32 +261,28 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path

line.arg("rustc");

line.arg("--profile");
if let Some(profile) = &args.profile {
line.arg(profile);
} else if args.tests && args.test.is_none() {
if args.release {
line.arg("bench");
} else {
line.arg("test");
}
} else if args.release {
line.arg("release");
} else {
line.arg("check");
if args.verbose {
line.arg("--verbose");
}

if let Some(features) = &args.features {
line.arg("--features");
line.arg(features);
line.arg("--color");
match color {
Coloring::Auto => line.arg(if cfg!(not(windows)) && io::stderr().is_terminal() {
"always"
} else {
"never"
}),
color => line.arg(color.to_possible_value().unwrap().get_name()),
}

if args.all_features {
line.arg("--all-features");
for unstable_flag in &args.unstable_flags {
line.arg("-Z");
line.arg(unstable_flag);
}

if args.no_default_features {
line.arg("--no-default-features");
if let Some(package) = &args.package {
line.arg("--package");
line.args(package);
}

let mut has_explicit_build_target = false;
Expand Down Expand Up @@ -330,43 +326,52 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
}
}

if let Some(target) = &args.target {
line.arg("--target");
line.arg(target);
}

if let Some(target_dir) = &args.target_dir {
line.arg("--target-dir");
line.arg(target_dir);
if let Some(features) = &args.features {
line.arg("--features");
line.arg(features);
}

if let Some(manifest_path) = &args.manifest_path {
line.arg("--manifest-path");
line.arg(manifest_path);
if args.all_features {
line.arg("--all-features");
}

if let Some(package) = &args.package {
line.arg("--package");
line.args(package);
if args.no_default_features {
line.arg("--no-default-features");
}

if let Some(jobs) = args.jobs {
line.arg("--jobs");
line.arg(jobs.to_string());
}

if args.verbose {
line.arg("--verbose");
line.arg("--profile");
if let Some(profile) = &args.profile {
line.arg(profile);
} else if args.tests && args.test.is_none() {
if args.release {
line.arg("bench");
} else {
line.arg("test");
}
} else if args.release {
line.arg("release");
} else {
line.arg("check");
}

line.arg("--color");
match color {
Coloring::Auto => line.arg(if cfg!(not(windows)) && io::stderr().is_terminal() {
"always"
} else {
"never"
}),
color => line.arg(color.to_possible_value().unwrap().get_name()),
if let Some(target) = &args.target {
line.arg("--target");
line.arg(target);
}

if let Some(target_dir) = &args.target_dir {
line.arg("--target-dir");
line.arg(target_dir);
}

if let Some(manifest_path) = &args.manifest_path {
line.arg("--manifest-path");
line.arg(manifest_path);
}

if args.frozen {
Expand All @@ -381,11 +386,6 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
line.arg("--offline");
}

for unstable_flag in &args.unstable_flags {
line.arg("-Z");
line.arg(unstable_flag);
}

line.arg("--");

line.arg("-o");
Expand Down
125 changes: 66 additions & 59 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use std::path::PathBuf;
use std::str::FromStr;
use syn_select::Selector;

// Help headings
const PACKAGE_SELECTION: &str = "Package Selection";
const TARGET_SELECTION: &str = "Target Selection";
const FEATURE_SELECTION: &str = "Feature Selection";
const COMPILATION_OPTIONS: &str = "Compilation Options";
const MANIFEST_OPTIONS: &str = "Manifest Options";

#[derive(Parser)]
#[command(bin_name = "cargo", version, author, disable_help_subcommand = true)]
pub enum Subcommand {
Expand All @@ -13,113 +20,113 @@ pub enum Subcommand {

#[derive(Parser, Debug)]
pub struct Expand {
/// Space-separated list of features to activate
#[arg(long, value_name = "FEATURES")]
pub features: Option<String>,
/// Do not attempt to run rustfmt
#[arg(long)]
pub ugly: bool,

/// Activate all available features
/// Select syntax highlighting theme
#[arg(long, value_name = "NAME")]
pub theme: Option<String>,

/// Print available syntax highlighting theme names
#[arg(long)]
pub all_features: bool,
pub themes: bool,

/// Do not activate the `default` feature
/// Print command lines as they are executed
#[arg(long)]
pub no_default_features: bool,
pub verbose: bool,

/// Expand only this package's library
/// Coloring: auto, always, never
#[arg(long, value_name = "WHEN")]
pub color: Option<Coloring>,

/// Unstable (nightly-only) flags to Cargo
#[arg(short = 'Z', value_name = "FLAG")]
pub unstable_flags: Vec<String>,

/// Print version
#[arg(long)]
pub version: bool,

/// Package to expand
#[arg(short, long, value_name = "SPEC", num_args = 0..=1, help_heading = PACKAGE_SELECTION)]
pub package: Option<Option<String>>,

/// Expand only this package's library
#[arg(long, help_heading = TARGET_SELECTION)]
pub lib: bool,

/// Expand only the specified binary
#[arg(long, value_name = "NAME", num_args = 0..=1)]
#[arg(long, value_name = "NAME", num_args = 0..=1, help_heading = TARGET_SELECTION)]
pub bin: Option<Option<String>>,

/// Expand only the specified example
#[arg(long, value_name = "NAME", num_args = 0..=1)]
#[arg(long, value_name = "NAME", num_args = 0..=1, help_heading = TARGET_SELECTION)]
pub example: Option<Option<String>>,

/// Expand only the specified test target
#[arg(long, value_name = "NAME", num_args = 0..=1)]
#[arg(long, value_name = "NAME", num_args = 0..=1, help_heading = TARGET_SELECTION)]
pub test: Option<Option<String>>,

/// Include tests when expanding the lib or bin
#[arg(long)]
#[arg(long, help_heading = TARGET_SELECTION)]
pub tests: bool,

/// Expand only the specified bench target
#[arg(long, value_name = "NAME", num_args = 0..=1)]
#[arg(long, value_name = "NAME", num_args = 0..=1, help_heading = TARGET_SELECTION)]
pub bench: Option<Option<String>>,

/// Target triple which compiles will be for
#[arg(long, value_name = "TARGET")]
pub target: Option<String>,
/// Space-separated list of features to activate
#[arg(long, value_name = "FEATURES", help_heading = FEATURE_SELECTION)]
pub features: Option<String>,

/// Directory for all generated artifacts
#[arg(long, value_name = "DIRECTORY")]
pub target_dir: Option<PathBuf>,
/// Activate all available features
#[arg(long, help_heading = FEATURE_SELECTION)]
pub all_features: bool,

/// Path to Cargo.toml
#[arg(long, value_name = "PATH")]
pub manifest_path: Option<PathBuf>,
/// Do not activate the `default` feature
#[arg(long, help_heading = FEATURE_SELECTION)]
pub no_default_features: bool,

/// Package to expand
#[arg(short, long, value_name = "SPEC", num_args = 0..=1)]
pub package: Option<Option<String>>,
/// Number of parallel jobs, defaults to # of CPUs
#[arg(short, long, value_name = "N", help_heading = COMPILATION_OPTIONS)]
pub jobs: Option<u64>,

/// Build artifacts in release mode, with optimizations
#[arg(long)]
#[arg(long, help_heading = COMPILATION_OPTIONS)]
pub release: bool,

/// Build artifacts with the specified profile
#[arg(long, value_name = "PROFILE-NAME")]
#[arg(long, value_name = "PROFILE-NAME", help_heading = COMPILATION_OPTIONS)]
pub profile: Option<String>,

/// Number of parallel jobs, defaults to # of CPUs
#[arg(short, long, value_name = "N")]
pub jobs: Option<u64>,
/// Target triple which compiles will be for
#[arg(long, value_name = "TARGET", help_heading = COMPILATION_OPTIONS)]
pub target: Option<String>,

/// Print command lines as they are executed
#[arg(long)]
pub verbose: bool,
/// Directory for all generated artifacts
#[arg(long, value_name = "DIRECTORY", help_heading = COMPILATION_OPTIONS)]
pub target_dir: Option<PathBuf>,

/// Coloring: auto, always, never
#[arg(long, value_name = "WHEN")]
pub color: Option<Coloring>,
/// Path to Cargo.toml
#[arg(long, value_name = "PATH", help_heading = MANIFEST_OPTIONS)]
pub manifest_path: Option<PathBuf>,

/// Require Cargo.lock and cache are up to date
#[arg(long)]
#[arg(long, help_heading = MANIFEST_OPTIONS)]
pub frozen: bool,

/// Require Cargo.lock is up to date
#[arg(long)]
#[arg(long, help_heading = MANIFEST_OPTIONS)]
pub locked: bool,

/// Run without accessing the network
#[arg(long)]
#[arg(long, help_heading = MANIFEST_OPTIONS)]
pub offline: bool,

/// Unstable (nightly-only) flags to Cargo
#[arg(short = 'Z', value_name = "FLAG")]
pub unstable_flags: Vec<String>,

/// Do not attempt to run rustfmt
#[arg(long)]
pub ugly: bool,

/// Select syntax highlighting theme
#[arg(long, value_name = "NAME")]
pub theme: Option<String>,

/// Print available syntax highlighting theme names
#[arg(long)]
pub themes: bool,

/// Local path to module or other named item to expand, e.g. os::unix::ffi
#[arg(value_name = "ITEM", value_parser = parse_selector)]
pub item: Option<Selector>,

/// Print version
#[arg(long)]
pub version: bool,
}

#[derive(ValueEnum, Debug, Clone, Copy)]
Expand Down

0 comments on commit a4a2dca

Please sign in to comment.