Skip to content
9 changes: 9 additions & 0 deletions agents/skills/command-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ Supported metadata keys:

Only use `omarchy:examples` where there are args that need explaining.

`omarchy:args` also decides how the dispatcher reads a word after the command. A
group parent (a command with `omarchy-<name>-*` children) that documents only
flags, or no arguments at all, refuses a bare word as a mistyped subcommand:
`omarchy update firmwre` reports the unknown command instead of starting a full
update with the typo ignored. A parent that legitimately takes an operand
documents it (`# omarchy:args=<name>`), and its leftovers are passed through.

A flag with a separate value placeholder, such as `[--agent <id>]`, counts as an operand and disables this check; a value in the same token, such as `[--webcam-device=<device>]`, does not.

Prefer explicit metadata for user-facing commands. Keep routes consistent with
the filename unless there is a deliberate alias or compatibility route.

Expand Down
61 changes: 61 additions & 0 deletions bin/omarchy
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,52 @@ command_takes_only_flags() {
done
}

# A command documenting no operand at all (only flags, or nothing) cannot
# interpret a bare word left over after it. Under a group parent such a word is
# a mistyped subcommand, not an argument: `omarchy update firmwre` must not run
# a full update with the typo silently ignored.
command_takes_no_operand() {
local key="$1"
local token=""
local tokens=()

read -r -a tokens <<<"${COMMAND_ARGS[$key]}"
for token in "${tokens[@]}"; do
[[ $token == -* || $token == "[-"* ]] || return 1
done
}

