-
Notifications
You must be signed in to change notification settings - Fork 0
fix(cli): make --help work on every subcommand #135
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| "@taskless/cli": patch | ||
| --- | ||
|
|
||
| Make `--help` work on every command, instead of running the command. | ||
|
|
||
| `taskless check --help` printed no usage — it ran `check`. So did every other | ||
| subcommand: `--help` was parsed as an unrecognized flag and the command body | ||
| executed anyway, which meant asking `init` how it works installed skills, and | ||
| asking `check` how it works migrated the `.taskless/` scaffold. The only place | ||
| help worked was the bare `taskless --help`, whose own output tells you to run | ||
| `taskless <command> --help`. | ||
|
|
||
| `--help` and `-h` are now recognized at every depth, including nested commands | ||
| (`taskless auth login --help` describes `login`, not `auth`), and a working | ||
| directory passed before the command (`taskless -d ./repo check --help`) no | ||
| longer confuses which command you asked about. The usage text itself is | ||
| unchanged, and nothing else about how commands run has changed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * Raw-argv scanning shared by everything that has to answer "which tokens are | ||
| * positionals?" — subcommand resolution for `--help`, the root command's | ||
| * no-subcommand check, `check`'s path arguments, and telemetry's command name. | ||
| * One copy, because the rules are subtle: | ||
| * | ||
| * - `-d`/`--dir` take a value, so the token after one of them is a flag value | ||
| * and NOT a positional (`taskless -d /tmp check` runs `check`, not `/tmp`). | ||
| * - `--` is the POSIX end-of-options marker: every token after it is a | ||
| * positional even if it starts with `-`, which is what lets `taskless check | ||
| * -- -h` scan a path literally named `-h` instead of asking for help. | ||
| */ | ||
|
|
||
| /** Flags every command takes a value for. */ | ||
| export const DIR_FLAGS = new Set(["-d", "--dir"]); | ||
|
|
||
| /** The POSIX end-of-options marker. */ | ||
| const END_OF_OPTIONS = "--"; | ||
|
|
||
| export interface SplitArguments { | ||
| /** | ||
| * Tokens that are not flags and not the value of a value-taking flag, plus | ||
| * everything after `--`. | ||
| */ | ||
| positionals: string[]; | ||
| /** | ||
| * Option tokens, in order, including the `--` marker itself. Values consumed | ||
| * by a value-taking flag are not included. | ||
| */ | ||
| flags: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * Split raw argv into positionals and flags, skipping flag values and honoring | ||
| * `--`. `valueFlags` names flags beyond `-d`/`--dir` that consume the next | ||
| * token (e.g. `check`'s `--timeout`). | ||
| */ | ||
| export function splitRawArguments( | ||
| rawArguments: string[], | ||
| valueFlags: readonly string[] = [] | ||
| ): SplitArguments { | ||
| const consumesValue = | ||
| valueFlags.length > 0 ? new Set([...DIR_FLAGS, ...valueFlags]) : DIR_FLAGS; | ||
| const positionals: string[] = []; | ||
| const flags: string[] = []; | ||
| for (let index = 0; index < rawArguments.length; index++) { | ||
| const argument = rawArguments[index]!; | ||
| if (argument === END_OF_OPTIONS) { | ||
| flags.push(argument); | ||
| positionals.push(...rawArguments.slice(index + 1)); | ||
| break; | ||
| } | ||
| if (argument.startsWith("-")) { | ||
| flags.push(argument); | ||
| // `--dir=<path>` carries its own value; `-d <path>` eats the next token — | ||
| // unless that token is `--`, which ends the options rather than being one. | ||
| if ( | ||
| consumesValue.has(argument) && | ||
| rawArguments[index + 1] !== undefined && | ||
| rawArguments[index + 1] !== END_OF_OPTIONS | ||
| ) { | ||
| index++; | ||
| } | ||
| continue; | ||
| } | ||
| positionals.push(argument); | ||
| } | ||
| return { positionals, flags }; | ||
| } | ||
|
|
||
| /** | ||
| * True when argv asks for help. Scanned through `splitRawArguments` so neither | ||
| * a flag value nor a path after `--` that happens to read like `-h` is mistaken | ||
| * for a help request. | ||
| */ | ||
| export function hasHelpFlag(rawArguments: string[]): boolean { | ||
| const { flags } = splitRawArguments(rawArguments); | ||
| return flags.includes("--help") || flags.includes("-h"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { showUsage } from "citty"; | ||
| import type { ArgsDef, CommandDef, Resolvable } from "citty"; | ||
|
|
||
| import { splitRawArguments } from "./argv"; | ||
|
|
||
| /** | ||
| * citty only implements `--help` inside `runMain`, and `runMain`'s help path | ||
| * calls `process.exit(0)` — which skips `finally` blocks, including the one in | ||
| * `src/index.ts` that emits the `cli_run` telemetry denominator. So the CLI | ||
| * keeps `runCommand` and resolves help itself, here, using citty's own | ||
| * exported `showUsage` so no usage text is hand-written. | ||
| * | ||
| * citty's `resolveSubCommand` is not exported, and its own walk would resolve | ||
| * `taskless -d /tmp check --help` to `/tmp` (it takes the first non-`-` token), | ||
| * so the walk below uses the shared argv scanner instead. | ||
| */ | ||
|
|
||
| /** Resolve a citty `Resolvable<T>` (value, promise, or factory) to its value. */ | ||
| async function resolveValue<T>( | ||
| value: Resolvable<T> | undefined | ||
| ): Promise<T | undefined> { | ||
| return typeof value === "function" | ||
| ? await (value as () => T | Promise<T>)() | ||
| : await value; | ||
| } | ||
|
|
||
| /** | ||
| * Render usage for the deepest command the positionals resolve to, with its | ||
| * parent (so nested commands like `auth login` print their own usage under | ||
| * their own name). Returns normally — never exits — so the caller's telemetry | ||
| * `finally` still runs. | ||
| */ | ||
| export async function showResolvedUsage<T extends ArgsDef = ArgsDef>( | ||
| root: CommandDef<T>, | ||
| rawArguments: string[] | ||
| ): Promise<void> { | ||
| // Widen once: the walk descends into subcommands, each with its own args | ||
| // shape, and only the rendering (which reads meta and args generically) is | ||
| // done with the result. | ||
| let command = root as unknown as CommandDef; | ||
| let parent: CommandDef | undefined; | ||
|
|
||
| for (const name of splitRawArguments(rawArguments).positionals) { | ||
| const subCommands = await resolveValue(command.subCommands); | ||
| const child = subCommands | ||
| ? await resolveValue(subCommands[name]) | ||
| : undefined; | ||
| // The first token that is not a child of the current command ends the | ||
| // walk (it is a positional argument, or a typo) — render what resolved. | ||
| if (!child) break; | ||
| parent = command; | ||
| command = child; | ||
| } | ||
|
|
||
| await showUsage(command, parent); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.