Skip to content

Append values when an array option is specified more than once - #264

Open
neuecc wants to merge 1 commit into
masterfrom
feature/repeated-array-option
Open

neuecc wants to merge 1 commit into
masterfrom
feature/repeated-array-option

Conversation

@neuecc

@neuecc neuecc commented Nov 15, 2026

Copy link
Copy Markdown
Member

Closes #263. Follow-up to #81 and #239.

What changed

A T[] option can now be specified more than once. Each occurrence is parsed exactly as before (JSON / comma-separated / custom parser) and the values are appended in order. Previously the last occurrence silently won.

ConsoleApp.Run(args, (string[] p) => Console.WriteLine(string.Join(" | ", p)));
Input Before After
-p a ["a"] ["a"]
-p a -p b ["b"] ["a", "b"]
-p a,b -p c ["c"] ["a", "b", "c"]
-p [1,2] -p 3 [3] [1, 2, 3]
-p a --prop b (alias) ["b"] ["a", "b"]
not specified default / required error unchanged

How

The parse code itself (BuildParseMethod, TrySplitParse) is untouched. The emitter wraps the existing parse statement of a named array option with two lines:

var prev0 = arg0;
if (!TryIncrementIndex(ref i, commandArgs.Length) || !TrySplitParse(commandArgs[i], out arg0)) { ThrowArgumentParseFailed("p", commandArgs[i]); }
if (prev0 != null) arg0 = ConcatArray(prev0, arg0);
  • No extra allocation in the common case. The only default value an array parameter can have is null, so prev != null tells whether the option has already appeared. No List<T>, no extra flag; a combined array is allocated only when the option really is repeated.
  • Default values are kept. When the option is absent arg is not touched, so string[]? x = null stays null.
  • Consistent across all parse paths. Because the merge happens outside the parse statement, ISpanParsable<T> elements, JSON elements (classes, enums) and custom parsers (IArgumentParser<T[]>) all behave the same.

Scope

  • Applies to named options of single-dimensional T[].
  • Does not apply to params T[] (consumes all remaining arguments) and [Argument] T[] (positional).
  • Global options are not affected; AddGlobalOption<T> does not support arrays.
  • Non-array options keep the last-wins behavior.

Values containing commas

Comma-separated parsing per occurrence is kept for compatibility, so -p List=a,b is still split. No new attribute is added for this: a custom parser for T[] that does not split is enough, and since each occurrence is appended, repeating still works. The README "Array" section shows it:

[AttributeUsage(AttributeTargets.Parameter)]
public class NoSplitAttribute : Attribute, IArgumentParser<string[]>
{
    public static bool TryParse(ReadOnlySpan<char> s, out string[] result)
    {
        result = [s.ToString()];
        return true;
    }
}

// --p List=a,b --p X=1 -> ["List=a,b", "X=1"]
ConsoleApp.Run(args, ([NoSplit] string[] p) => { });

Notes for reviewers

  • Behavior change. Code that relied on last-wins for an array option (for example a wrapper script that appends an override at the end) now receives all the values.
  • ConcatArray returns the other array as is when one side is empty, and treats a null second array (JSON null) as empty.

Tests

  • ArrayParseTest gets 8 new tests: repeated / interleaved / comma mixed / JSON mixed / [] mixed / ignore-case mixed, aliases, null default kept, required error and parse failure in a later occurrence, JSON element types (class and enum arrays), custom parser, ConsoleApp.Create() with Add and Add<T>, and [Argument] T[] not being merged.
  • All 140 GeneratorTests pass. The 8 new tests fail without the src change.
  • NativeAotTests were not run locally.

🤖 Generated with Claude Code

`-p a -p b` is now bound to ["a", "b"] instead of only the last value.
Each occurrence is parsed as before (JSON / comma-separated / custom parser)
and appended in order. params T[] and [Argument] T[] are not affected.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@FairPlayer4

Copy link
Copy Markdown

LGTM,
Some documentation nits, that Opus found:

  1. [] no longer resets an array. --sx a,b --sx [] now yields [a, b] (previously []), so a later occurrence can't clear earlier values anymore. That's a consequence of the behavior change worth calling out in the release notes, not just via the ConcatArray note.
  2. Other collection types still use last-wins. List<T>, IEnumerable<T>, ImmutableArray<T> etc. go through the JSON path, so --xs ["a"] --xs ["b"] → ["b"]. Fine to keep the scope to T[], but please document it so users don't hit this inconsistency by surprise.

Minor/optional: --help gives no hint that an array option can be repeated.

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.

Bind repeated array options to all values (-p a -p b → ["a","b"]), follow-up to #239

2 participants