fix: Handle repeated CLI flags instead of silently dropping values#126
Merged
Conversation
) Repeated string flags now accumulate with comma separation (e.g. --index a --index b -> "a,b"). Repeated non-string flags (number, enum, object, array) error with a clear message. Boolean flags remain idempotent. Uses Commander's getOptionValueSource() to detect repetition at parse time, requiring no changes to OptionDefinition, ParsedResult, or handler code.
JoshMock
approved these changes
Apr 13, 2026
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
Fixes #101. Repeated CLI flags are now handled explicitly instead of silently keeping only the last value.
--index a --index bproduces"a,b"--count 5 --count 10produces"option --count cannot be specified more than once"Applies to both
OptionDefinitionoptions and schema-derived options. No changes toOptionDefinition,SchemaArgDefinition,ParsedResult, or any handler code.Implementation
Two new helpers in
factory.ts(~30 lines), using Commander's built-ingetOptionValueSource()API insideparseArgcallbacks:stringAccumulator(cmd, attrName): on first CLI occurrence returns the value as-is; on subsequent occurrences comma-joins with the previous valuesingleValueGuard(cmd, attrName, flagDisplay, innerParse?): errors viacmd.error()on repetition, otherwise delegates to the inner parserDesign decisions for reviewer awareness
All string options accumulate, not just "known multi-value" ones. We cannot distinguish
z.string()fields that accept comma-separated values (an Elasticsearch convention) from those that don't, since both are typed asstring. Accumulating all strings is safe: Zod validation still runs, and if the ES API doesn't support multi-value, Elasticsearch itself returns a clear error. The key point is no data is silently lost, which was the original bug. If this is too permissive, an alternative is an explicitrepeatable: truefield onOptionDefinition/SchemaArgDefinition, but that adds config surface and requires every command author to opt in.Comma is the separator, not a configurable delimiter. Elasticsearch universally uses commas for multi-value path/query parameters (indices, fields, node IDs). A configurable separator would add complexity with no clear use case.
--input-fileis not guarded. It is registered without aparseArgcallback. Commander already errors if the file doesn't exist, so double-specification is unlikely to be a real problem. Can add a guard as a follow-up.Global options (
--json,--config-file,--use-context) are not guarded. They are registered on the root program incli.ts, not through the factory. Guarding them would be a small follow-up change incli.ts.Test plan
describe('repeated flags')covering:npm run buildclean--index idx-a --index idx-bproduces"idx-a,idx-b"via--dry-run