Skip to content

Commit 0d6e1b1

Browse files
committed
[build-script] Introduce "concat" argument action
Get rid of unnecessary list of list argument.
1 parent 830a815 commit 0d6e1b1

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

utils/build-script

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ details of the setups of other systems or automated environments.""")
955955
help="Pass through extra options to CMake in the form of comma "
956956
"separated options '-DCMAKE_VAR1=YES,-DCMAKE_VAR2=/tmp'. Can be "
957957
"called multiple times to add multiple such options.",
958-
action="append",
958+
action=arguments.action.concat,
959959
type=arguments.type.shell_split,
960960
default=[])
961961

@@ -1436,13 +1436,10 @@ details of the setups of other systems or automated environments.""")
14361436

14371437
# If we have extra_cmake_options, combine all of them together and then add
14381438
# them as one command.
1439-
# Note that args.extra_cmake_options is a list of lists of options.
14401439
if args.extra_cmake_options:
14411440
build_script_impl_args += [
14421441
"--extra-cmake-options=%s" % ' '.join(
1443-
pipes.quote(opt)
1444-
for options in args.extra_cmake_options
1445-
for opt in options)
1442+
pipes.quote(opt) for opt in args.extra_cmake_options)
14461443
]
14471444

14481445
build_script_impl_args += args.build_script_impl_args

utils/swift_build_support/swift_build_support/arguments.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,16 @@ def __call__(self, parser, namespace, values, option_string=None):
128128
parser.error('unknown argument: %s' % arg)
129129

130130
_register(action, 'unavailable', _UnavailableAction)
131+
132+
133+
class _ConcatAction(argparse.Action):
134+
135+
def __call__(self, parser, namespace, values, option_string=None):
136+
old_val = getattr(namespace, self.dest)
137+
if old_val is None:
138+
val = values
139+
else:
140+
val = old_val + values
141+
setattr(namespace, self.dest, val)
142+
143+
_register(action, 'concat', _ConcatAction)

utils/swift_build_support/tests/test_arguments.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,44 @@ def test_unavailable(self):
134134
self.assertIn('--never-ever', stderr.getvalue())
135135

136136
sys.stderr = orig_stderr
137+
138+
def test_concat(self):
139+
# Has default
140+
parser = argparse.ArgumentParser()
141+
parser.add_argument(
142+
"--str-opt",
143+
action=argaction.concat,
144+
default="def")
145+
parser.add_argument(
146+
"--list-opt",
147+
action=argaction.concat,
148+
type=argtype.shell_split,
149+
default=["def"])
150+
151+
self.assertEqual(
152+
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
153+
argparse.Namespace(str_opt="def1242", list_opt=["def"]))
154+
155+
self.assertEqual(
156+
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
157+
argparse.Namespace(
158+
str_opt="def", list_opt=["def", "foo", "12", "bar", "42"]))
159+
160+
# Default less
161+
parser = argparse.ArgumentParser()
162+
parser.add_argument(
163+
"--str-opt",
164+
action=argaction.concat)
165+
parser.add_argument(
166+
"--list-opt",
167+
action=argaction.concat,
168+
type=argtype.shell_split)
169+
170+
self.assertEqual(
171+
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
172+
argparse.Namespace(str_opt="1242", list_opt=None))
173+
174+
self.assertEqual(
175+
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
176+
argparse.Namespace(
177+
str_opt=None, list_opt=["foo", "12", "bar", "42"]))

0 commit comments

Comments
 (0)