Skip to content

Commit 6b58ac0

Browse files
cknittclaude
andauthored
Drop the build system's clap-era argument rewriting (#9266)
* Require the command before options in the build system's CLI Why: normalize_argv rewrote argv so that Rust-era forms (a bare folder, options before the command, -h/-V clusters) kept working on Cmdliner. Its help/version precedence dropped positional words, so an unknown command followed by --help or --version printed help or the version and exited with 0. Only -h and -V remain as aliases now. Diagnostic modified (unknown command with a display flag): Before (text): $ rescript invalid --help NAME rescript - Fast, Simple, Fully Typed JavaScript from the Future … exit 0 After (text): $ rescript invalid --help Usage: rescript [--help] [COMMAND] … rescript: unknown command invalid. Must be one of build, clean, compiler-args, format, help or watch exit 2 Diagnostic modified (options before the command): Before (text): $ rescript -v build (builds the current folder with debug output) After (text): $ rescript -v build Usage: rescript [--help] [COMMAND] … rescript: options go after the command: rescript build [OPTION]… (write ./build for a folder named build) exit 2 Diagnostic modified (bare folder): Before (text): $ rescript ./pkg (builds ./pkg) After (text): $ rescript ./pkg rescript: unknown command ./pkg. Must be one of build, clean, … exit 2 Review: Why reject in the default term instead of scanning argv? Options such as -f take a value, so a scan cannot tell `-f build` from `-v build`; Cmdliner has already parsed the options when the default term sees a folder named like a command. Breaking: `rescript <folder>` becomes `rescript build <folder>`, options go after the command (`rescript build -v`), and -h/-V no longer combine with other flags in one cluster. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gLt5FhNDjMw2sNude8 Signed-off-by: Christoph Knittel <ck@cca.io> * Match unknown commands in the CLI test whether or not colors are on CI forces colors, and Cmdliner then quotes the command name instead of styling it, so the test checks the message and the name separately. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gLt5FhNDjMw2sNude8 Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 53a934d commit 6b58ac0

9 files changed

Lines changed: 87 additions & 148 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `rescript` needs the command before its options and folder, as in `rescript build -v ./pkg`, and reports an unknown command even when `--help` or `--version` follows it.

‎rewatch-ocaml/cli.ml‎

