8000 Add argparse custom action refactoring comment · localstack/localstack@4301369 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4301369

Browse files
committed
Add argparse custom action refactoring comment
Requires more testing to ensure consistent behavior (e.g., with multiple arguments, empty arguments, and combinations).
1 parent 1de8682 commit 4301369

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

localstack/utils/container_utils/container_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ def parse_additional_flags(
956956
:return: A DockerRunFlags object that will return new objects if respective parameters were None and
957957
additional flags contained a flag for that object or the same which are passed otherwise.
958958
"""
959+
# Argparse refactoring opportunity: custom argparse actions can be used to modularize parsing (e.g., key=value)
960+
# https://docs.python.org/3/library/argparse.html#action
961+
959962
# Configure parser
960963
parser = NoExitArgumentParser(description="Docker run flags parser")
961964
parser.add_argument(

localstack/utils/no_exit_argument_parser.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
LOG = logging.getLogger(__name__)
66

77

8-
# Implements the `exit_on_error=False` behavior introduced in Python 3.9 to support older Python versions
9-
# and prevents further SystemExit for other error categories.
10-
# Limitations of error cases: https://stackoverflow.com/a/67891066/6875981
11-
# Subclassing workaround example: https://stackoverflow.com/a/59072378/6875981
128
class NoExitArgumentParser(argparse.ArgumentParser):
9+
"""Implements the `exit_on_error=False` behavior introduced in Python 3.9 to support older Python versions
10+
and prevents further SystemExit for other error categories.
11+
* Limitations of error categories: https://stackoverflow.com/a/67891066/6875981
12+
* ArgumentParser subclassing example: https://stackoverflow.com/a/59072378/6875981
13+
"""
14+
1315
def exit(self, status: int = ..., message: Optional[str] = ...) -> NoReturn:
1416
LOG.warning(f"Error in argument parser but preventing exit: {message}")
1517

tests/unit/test_dockerclient.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ def test_labels(self):
168168
argument_string = r'--label ""' # assert that we gracefully handle invalid labels
169169
flags = Util.parse_additional_flags(argument_string)
170170
assert flags.labels == {}
171+
argument_string = r"--label =bar" # assert that we ignore empty labels
172+
flags = Util.parse_additional_flags(argument_string)
173+
assert flags.labels == {}
171174

172175
def test_network(self):
173176
argument_string = r'-v "/tmp/test.jar:/tmp/foo bar/test.jar" --network mynet123'

0 commit comments

Comments
 (0)
0