Skip to content

Commit

Permalink
Use the cargo cli style
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Oct 1, 2024
1 parent 08cdda3 commit 60c6852
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cargo = "0.82.0"
cargo-util = "0.2"
semver = "1.0.3"
log = "0.4"
clap = { version = "4.0.29", features = ["color", "derive", "cargo", "string"] }
clap = { version = "4.5.18", features = ["color", "derive", "cargo", "string", "wrap_help"] }
regex = "1.5.6"
cbindgen = { version="0.27.0", default-features=false }
toml = "0.8"
Expand Down
21 changes: 9 additions & 12 deletions src/bin/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ fn main() -> CliResult {
let cli_install = subcommand_install("install", "Install the crate C-API");
let cli_test = subcommand_test("test");

let mut app = clap::command!()
.dont_collapse_args_in_usage(true)
.allow_external_subcommands(true)
.subcommand(
Command::new("capi")
.allow_external_subcommands(true)
.about("Build or install the crate C-API")
.arg(flag("version", "Print version info and exit").short('V'))
.subcommand(cli_build)
.subcommand(cli_install)
.subcommand(cli_test),
);
let mut app = main_cli().subcommand(
Command::new("capi")
.allow_external_subcommands(true)
.about("Build or install the crate C-API")
.arg(flag("version", "Print version info and exit").short('V'))
.subcommand(cli_build)
.subcommand(cli_install)
.subcommand(cli_test),
);

let args = app.clone().get_matches();

Expand Down
8 changes: 2 additions & 6 deletions src/bin/cbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ use cargo::CliResult;
use cargo::GlobalContext;

use cargo_c::build::*;
use cargo_c::cli::run_cargo_fallback;
use cargo_c::cli::subcommand_build;
use cargo_c::cli::{main_cli, run_cargo_fallback, subcommand_build};
use cargo_c::config::*;

fn main() -> CliResult {
let mut config = GlobalContext::default()?;

let subcommand = subcommand_build("cbuild", "Build the crate C-API");
let mut app = clap::command!()
.dont_collapse_args_in_usage(true)
.allow_external_subcommands(true)
.subcommand(subcommand);
let mut app = main_cli().subcommand(subcommand);

let args = app.clone().get_matches();

Expand Down
8 changes: 2 additions & 6 deletions src/bin/cinstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ use cargo::CliResult;
use cargo::GlobalContext;

use cargo_c::build::cbuild;
use cargo_c::cli::run_cargo_fallback;
use cargo_c::cli::subcommand_install;
use cargo_c::cli::{main_cli, run_cargo_fallback, subcommand_install};
use cargo_c::config::global_context_configure;
use cargo_c::install::cinstall;

fn main() -> CliResult {
let mut config = GlobalContext::default()?;

let subcommand = subcommand_install("cinstall", "Install the crate C-API");
let mut app = clap::command!()
.dont_collapse_args_in_usage(true)
.allow_external_subcommands(true)
.subcommand(subcommand);
let mut app = main_cli().subcommand(subcommand);

let args = app.clone().get_matches();

Expand Down
8 changes: 2 additions & 6 deletions src/bin/ctest.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use cargo::util::command_prelude::*;

use cargo_c::build::*;
use cargo_c::cli::run_cargo_fallback;
use cargo_c::cli::subcommand_test;
use cargo_c::cli::{main_cli, run_cargo_fallback, subcommand_test};
use cargo_c::config::*;

fn main() -> CliResult {
let mut config = GlobalContext::default()?;

let subcommand = subcommand_test("ctest");

let mut app = clap::command!()
.dont_collapse_args_in_usage(true)
.allow_external_subcommands(true)
.subcommand(subcommand);
let mut app = main_cli().subcommand(subcommand);

let args = app.clone().get_matches();

Expand Down
19 changes: 18 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use cargo::util::command_prelude::CommandExt;
use cargo::util::command_prelude::{flag, multi_opt, opt};
use cargo::util::{CliError, CliResult};
use cargo::util::{style, CliError, CliResult};

use cargo_util::{ProcessBuilder, ProcessError};

Expand Down Expand Up @@ -57,6 +57,23 @@ struct Common {
meson: bool,
}

pub fn main_cli() -> Command {
let styles = {
clap::builder::styling::Styles::styled()
.header(style::HEADER)
.usage(style::USAGE)
.literal(style::LITERAL)
.placeholder(style::PLACEHOLDER)
.error(style::ERROR)
.valid(style::VALID)
.invalid(style::INVALID)
};
clap::command!()
.dont_collapse_args_in_usage(true)
.allow_external_subcommands(true)
.styles(styles)
}

fn base_cli() -> Command {
let default_target = Target::new::<&str>(None, false);
let app = Common::command()
Expand Down

0 comments on commit 60c6852

Please sign in to comment.