Lines changed: 27 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -287,104 +287,46 @@ let root =
287287
~version:("rescript " ^ Rewatch_version.version)
288288
~doc:"Fast, Simple, Fully Typed JavaScript from the Future" ~man
289289
in
290-
Cmd.group info ~default:(build_term ~watch:false)
291-
[build; watch; clean; format; compiler_args; help]
290+
let commands = [build; watch; clean; format; compiler_args; help] in
291+
(* Options before a command name make Cmdliner read the name as the folder of
292+
the default build, so [rescript -v watch] would build a folder named
293+
watch. *)
294+
let default =
295+
Term.term_result ~usage:true
296+
(let+ command = build_term ~watch:false in
297+
match command with
298+
| Build {folder}
299+
when List.exists (fun cmd -> Cmd.name cmd = folder) commands ->
300+
Error
301+
(`Msg
302+
(Printf.sprintf
303+
"options go after the command: rescript %s [OPTION]… (write \
304+
./%s for a folder named %s)"
305+
folder folder folder))
306+
| command -> Ok command)
307+
in
308+
Cmd.group info ~default commands
292309

293310
type evaluation = Run of command | Exit of int
294311

295312
exception Parse_error of string
296313
exception Help
297314
exception Version
298315

299-
(* Bare project folders must select the default build command, even though
300-
Cmdliner otherwise treats them as unknown commands. Move global options
301-
behind the selected command and expand short help/version clusters because
302-
Cmdliner's standard display options only provide long names. A root display
303-
request takes precedence over otherwise invalid implicit-build arguments. *)
316+
(* Cmdliner's standard display options only have long names. Accept -h and -V
317+
as aliases, and print help as plain text instead of through a pager, up to a
318+
"--" that ends the options. *)
304319
let normalize_argv argv =
305-
let is_short_global_cluster argument =
306-
let length = String.length argument in
307-
length > 1
308-
&& argument.[0] = '-'
309-
&& argument.[1] <> '-'
310-
&& String.for_all
311-
(function
312-
| 'v' | 'q' | 'h' | 'V' -> true
313-
| _ -> false)
314-
(String.sub argument 1 (length - 1))
315-
in
316-
let short_cluster_contains character argument =
317-
is_short_global_cluster argument
318-
&& String.contains_from argument 1 character
319-
in
320-
let is_global = function
321-
| "--verbose" | "--quiet" | "--help" | "--version" -> true
322-
| argument ->
323-
is_short_global_cluster argument
324-
|| String.starts_with ~prefix:"--help=" argument
325-
in
326-
let requests_help argument =
327-
argument = "--help"
328-
|| String.starts_with ~prefix:"--help=" argument
329-
|| short_cluster_contains 'h' argument
330-
in
331-
let requests_version argument =
332-
argument = "--version" || short_cluster_contains 'V' argument
333-
in
334-
let is_command = function
335-
| "build" | "watch" | "clean" | "format" | "compiler-args" | "help" -> true
336-
| _ -> false
337-
in
338-
let rec normalize_display_options = function
320+
let rec normalize = function
339321
| [] -> []
340322
| "--" :: rest -> "--" :: rest
341-
| argument :: rest when String.starts_with ~prefix:"--help=" argument ->
342-
argument :: normalize_display_options rest
343-
| argument :: rest when requests_help argument ->
344-
"--help=plain" :: normalize_display_options rest
345-
| argument :: rest when requests_version argument ->
346-
"--version" :: normalize_display_options rest
347-
| argument :: rest -> argument :: normalize_display_options rest
348-
in
349-
let explicit_command arguments =
350-
let rec loop globals = function
351-
| [] | "--" :: _ -> None
352-
| argument :: rest when is_global argument ->
353-
loop (argument :: globals) rest
354-
| command :: rest when is_command command ->
355-
Some (List.rev globals, command, rest)
356-
| _ -> None
357-
in
358-
loop [] arguments
359-
in
360-
let partition_implicit arguments =
361-
let rec loop globals others = function
362-
| [] -> (List.rev globals, List.rev others)
363-
| "--" :: rest -> (List.rev globals, List.rev_append others ("--" :: rest))
364-
| argument :: rest when is_global argument ->
365-
loop (argument :: globals) others rest
366-
| argument :: rest -> loop globals (argument :: others) rest
367-
in
368-
loop [] [] arguments
323+
| ("-h" | "--help") :: rest -> "--help=plain" :: normalize rest
324+
| "-V" :: rest -> "--version" :: normalize rest
325+
| argument :: rest -> argument :: normalize rest
369326
in
370327
match Array.to_list argv with
371328
| [] -> argv
372-
| executable :: arguments ->
373-
let routed =
374-
match explicit_command arguments with
375-
| Some (globals, command, rest) ->
376-
executable :: command :: (globals @ rest)
377-
| None ->
378-
let globals, others = partition_implicit arguments in
379-
if
380-
List.exists
381-
(fun argument ->
382-
requests_help argument || requests_version argument)
383-
globals
384-
then executable :: globals
385-
else executable :: "build" :: (globals @ others)
386-
in
387-
Array.of_list (normalize_display_options routed)
329+
| executable :: arguments -> Array.of_list (executable :: normalize arguments)
388330

389331
(* Evaluates the command line and returns Cmdliner's result with the
390332
diagnostics it wrote, or [Error] for arguments that are not UTF-8. Help is

‎rewatch-ocaml/tests/check_command_validation.sh‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ run_build_output_case() {
474474
build_project="$work/$name-build"
475475
cp -R "$fixture" "$build_project"
476476
if [ "$mode" = quiet ]; then
477-
run_rescript -q build "$build_project"
477+
run_rescript build -q "$build_project"
478478
elif [ "$mode" = forced-color ]; then
479479
CLICOLOR_FORCE=1 run_rescript build "$build_project"
480480
else
@@ -582,12 +582,15 @@ run_build_output_case quiet-warning 0 \
582582
"$root/rewatch-ocaml/tests/warning-replay" quiet
583583

584584
run_case build-subcommand-version accept build --version
585-
run_case clustered-global-version accept -vV build
586-
run_case clustered-global-verbosity accept -vvvvv build "$project"
587-
run_case clustered-global-version-before-help accept -Vh build
588-
run_case clustered-global-help-before-version accept -hV build
589-
run_case clustered-subcommand-help-before-version accept build -hV
590-
run_case clustered-subcommand-version-before-help accept build -Vh
585+
run_case short-help accept -h
586+
run_case short-version accept -V
587+
run_case clustered-verbosity accept build -vvvvv "$project"
588+
run_case options-before-command exit2 -v build
589+
require_error_contains options-before-command \
590+
'options go after the command: rescript build'
591+
run_case folder-named-like-command reject -v ./build
592+
require_error_contains folder-named-like-command \
593+
"Project folder './build' does not exist."
591594
"$rescript" build --help=groff >"$work/help-groff.out"
592595
if ! grep -F '.\" Pipe this output to groff' \
593596
"$work/help-groff.out" >/dev/null; then
@@ -597,11 +600,17 @@ if ! grep -F '.\" Pipe this output to groff' \
597600
fi
598601
checked=$((checked + 1))
599602
run_case implicit-conflicting-verbosity exit2 -v -q
600-
require_error_contains implicit-conflicting-verbosity 'Usage: rescript build'
603+
require_error_contains implicit-conflicting-verbosity \
604+
'verbose cannot be used together with --quiet'
601605
run_case build-conflicting-verbosity exit2 build --verbose --quiet
602-
run_case unknown-command reject bogus-command
603-
require_error_contains unknown-command \
604-
"Project folder 'bogus-command' does not exist. Run 'rescript --help' for the available commands."
606+
# Cmdliner quotes the command name when colors are off and styles it
607+
# otherwise, so the name is checked apart from the message.
608+
for display in "" --help -V; do
609+
name=unknown-command${display:+-$display}
610+
run_case "$name" exit2 bogus-command ${display:+"$display"}
611+
require_error_contains "$name" 'unknown command'
612+
require_error_contains "$name" 'bogus-command'
613+
done
605614
run_case filter-perl-quoting accept build --filter '\QA.res\E' "$project"
606615
run_case filter-invalid exit2 build --filter '(' "$project"
607616
require_error_contains filter-invalid 'invalid regular expression'
@@ -989,7 +998,7 @@ if [ "$rescript_status" -ne 0 ] || \
989998
fi
990999
check_output_snapshot clean-redirected
9911000
checked=$((checked + 1))
992-
run_rescript -q clean "$project"
1001+
run_rescript clean -q "$project"
9931002
if [ "$rescript_status" -ne 0 ] || \
9941003
[ -s "$work/rescript.out" ] || [ -s "$work/rescript.err" ]; then
9951004
echo "Quiet redirected clean emitted output" >&2
@@ -1122,7 +1131,7 @@ wait_for_file "$work/watch-dependency-recovery/src/A.js"
11221131
terminate_and_wait "$dependency_recovery_pid" "dependency-recovery watcher"
11231132
checked=$((checked + 1))
11241133

1125-
"$rescript" -v watch "$work/watch-dependency-install" \
1134+
"$rescript" watch -v "$work/watch-dependency-install" \
11261135
>"$work/watch-dependency-install.out" \
11271136
2>"$work/watch-dependency-install.err" &
11281137
dependency_install_pid=$!
@@ -1150,7 +1159,7 @@ checked=$((checked + 1))
11501159

11511160
directory_link "$work/watch-dependency-fallback/packages/dep" \
11521161
"$work/node_modules/dep"
1153-
"$rescript" -v watch "$work/watch-dependency-fallback" \
1162+
"$rescript" watch -v "$work/watch-dependency-fallback" \
11541163
>"$work/watch-dependency-fallback.out" \
11551164
2>"$work/watch-dependency-fallback.err" &
11561165
dependency_fallback_pid=$!
@@ -1256,7 +1265,7 @@ checked=$((checked + 1))
12561265
quiet_watch_project="$work/quiet-watch"
12571266
quiet_watch_marker="$quiet_watch_project/after-build.log"
12581267
REWATCH_WATCH_FILTER_MARKER="$quiet_watch_marker" \
1259-
"$rescript" -q watch \
1268+
"$rescript" watch -q \
12601269
--after-build "node $command_work/watch-filter-marker.js" \
12611270
"$quiet_watch_project" \
12621271
>"$work/quiet-watch.out" 2>"$work/quiet-watch.err" &

‎rewatch-ocaml/tests/check_interactive_output.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ if $windows_posix_shell; then
183183
fi
184184

185185
echo "Checking quiet interactive builds..."
186-
run_in_terminal "$work/quiet.tty" -q build "$(command_path "$work/project")"
186+
run_in_terminal "$work/quiet.tty" build -q "$(command_path "$work/project")"
187187
if tr '\r' '\n' <"$work/quiet.tty" \
188188
| grep -E '(Cleaned|Parsed|Parsing\.\.\.|Compiled|Compiling\.\.\.|Finished .*compilation)' >/dev/null; then
189189
echo "A quiet interactive build emitted progress" >&2
@@ -216,7 +216,7 @@ if ! cmp -s "$work/expected-clean" "$work/clean.phases"; then
216216
fi
217217

218218
echo "Checking quiet interactive cleans..."
219-
run_in_terminal "$work/clean-quiet.tty" -q clean "$(command_path "$work/project")"
219+
run_in_terminal "$work/clean-quiet.tty" clean -q "$(command_path "$work/project")"
220220
if tr '\r' '\n' <"$work/clean-quiet.tty" | grep -E '(Cleaning|Cleaned)' >/dev/null; then
221221
echo "A quiet interactive clean emitted progress" >&2
222222
cat "$work/clean-quiet.tty" >&2

‎rewatch-ocaml/tests/check_verbose_output.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Checks the DEBUG and TRACE events of `rescript -v build` and `-vv build`
2+
# Checks the DEBUG and TRACE events of `rescript build -v` and `build -vv`
33
# against snapshots/verbose_output. Only the event lines are compared, sorted,
44
# so parallel compilation does not reorder them.
55
set -eu
@@ -45,7 +45,7 @@ capture() {
4545
project="$work/$level"
4646
(
4747
cd "$project"
48-
"$rescript" "-$level" build >"$work/$name.stdout" 2>"$work/$name.stderr"
48+
"$rescript" build "-$level" >"$work/$name.stdout" 2>"$work/$name.stderr"
4949
)
5050
if [ -s "$work/$name.stderr" ]; then
5151
echo "$name unexpectedly wrote verbose build output to stderr" >&2

‎rewatch-ocaml/tests/run.sh‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ test ! -f "$watch_basic/src/New.js"
10771077
# A shallow watch on the nearest existing ancestor must advance one directory
10781078
# at a time until a configured nested source root exists. The slash in the JSON
10791079
# path also exercises native separator normalization on Windows.
1080-
"$port" -v watch "$missing_nested_source" \
1080+
"$port" watch -v "$missing_nested_source" \
10811081
>"$missing_nested_source/watch.log" 2>&1 &
10821082
missing_nested_source_pid=$!
10831083
background_pids="$background_pids $missing_nested_source_pid"
@@ -1725,7 +1725,7 @@ awk 'BEGIN { for (i = 1; i <= 200000; i++) printf "let v%d = (%d, \"%d\")\n", i,
17251725
>"$second_interrupt/src/Slow.res"
17261726

17271727
# A second build waits for the first one's build lock and then runs.
1728-
"$port" -vv build "$second_interrupt" >"$second_interrupt/first.log" 2>&1 &
1728+
"$port" build -vv "$second_interrupt" >"$second_interrupt/first.log" 2>&1 &
17291729
lock_first_pid=$!
17301730
background_pids="$background_pids $lock_first_pid"
17311731
# Logged when the parsed module is handed to the compiler.
@@ -1759,7 +1759,7 @@ if ! $windows_posix_shell; then
17591759
rm "$second_interrupt/src/Slow.js"
17601760
mkdir -p "$second_interrupt/tmp"
17611761
TMPDIR="$second_interrupt/tmp" \
1762-
"$port" -vv build "$second_interrupt" >"$second_interrupt/build.log" 2>&1 &
1762+
"$port" build -vv "$second_interrupt" >"$second_interrupt/build.log" 2>&1 &
17631763
second_interrupt_pid=$!
17641764
background_pids="$background_pids $second_interrupt_pid"
17651765
# Logged when the parsed module is handed to the compiler.
@@ -1860,7 +1860,7 @@ if ! $windows_posix_shell; then
18601860
expected_status=${interrupt_signal#*:}
18611861
interrupt_build="$work/interrupt-build-$signal_name"
18621862
slow_project "$interrupt_build"
1863-
"$port" -vv build "$interrupt_build" >"$interrupt_build/build.log" 2>&1 &
1863+
"$port" build -vv "$interrupt_build" >"$interrupt_build/build.log" 2>&1 &
18641864
interrupt_build_pid=$!
18651865
background_pids="$background_pids $interrupt_build_pid"
18661866
if ! wait_for_text "$interrupt_build/build.log" 'compile dirty: Slow'; then
@@ -1886,7 +1886,7 @@ fi
18861886
# watcher's compiler work like a signal, before the queued dependent.
18871887
lock_interrupt="$work/lock-interrupt"
18881888
slow_project "$lock_interrupt"
1889-
"$port" -vv watch "$lock_interrupt" >"$lock_interrupt/watch.log" 2>&1 &
1889+
"$port" watch -vv "$lock_interrupt" >"$lock_interrupt/watch.log" 2>&1 &
18901890
lock_interrupt_pid=$!
18911891
background_pids="$background_pids $lock_interrupt_pid"
18921892
if ! wait_for_text "$lock_interrupt/watch.log" 'compile dirty: Slow'; then
@@ -1909,7 +1909,7 @@ fi
19091909
# becoming the baseline of the next one.
19101910
atomic_save="$work/atomic-save-rebuild"
19111911
slow_project "$atomic_save"
1912-
"$port" -vv watch "$atomic_save" >"$atomic_save/watch.log" 2>&1 &
1912+
"$port" watch -vv "$atomic_save" >"$atomic_save/watch.log" 2>&1 &
19131913
atomic_save_pid=$!
19141914
background_pids="$background_pids $atomic_save_pid"
19151915
if ! wait_for_text "$atomic_save/watch.log" 'Finished initial compilation' 1 1200; then

‎rewatch/tests/watch/06-watch-missing-source-folder.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ node -e "
2626
"
2727

2828
# Start watcher and capture output (use -vv to get error logs)
29-
rewatch_bg -vv watch > rewatch.log 2>&1 &
29+
rewatch_bg watch -vv > rewatch.log 2>&1 &
3030
success "Watcher Started"
3131

3232
# Wait for initial build to complete

‎tests/build_tests/cli_help/input.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,11 @@ await test(["-h"], { stdout: cliHelp, stderr: "", status: 0 });
369369
// Shows cli help with -h arg
370370
await test(["help"], { stdout: cliHelp, stderr: "", status: 0 });
371371

372-
// Exits with cli help with unknown command
373-
// Exits with build usage on unknown args
372+
// Exits with cli usage on unknown args
374373
await test(["--foo"], {
375374
stdout: "",
376375
stderr:
377-
"Usage: rescript build [--help] [OPTION]… [FOLDER]\n" +
376+
"Usage: rescript [--help] [COMMAND] …\n" +
378377
"rescript: unknown option --foo. Did you mean -f?\n",
379378
status: 2,
380379
});

0 commit comments

Comments
 (0)