fix: respect --json flag on --help for root and groups#373
Merged
Conversation
Commander's help formatter was only configured for leaf commands, and the leaf-level JSON detection used thisCmd.parent?.optsWithGlobals(), which returns undefined for the root program. As a result, `elastic --help --json` and `elastic <group> --help --json` printed plain text instead of JSON. Add a unified hasGlobalJsonFlag() helper that walks to the root, a formatHelpAsJson() serialiser, and an exported configureJsonHelp() that hooks into Commander's help formatter. Apply it to the root program and to every group built via defineGroup. Existing leaf-command behaviour (input JSON Schema for commands with an input schema, empty otherwise) is preserved. Add regression tests covering root --help --json, flag order, the negative case (text help when --json is absent), and group --help --json.
Contributor
✅MegaLinter analysis: Success
Notices📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining See detailed reports in MegaLinter artifacts MegaLinter is graciously provided by OX Security |
CI flagged branch coverage at 89.99% (threshold 90%) and a stale NOTICE.txt left over from the yaml@2.9.0 bump in main. - Add focused unit tests for `configureJsonHelp` covering the JSON/text paths, mandatory options, string/number/boolean defaults, hidden options and commands, sub-command aliases, the auto-added `help` command filter, and the group-via-parent-walk case. - Drop dead `?? ''` fallbacks on Commander's `description` (it's always a string) and a redundant `dv !== undefined` short-circuit in `formatHelpAsJson` to keep branch coverage above threshold. - Regenerate NOTICE.txt so the yaml entry matches the installed version.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
elastic --help --json(andelastic <group> --help --json) was printing plain text help instead of respecting the--jsonflag. Only leaf commands ran their help through the JSON-aware formatter, and even there the detection usedthisCmd.parent?.optsWithGlobals(), which returnsundefinedfor the root program.What changed
src/factory.ts:hasGlobalJsonFlag(cmd)walks up to the root and reads its--jsonopt, so detection works uniformly for root, groups, and leaves.formatHelpAsJson(cmd)serialises name, description, usage, visible options, and visible sub-commands.configureJsonHelp(cmd)hooks Commander's help formatter and falls back to text help when--jsonis not set.defineGroupnow callsconfigureJsonHelpso every group inherits the behaviour.src/cli.tscallsconfigureJsonHelp(program)on the root program.inputZod schema continue to emit that schema; commands without one still emit empty output (existing contract covered bytest/factory.test.ts).Tests
Added a new
elastic CLI -- --help --jsonsuite intest/cli.test.ts:elastic --help --jsonreturns structured JSON withname,options, andcommands.--json --help) also works.elastic --help(no--json) still emits plain text.elastic sanitize --help --jsonreturns JSON for a group.Updated the export-surface assertion in
test/factory.test.tsto include the newconfigureJsonHelpexport.Test plan
npm run buildnode --import tsx/esm --test test/cli.test.ts(30 pass, including 4 new)node --import tsx/esm --test test/factory.test.ts(268 pass)npm run test:lintelastic --help --json→ JSONelastic --json --help→ JSONelastic --help→ text (unchanged)elastic sanitize --help --json→ JSONelastic cli-schema --help --json→ empty (unchanged leaf-without-input-schema contract)