diff --git a/party b/party index 9bb95b3..069c73e 100755 --- a/party +++ b/party @@ -1090,10 +1090,7 @@ Usage: party log [--stop] [--party ] EOF exit 0 ;; -*) echo "party log: unknown flag '$1'" >&2; exit 2 ;; - *) - if [ -z "$partyname" ]; then partyname="$1"; shift - else echo "party log: unexpected arg '$1'" >&2; exit 2; fi - ;; + *) echo "party log: unexpected arg '$1'" >&2; exit 2 ;; esac done @@ -1277,10 +1274,22 @@ EOF # the socket path. exec "$PARTY_TMUX" -S "$RR_SOCKET" "$@" } -cmd_leave() { + +# Argv check for the subcommands that take no arguments. $1 = label. +# A stray word is more likely a typo'd subcommand line than something +# to ignore, so it is a usage error like everywhere else. +_parse_no_args() { + label="$1"; shift case "${1:-}" in - -h|--help) echo "Usage: party leave"; exit 0 ;; + '') ;; + -h|--help) echo "Usage: party $label"; exit 0 ;; + -*) echo "party $label: unknown flag '$1'" >&2; exit 2 ;; + *) echo "party $label: unexpected arg '$1'" >&2; exit 2 ;; esac +} + +cmd_leave() { + _parse_no_args leave "$@" # Walk every live party; for each one we have any presence on # (either via __party_guest_$USER or as a client of the host's @@ -1520,6 +1529,7 @@ cmd_detach() { } cmd_list() { + _parse_no_args list "$@" found=0 lines='' first_group='' mixed=0 for rec in $(roster_list); do roster_read "$rec" || continue @@ -1583,7 +1593,10 @@ cmd_who() { echo "Usage: party who [--short] [--party name|--socket path]" exit 0 ;; -*) echo "party who: unknown flag '$1'" >&2; exit 2 ;; - *) partyname="$1"; shift ;; + *) + if [ -z "$partyname" ]; then partyname="$1"; shift + else echo "party who: unexpected arg '$1'" >&2; exit 2; fi + ;; esac done @@ -1636,9 +1649,7 @@ cmd_who() { } cmd_status() { - case "${1:-}" in - -h|--help) echo "Usage: party status"; exit 0 ;; - esac + _parse_no_args status "$@" hosting='' joined='' for rec in $(roster_list); do diff --git a/tests/10-skeleton.bats b/tests/10-skeleton.bats index 51493ed..24e4a35 100644 --- a/tests/10-skeleton.bats +++ b/tests/10-skeleton.bats @@ -31,3 +31,25 @@ teardown() { teardown_party_sandbox; } # assertion is the test. [ -n "${PARTY_VERSION:-}" ] } + +@test "no-argument subcommands print help and reject stray args" { + for sub in list leave status; do + run "$PARTY_BIN" "$sub" --help + [ "$status" -eq 0 ] + [[ "$output" == "Usage: party $sub" ]] + run "$PARTY_BIN" "$sub" stray + [ "$status" -eq 2 ] + [[ "$output" == *"unexpected arg 'stray'"* ]] + run "$PARTY_BIN" "$sub" --bogus + [ "$status" -eq 2 ] + done +} + +@test "who rejects a second positional, log rejects any" { + run "$PARTY_BIN" who one two + [ "$status" -eq 2 ] + [[ "$output" == *"unexpected arg 'two'"* ]] + run "$PARTY_BIN" log one + [ "$status" -eq 2 ] + [[ "$output" == *"unexpected arg 'one'"* ]] +}