Skip to content

Rewatch cli help: do not show build command options in the root help #7715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Polish arity mismatch error message a bit. https://github.com/rescript-lang/rescript/pull/7709
- Suggest related functions with the expected arity in errors when it makes sense. https://github.com/rescript-lang/rescript/pull/7712
- Improve error when a constructor expects an inline record. https://github.com/rescript-lang/rescript/pull/7713
- Rewatch cli: do not show build command options in the root help. https://github.com/rescript-lang/rescript/pull/7715

#### :house: Internal

Expand Down
10 changes: 5 additions & 5 deletions rewatch/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum FileExtension {
#[derive(Parser, Debug)]
#[command(version)]
#[command(args_conflicts_with_subcommands = true)]
#[command(
after_help = "Note: If no command is provided, the build command is run by default. See `rescript help build` for more information."
)]
pub struct Cli {
/// Verbosity:
/// -v -> Debug
Expand All @@ -35,10 +38,7 @@ pub struct Cli {

/// The command to run. If not provided it will default to build.
#[command(subcommand)]
pub command: Option<Command>,

#[command(flatten)]
pub build_args: BuildArgs,
pub command: Command,
}

#[derive(Args, Debug, Clone)]
Expand Down Expand Up @@ -164,7 +164,7 @@ impl From<BuildArgs> for WatchArgs {

#[derive(Subcommand, Clone, Debug)]
pub enum Command {
/// Build the project
/// Build the project (default command)
Build(BuildArgs),
/// Build, then start a watcher
Watch(WatchArgs),
Expand Down
25 changes: 21 additions & 4 deletions rewatch/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use anyhow::Result;
use clap::Parser;
use log::LevelFilter;
use std::{io::Write, path::Path};
use std::{env, io::Write, path::Path};

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

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

let log_level_filter = args.verbose.log_level_filter();
let cli = cli::Cli::parse_from(args);

let log_level_filter = cli.verbose.log_level_filter();

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

let mut command = args.command.unwrap_or(cli::Command::Build(args.build_args));
let mut command = cli.command;

if let cli::Command::Build(build_args) = &command {
if build_args.watch {
Expand Down Expand Up @@ -109,3 +112,17 @@ fn get_lock(folder: &str) -> lock::Lock {
acquired_lock => acquired_lock,
}
}

fn handle_default_arg(args: &mut Vec<String>) {
let first_arg = args.get(1);
let global_flags = ["-h", "--help", "-V", "--version"];

let needs_default_arg = match first_arg {
Some(arg) => arg.starts_with("-") && !global_flags.contains(&arg.as_str()),
None => true,
};

if needs_default_arg {
args.insert(1, "build".to_string());
}
}