Skip to content

[build-script] Introduce "concat" argument action #2884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ details of the setups of other systems or automated environments.""")
help="Pass through extra options to CMake in the form of comma "
"separated options '-DCMAKE_VAR1=YES,-DCMAKE_VAR2=/tmp'. Can be "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the comment still accurate? It seems like the list is space-separated, or rather, shell-script-style.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so accurate:(
Accept both comma and space.

"called multiple times to add multiple such options.",
action="append",
action=arguments.action.concat,
type=arguments.type.shell_split,
default=[])

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

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

build_script_impl_args += args.build_script_impl_args
Expand Down
13 changes: 13 additions & 0 deletions utils/swift_build_support/swift_build_support/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,16 @@ def __call__(self, parser, namespace, values, option_string=None):
parser.error('unknown argument: %s' % arg)

_register(action, 'unavailable', _UnavailableAction)


class _ConcatAction(argparse.Action):

def __call__(self, parser, namespace, values, option_string=None):
old_val = getattr(namespace, self.dest)
if old_val is None:
val = values
else:
val = old_val + values
setattr(namespace, self.dest, val)

_register(action, 'concat', _ConcatAction)
41 changes: 41 additions & 0 deletions utils/swift_build_support/tests/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,44 @@ def test_unavailable(self):
self.assertIn('--never-ever', stderr.getvalue())

sys.stderr = orig_stderr

def test_concat(self):
# Has default
parser = argparse.ArgumentParser()
parser.add_argument(
"--str-opt",
action=argaction.concat,
default="def")
parser.add_argument(
"--list-opt",
action=argaction.concat,
type=argtype.shell_split,
default=["def"])

self.assertEqual(
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
argparse.Namespace(str_opt="def1242", list_opt=["def"]))

self.assertEqual(
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
argparse.Namespace(
str_opt="def", list_opt=["def", "foo", "12", "bar", "42"]))

# Default less
parser = argparse.ArgumentParser()
parser.add_argument(
"--str-opt",
action=argaction.concat)
parser.add_argument(
"--list-opt",
action=argaction.concat,
type=argtype.shell_split)

self.assertEqual(
parser.parse_args(['--str-opt', '12', '--str-opt=42']),
argparse.Namespace(str_opt="1242", list_opt=None))

self.assertEqual(
parser.parse_args(['--list-opt', 'foo 12', '--list-opt=bar 42']),
argparse.Namespace(
str_opt=None, list_opt=["foo", "12", "bar", "42"]))