Skip to content

fix(api): read --name value like --name=value (#68) - #73

Merged
neoromantic merged 7 commits into
mainfrom
fix/api-space-flags-68
Oct 22, 2026
Merged

neoromantic merged 7 commits into
mainfrom
fix/api-space-flags-68

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Fixes #68

Problem

citty reads an undeclared --name without = as true and leaves the value as a stray positional. So tg api account.checkUsername --username myuser, the form every docs example uses, sent username: true and failed with TELEGRAM_ERROR (exit 5). Only --username=myuser and --json worked. A negative number was worse: --addOffset -10 became addOffset: true plus junk keys named after the digits.

Every request also carried r: false, and --json / --account calls carried j / a: citty copies those options to their one-letter aliases. tg's global options reach the api subcommand undeclared, so -v, --verbose, -f, --format, -q and --quiet went into the request too. mtcute ignores unknown keys, so live calls were fine, but the same call got a different replay fixture depending on how it was written.

Fix

  • fix(api): keep the -j, -a and -r aliases out of the request: the aliases join the list of tg's own arguments that never reach Telegram.
  • fix(api): read --name value like --name=value: the new spacedFlagValues in src/utils/args.ts pairs a --name that citty read as true with the argument after it. It does this only after the method, and only when that argument is a value or a negative number. The value then goes through the same typing as --name=value. Keys that start with a digit are dropped. Everything else parses as before: --name=value, --no-name, --account/-a, --json/-j, -r, a flag followed by another option or at the end (still true), a value such as -hi (still needs =), and anything after --.
  • fix(api): keep tg's global options out of the request: format/f, verbose/v and quiet/q join the same list. Without it, the new pairing would even send format: 'pretty' for --format pretty.
  • docs/api-design.md ("Argument Merging") now covers both forms, flags, negative numbers and values that start with -. It also lists the option names tg reads itself and keeps out of the request, and where they go: after the method (--json, --account and --raw also between api and the method, the global options also before api; -f/--format between api and the method has its value taken as the method). A method argument with one of those names goes in --json (tg api contacts.search --json '{"q":"alice","limit":5}'). tg api --help mentions both forms.

The README's --username=myuser and the --username myuser example in docs/cli-commands.md both work now.

Behaviour change: a method argument that shares a name with a tg option now has to go in --json. q (contacts.search, messages.search, messages.searchGlobal, channelParticipantsSearch) is the one that worked before: tg api contacts.search --q=alice --limit=5 --raw sent q, and now --q=… no longer reaches Telegram (and root's -q still makes the failed call silent). format (account.getThemes) is also dropped, but --format=<theme format> always failed root's enum check.

Replay fixtures: the alias keys (j, a, r) and global options are gone, so replay fixture hashes for tg api calls change. API fixtures recorded for tg api calls need recording again. No fixtures are committed.

Tests

  • New src/__e2e__/api-args.e2e.test.ts: each test records a replay fixture for an exact request with a stub client, then runs tg api with TELEGRAM_API_REPLAY=1. The fixture name is a hash of the request, so any extra key misses it.
    • account.checkUsername with --username myuser, --username=myuser and --json all print the same success JSON, as do -v, --verbose, -f json and --format json next to --username myuser (the envelope is read from where the JSON starts: under -v mtcute's debug log prints to stdout first, see follow-up 7; the replay runs unset MTCUTE_LOG_LEVEL and VERBOSE); -q and --quiet exit 0 with no output. --username otheruser misses the fixture (exit 5, TELEGRAM_ERROR).
    • contacts.getTopPeers with --correspondents first, last, and with --account 1 / -a 1 before or after the method.
    • messages.getHistory with --peer._, --addOffset -10, --limit 10 and --json in both forms.
    • contacts.getLocated with --geoPoint.lat -1.5 in both forms: citty reads -1.5 as the short options 1, . and 5, and none of them may reach the request.
  • New src/__tests__/args.test.ts: spacedFlagValues on citty's own parse of each argv. The -- case names the flag on both sides of --, so it fails if the helper reads past --.
  • Mutations checked: removing the -- break fails the -- unit test; narrowing the key filter to /^\d/ fails the -1.5 e2e case; without the global option names, all six new account.checkUsername cases miss their fixture.
  • Red before green: on main every account.checkUsername case failed with exit 5. After the first commit the space form still failed. After the second, everything passes.
  • Rebased onto main after fix(api): close the client on every path so tg api --raw exits #71. bun run check and bun run test (1738 pass) are green locally; bun run test:e2e has 271 pass, and its only failures are the network-bound "should try to fetch from API" cases in commands.e2e.test.ts, which time out the same way on main on this machine.

Follow-ups (not in this PR)

  1. Root options placed after the method go to tg itself: --q/-q make the call silent, and --format <x> with a value outside json/pretty/quiet fails citty's enum check with usage and exit 1. Placements that fail: tg api -f pretty <method> / tg api --format json <method> take the format value as the method (INVALID_ARGS), and tg -a 1 api … / tg -j '{}' api … fail with "Unknown command" because root doesn't declare -a/-j as value options. The docs now describe these.
  2. Broken docs examples: --peer @username (peer must be a TL object), contacts.getContacts without hash, --peer.username objects without _, and sendMessage without randomId. They appear in docs/api-design.md, docs/cli-commands.md and the api.ts header comment.
  3. Stray positionals and non-numeric -x values are still silently ignored or misparsed. They could become INVALID_ARGS.
  4. Invalid --json is TELEGRAM_ERROR (exit 5) instead of INVALID_ARGS.
  5. Long fields given as numbers (--hash 123) are written as 0 by mtcute's long writer, and parseInt loses precision above 2^53.
  6. --json '{"_":…}' can override the method.
  7. tg -v (or MTCUTE_LOG_LEVEL) sends mtcute's debug log to stdout (src/services/telegram.ts, log level 5), so the JSON output no longer parses.

🤖 Generated with Claude Code

neoromantic and others added 7 commits October 22, 2026 16:56
citty copies --json, --account and --raw to their one-letter aliases, and
tg api passed every key it didn't know to Telegram. Every request carried
r: false, and --json or --account calls also carried j or a. The same call
therefore got a different replay fixture depending on how it was written.
mtcute ignored these keys, so live calls were unaffected.

The new e2e test records a fixture for the exact request with a stub
client and checks that --username=myuser and --json find it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
citty read an undeclared --name without = as true and dropped the value
as a stray positional. The form used in every docs example
(tg api account.checkUsername --username myuser) therefore sent
username: true and failed with TELEGRAM_ERROR, and only --name=value
worked. A negative number such as --addOffset -10 was even read as short
flags named after its digits, which put true in the request and junk keys
next to it.

tg api now pairs a --name that citty read as true with the argument after
it, when that argument is a value or a negative number and comes after the
method. Both forms then go through the same typing, so they build the
same call. Keys that start with a digit are dropped. docs/api-design.md
says which forms work and which names need --json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The old argv had no --username before --, so citty never read the name
as true and the test passed with the break removed. Naming the flag on
both sides of -- leaves the break as the only thing that stops the
pairing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
citty reads `--geoPoint.lat -1.5` as the short options 1, . and 5. The
`.` in the key filter exists for that `.` key, but the only full-CLI
case used `-10`, which gives digit keys only, so narrowing the filter
to digits left every test green while the request gained `'': { '':
true }`. The new case catches that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The api subcommand gets --format/-f, --verbose/-v and --quiet/-q
undeclared, so they went into the request as method arguments: adding
-v to a call missed the replay fixture recorded without it, the same
problem that dropping j, a and r fixed. With the new --name value
pairing, --format pretty even sent format: 'pretty'. The docs already
say a method argument with one of these names goes in --json, so tg
now drops them like its own api options.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since tg api closes its client before it prints, -v and --verbose turn on
mtcute's debug log, and its "client destroyed" lines go to stdout before
the JSON. The -v and --verbose cases found their fixture and exited 0,
but result.json was undefined, so they failed on every run. The cases now
parse the envelope from where the JSON starts, and the replay runs unset
MTCUTE_LOG_LEVEL and VERBOSE so a developer's shell can't add the log to
the other cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The docs said tg reads its own options wherever they appear. Two
placements fail: -f/--format between api and the method has its value
taken as the method (INVALID_ARGS), and --account, --json and --raw
before api are not tg api's options at all. The docs now say to put them
after the method, list the other places that work, and name the method
arguments (q, format) that have to go in --json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the fix/api-space-flags-68 branch from dcc5b38 to 163ac91 Compare October 22, 2026 13:02
@neoromantic
neoromantic merged commit 4144f47 into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/api-space-flags-68 branch October 22, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tg api takes a space-separated flag value as true

1 participant