Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Add select support for '-x'-less cargo commands
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Jan 4, 2024
1 parent 466c6bb commit 53686df
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
26 changes: 21 additions & 5 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,35 @@ pub fn parse() -> ArgMatches<'static> {
.short("L")
.takes_value(true)
)
.arg(
Arg::with_name("cmd:trail")
.raw(true)
.help("Full command to run. -x and -s will be ignored!"),
)
.arg(
Arg::with_name("skip-local-deps")
.help("Don't try to find local dependencies of the current crate and watch their working directories. Only watch the current directory.")
.long("skip-local-deps")
)
.subcommand(special_cargo_subc("bench"))
.subcommand(special_cargo_subc("build"))
.subcommand(special_cargo_subc("check"))
.subcommand(special_cargo_subc("clippy"))
.subcommand(special_cargo_subc("test"))
.arg(
Arg::with_name("cmd:trail")
.raw(true)
.help("Full command to run. -x and -s will be ignored!"),
)
.after_help(footnote.as_str()),
);

fn special_cargo_subc(name: &str) -> App {
SubCommand::with_name(name)
.setting(AppSettings::AllowLeadingHyphen)
.setting(AppSettings::DisableHelpFlags)
.setting(AppSettings::DisableHelpSubcommand)
.setting(AppSettings::DisableVersion)
.setting(AppSettings::Hidden)
.setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("args").multiple(true))
}

// Allow invocation of cargo-watch with both `cargo-watch watch ARGS`
// (as invoked by cargo) and `cargo-watch ARGS`.
let mut args: Vec<String> = env::args().collect();
Expand Down
29 changes: 26 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use cargo_metadata::{MetadataCommand, Node, Package, PackageId};
use clap::{value_t, values_t, ArgMatches};
use clap::{value_t, values_t, ArgMatches, ErrorKind};
use log::{debug, warn};
use watchexec::{
config::{Config, ConfigBuilder},
Expand All @@ -21,9 +21,32 @@ pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) {
// and before the remaining arguments
let features = value_t!(matches, "features", String).ok();

let subcommand_cargo = {
let (name, args) = matches.subcommand();
if name == "" {
None
} else if let Some(args) = args {
let args = values_t!(args, "args", String).unwrap_or_else(|e| e.exit()).join(" ");
Some(format!("{name} {args}"))
} else {
// shouldn't happen per clap2, but just in case:
Some(name.to_string())
}
};

// Cargo commands are in front of the rest
if matches.is_present("cmd:cargo") {
for cargo in values_t!(matches, "cmd:cargo", String).unwrap_or_else(|e| e.exit()) {
if matches.is_present("cmd:cargo") || subcommand_cargo.is_some() {
let normal_cargos = values_t!(matches, "cmd:cargo", String).unwrap_or_else(|e| {
if e.kind == ErrorKind::ArgumentNotFound {
Vec::new()
} else {
e.exit()
}
});
for cargo in normal_cargos
.into_iter()
.chain(subcommand_cargo.into_iter())
{
let mut cmd: String = "cargo ".into();
let cargo = cargo.trim_start();
// features are supported for the following
Expand Down

0 comments on commit 53686df

Please sign in to comment.