Skip to content

Move legacy argparser #11872

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 4 commits into from
Sep 15, 2017
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
2 changes: 0 additions & 2 deletions utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ class BuildScriptInvocation(object):

@staticmethod
def apply_default_arguments(toolchain, args):
driver_arguments.apply_default_arguments(args)

# infer if ninja is required
ninja_required = (
args.cmake_generator == 'Ninja' or args.build_foundation)
Expand Down
12 changes: 12 additions & 0 deletions utils/build_swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# build_swift

The `build_swift` module contains data-structures and functions used by
the Swift build-script.

## Unit Tests

You may run the unit test suite using the command:

```sh
$ python -m unittest discover -s utils/build_swift
```
31 changes: 27 additions & 4 deletions utils/build_swift/driver_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,31 @@


__all__ = [
'apply_default_arguments',
'create_argument_parser',
]


def apply_default_arguments(args):
"""Preprocess argument namespace to apply default behaviors."""
class _ApplyDefaultsArgumentParser(argparse.ArgumentParser):
"""Wrapper class around the default ArgumentParser that allows for
post-processing the parsed argument namespace to apply default argument
transformations.
"""

def __init__(self, apply_defaults=None, *args, **kwargs):
self._apply_defaults = apply_defaults
super(_ApplyDefaultsArgumentParser, self).__init__(*args, **kwargs)

def parse_known_args(self, args=None, namespace=None):
args, argv = super(_ApplyDefaultsArgumentParser, self)\
.parse_known_args(args, namespace)

self._apply_defaults(args)
return args, argv


def _apply_default_arguments(args):
"""Preprocess argument namespace to apply default behaviors.
"""

# Build cmark if any cmark-related options were specified.
if (args.cmark_build_variant is not None):
Expand Down Expand Up @@ -245,7 +263,10 @@ def apply_default_arguments(args):


def create_argument_parser():
parser = argparse.ArgumentParser(
"""Return a configured argument parser."""

parser = _ApplyDefaultsArgumentParser(
apply_defaults=_apply_default_arguments,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE,
description=DESCRIPTION,
Expand Down Expand Up @@ -1111,6 +1132,8 @@ def create_argument_parser():
return parser


# ----------------------------------------------------------------------------

USAGE = """
%(prog)s [-h | --help] [OPTION ...]
%(prog)s --preset=NAME [SUBSTITUTION ...]
Expand Down
7 changes: 7 additions & 0 deletions utils/build_swift/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Loading