Skip to content

Commit 61127b9

Browse files
committed
Rewatch cli help: do not show build command options in the root help
1 parent 724232e commit 61127b9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

rewatch/src/cli.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum FileExtension {
2222
#[derive(Parser, Debug)]
2323
#[command(version)]
2424
#[command(args_conflicts_with_subcommands = true)]
25+
#[command(after_help = "NOTE: If no subcommand is provided, `build` is run by default.")]
2526
pub struct Cli {
2627
/// Verbosity:
2728
/// -v -> Debug
@@ -35,10 +36,7 @@ pub struct Cli {
3536

3637
/// The command to run. If not provided it will default to build.
3738
#[command(subcommand)]
38-
pub command: Option<Command>,
39-
40-
#[command(flatten)]
41-
pub build_args: BuildArgs,
39+
pub command: Command,
4240
}
4341

4442
#[derive(Args, Debug, Clone)]
@@ -164,7 +162,7 @@ impl From<BuildArgs> for WatchArgs {
164162

165163
#[derive(Subcommand, Clone, Debug)]
166164
pub enum Command {
167-
/// Build the project
165+
/// Build the project (default command)
168166
Build(BuildArgs),
169167
/// Build, then start a watcher
170168
Watch(WatchArgs),

rewatch/src/main.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
use anyhow::Result;
22
use clap::Parser;
33
use log::LevelFilter;
4-
use std::{io::Write, path::Path};
4+
use std::{env, io::Write, path::Path};
55

66
use rescript::{build, cli, cmd, format, lock, watcher};
77

88
fn main() -> Result<()> {
9-
let args = cli::Cli::parse();
9+
let mut args: Vec<String> = env::args().collect();
10+
handle_default_arg(&mut args);
1011

11-
let log_level_filter = args.verbose.log_level_filter();
12+
let cli = cli::Cli::parse_from(args);
13+
14+
let log_level_filter = cli.verbose.log_level_filter();
1215

1316
env_logger::Builder::new()
1417
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
1518
.filter_level(log_level_filter)
1619
.target(env_logger::fmt::Target::Stdout)
1720
.init();
1821

19-
let mut command = args.command.unwrap_or(cli::Command::Build(args.build_args));
22+
let mut command = cli.command;
2023

2124
if let cli::Command::Build(build_args) = &command {
2225
if build_args.watch {
@@ -109,3 +112,17 @@ fn get_lock(folder: &str) -> lock::Lock {
109112
acquired_lock => acquired_lock,
110113
}
111114
}
115+
116+
fn handle_default_arg(args: &mut Vec<String>) {
117+
let first_arg = args.get(1);
118+
let global_flags = ["-h", "--help", "-V", "--version"];
119+
120+
let needs_default_arg = match first_arg {
121+
Some(arg) if !arg.starts_with("-") || global_flags.contains(&arg.as_str()) => false,
122+
_ => true,
123+
};
124+
125+
if needs_default_arg {
126+
args.insert(1, "build".to_string());
127+
}
128+
}

0 commit comments

Comments
 (0)