# Sets UNKNOWN_SUBCOMMAND and succeeds when a group parent has no operand for
# these leftovers and the first word among them is not a flag. A `--` closes the
# option list, so the words after it belong to the command by the caller's own
# declaration and are never treated as a mistyped subcommand.
find_unknown_subcommand() {
local key="$1"
shift
local args=("$@")
local index=0

group_has_child_commands "${COMMAND_BINARY[$key]#omarchy-}" || return 1
command_takes_no_operand "$key" || return 1

for (( index = 0; index < ${#args[@]}; index++ )); do
[[ ${args[index]} == "--" ]] && return 1
[[ ${args[index]} == -* ]] && continue
UNKNOWN_SUBCOMMAND=$(join_words " " "${args[@]:index}")
return 0
done

return 1
}

report_unknown_subcommand() {
local route="$1"

echo "Unknown Omarchy command: $route $UNKNOWN_SUBCOMMAND" >&2
echo "Run '$route --help' for usage." >&2
return 127
}

load_group_commands() {
local group="$1"
local file=""
Expand Down Expand Up @@ -1075,6 +1121,16 @@ dispatch_fast_or_help() {
fi
fi

if [[ -z $key ]]; then
load_command_by_binary "$DIRECT_RESOLVED_BINARY"
key="${BINARY_TO_KEY[$DIRECT_RESOLVED_BINARY]}"
fi

if [[ -n $key ]] && find_unknown_subcommand "$key" "${remaining[@]}"; then
report_unknown_subcommand "$DIRECT_RESOLVED_ROUTE"
return 127
fi

exec "$binary_path" "${remaining[@]}"
fi

Expand Down Expand Up @@ -1139,6 +1195,11 @@ dispatch_or_help() {
fi
fi

if find_unknown_subcommand "$key" "${remaining[@]}"; then
report_unknown_subcommand "$RESOLVED_ROUTE"
return 127
fi

exec "$binary_path" "${remaining[@]}"
fi

Expand Down
11 changes: 3 additions & 8 deletions docs/cli-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ Resolution is longest-prefix: the router tries the full argument list as a
route, then drops trailing words until something matches. Whatever it drops is
passed to the binary as arguments. This runs in two passes.

The fast path joins argument prefixes with hyphens and checks for an
executable file: `omarchy theme set foo` probes `omarchy-theme-set-foo`, then
`omarchy-theme-set`, which exists — resolved without reading a single metadata
header. This exists because plain dispatch is the hot path: parsing the
headers of several hundred binaries on every invocation is measurable
latency (`omarchy dev benchmark cli` tracks it), and a filename probe is a few
stat calls. Metadata loads lazily, only for the resolved command when help is
needed.
The fast path joins argument prefixes with hyphens and checks for an executable file: `omarchy theme set foo` probes `omarchy-theme-set-foo`, then `omarchy-theme-set`, which exists. Filename resolution reads no metadata headers. Before executing the resolved command, dispatch reads that command's header to check required arguments and detect mistyped subcommands under group parents. Normal dispatch avoids parsing the headers of several hundred binaries, which adds measurable latency (`omarchy dev benchmark cli` tracks it); filename probes use a few stat calls.

When no filename matches — metadata-moved routes like `omarchy share`, and aliases like `omarchy screenshot` — the router first loads only the commands that could own a route under the first word: those whose filename starts with it, and those whose header names it as their group or in an alias. If that resolves a route that runs its command, it dispatches from there, about as fast as a filename route. Help output, a route that needs arguments it wasn't given, and anything still unresolved fall back to loading all metadata and resolving against the full route table, with the same longest-prefix rule, since help lists related commands from the whole table.

Expand All @@ -76,6 +69,8 @@ none were given, the router shows help instead of executing — `omarchy theme
set` prints usage rather than running an interactive setter. A bare group name
with child commands shows the group help.

A group parent whose `args` metadata lists only flag tokens, or nothing, refuses a leftover bare word as a mistyped subcommand and exits 127, so `omarchy update firmwre` reports an unknown command instead of starting an update; a flag with a separate value placeholder such as `[--agent <id>]` counts as an operand and turns the check off, and a `--` before the first bare word passes the words after it through to the command.

Dispatch is `exec`: the router process is replaced, the binary sees only the leftover arguments, and the exit code is the binary's own. The router itself exits 127 for unknown routes or missing binaries, and 1 for a bare command missing its required arguments, after showing its help. A bare group showing its help, and an explicit `--help`, exit 0.

When nothing resolves, the router tries a prefix listing — `omarchy hw asus`
Expand Down
67 changes: 67 additions & 0 deletions test/cli
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ assert_output_contains "body metadata command dispatches by filename" "$output"
{
printf '#!/bin/bash\n\n'
printf '# omarchy:summary=Partial resolution help test parent\n'
printf '# omarchy:args=<word> [args...]\n'
printf '# omarchy:alias=omarchy parenthelp-alias\n'
printf 'echo "parenthelp-parent-ran args=[$*]"\n'
} >"$TMPDIR/omarchy-parenthelp"
Expand Down Expand Up @@ -850,6 +851,72 @@ assert_output_contains "a --help behind -- belongs to the command and still forw
output=$("$TMPDIR/omarchy" parenthelp --helpme x--help)
assert_output_contains "help lookalike tokens do not trigger help" "$output" "parenthelp-parent-ran args=[--helpme x--help]"

# A group parent documents what an operand looks like. When it documents none,
# a bare word left over after it is a mistyped subcommand and must not run the
# parent: `omarchy update firmwre` used to start a full update, ignore the typo
# and report the full run instead of what was asked for.
{
printf '#!/bin/bash\n\n'
printf '# omarchy:summary=Unknown subcommand test parent\n'
printf '# omarchy:args=[-y]\n'
printf '# omarchy:alias=omarchy subcommandparent-alias\n'
printf 'echo "subcommand-parent-ran args=[$*]"\n'
} >"$TMPDIR/omarchy-subcommandparent"
{
printf '#!/bin/bash\n\n'
printf '# omarchy:summary=Unknown subcommand test child\n'
printf 'echo subcommand-child-ran\n'
} >"$TMPDIR/omarchy-subcommandparent-child"
{
printf '#!/bin/bash\n\n'
printf '# omarchy:summary=Unknown subcommand test parent without arguments\n'
printf 'echo "noargs-parent-ran args=[$*]"\n'
} >"$TMPDIR/omarchy-noargsparent"
{
printf '#!/bin/bash\n\n'
printf '# omarchy:summary=Unknown subcommand test child without arguments\n'
printf 'echo noargs-child-ran\n'
} >"$TMPDIR/omarchy-noargsparent-child"
chmod +x "$TMPDIR"/omarchy-subcommandparent* "$TMPDIR"/omarchy-noargsparent*

status=0
stderr=$("$TMPDIR/omarchy" subcommandparent typo 2>&1 >/dev/null) || status=$?
(( status == 127 )) || fail "a mistyped subcommand exits 127 (got $status)"
[[ $stderr == *"Unknown Omarchy command: omarchy subcommandparent typo"* ]] ||
fail "a mistyped subcommand names the whole route"
[[ $stderr == *"omarchy subcommandparent --help"* ]] ||
fail "a mistyped subcommand points at the group's help"
pass "a mistyped subcommand is rejected by its name"

output=$("$TMPDIR/omarchy" subcommandparent typo 2>/dev/null) || true
[[ -z $output ]] || fail "a mistyped subcommand does not run the parent"
pass "a mistyped subcommand does not run the parent"

output=$("$TMPDIR/omarchy" noargsparent typo 2>/dev/null) || true
[[ -z $output ]] || fail "a parent without documented arguments rejects a bare word too"
pass "a parent without documented arguments rejects a bare word too"

status=0
stderr=$("$TMPDIR/omarchy" subcommandparent -y typo 2>&1 >/dev/null) || status=$?
(( status == 127 )) || fail "a bare word after a flag is still a mistyped subcommand"
[[ $stderr == *"omarchy subcommandparent typo"* ]] || fail "a bare word after a flag is named in the error"
pass "a bare word after a flag is still a mistyped subcommand"

status=0
output=$("$TMPDIR/omarchy" subcommandparent-alias typo 2>&1) || status=$?
(( status == 127 )) && [[ $output != *"subcommand-parent-ran"* ]] ||
fail "a mistyped subcommand behind an alias does not run the parent (status $status): $output"
pass "a mistyped subcommand behind an alias does not run the parent"

output=$("$TMPDIR/omarchy" subcommandparent child)
assert_output_contains "a documented child still resolves" "$output" "subcommand-child-ran"

output=$("$TMPDIR/omarchy" subcommandparent -y)
assert_output_contains "a flags-only parent still takes its flags" "$output" "subcommand-parent-ran args=[-y]"

output=$("$TMPDIR/omarchy" subcommandparent -- literal)
assert_output_contains "a -- closes the option list before the bare word check" "$output" "subcommand-parent-ran args=[-- literal]"

# Alias and metadata-moved routes resolve by loading only the commands that can
# own a route under their first word, then dispatch exactly like a full load.
{
Expand Down
Loading