From 5db5bf990dd2f00fde2eceba7bb62d9c42cf7062 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 3 Sep 2026 01:31:03 +0100 Subject: [PATCH] fix: extend action should spread a scalar string by code point --- lib/argparse.js | 2 +- test/test_argparse.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/argparse.js b/lib/argparse.js index 87b2f31..ab99ecf 100644 --- a/lib/argparse.js +++ b/lib/argparse.js @@ -1903,7 +1903,7 @@ const _ExtendAction = _callable(class _ExtendAction extends _AppendAction { call (parser, namespace, values/*, option_string = undefined */) { let items = getattr(namespace, this.dest, undefined) items = _copy_items(items) - items = items.concat(values) + items = items.concat(typeof values === 'string' ? Array.from(values) : values) setattr(namespace, this.dest, items) } }) diff --git a/test/test_argparse.js b/test/test_argparse.js index 82127bd..c3fddc6 100644 --- a/test/test_argparse.js +++ b/test/test_argparse.js @@ -2468,6 +2468,19 @@ class WFile { ] }).run() +;(new class TestActionExtendDefaultNargs extends ParserTestCase { + /* Without nargs, a single value is a string, and extend() spreads it + * character by character, same as Python's list.extend() on a str */ + + argument_signatures = [ + Sig('--foo', { action: 'extend' }), + ] + failures = [] + successes = [ + ['--foo abc', NS({ foo: ['a', 'b', 'c'] })], + ] +}).run() + ;(new class TestNegativeNumber extends ParserTestCase { /* Test parsing negative numbers */