Skip to content

Commit b1de301

Browse files
authored
add flag to query non-recursive parser argument destinations (#318)
* separate nested parser decorators into group and non-group * add flag to query non-recursive parser argument destinations * add test coverage for new option * add word to dictionary
1 parent ef83d9f commit b1de301

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

colcon_core/argument_parser/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import traceback
55
import types
6+
import warnings
67

78
from colcon_core.logging import colcon_logger
89
from colcon_core.plugin_system import instantiate_extensions
@@ -108,11 +109,23 @@ def __init__(self, parser, **kwargs):
108109
"""
109110
assert '_parser' not in kwargs
110111
kwargs['_parser'] = parser
111-
assert '_nested_decorators' not in kwargs
112-
kwargs['_nested_decorators'] = []
112+
assert '_nested_decorators_' not in kwargs
113+
kwargs['_nested_decorators_'] = []
114+
assert '_group_decorators' not in kwargs
115+
kwargs['_group_decorators'] = []
116+
assert '_recursive_decorators' not in kwargs
117+
kwargs['_recursive_decorators'] = []
113118
for k, v in kwargs.items():
114119
self.__dict__[k] = v
115120

121+
@property
122+
def _nested_decorators(self):
123+
warnings.warn(
124+
'colcon_core.argument_parser.ArgumentParserDecorator.'
125+
'_nested_decorators is a private variable and has been '
126+
'deprecated', stacklevel=2)
127+
return self._nested_decorators_
128+
116129
def __getattr__(self, name):
117130
"""
118131
Get an attribute from this decorator if it exists or the decoree.
@@ -157,7 +170,8 @@ def add_argument_group(self, *args, **kwargs):
157170
"""
158171
group = self.__class__(
159172
self._parser.add_argument_group(*args, **kwargs))
160-
self._nested_decorators.append(group)
173+
self._nested_decorators_.append(group)
174+
self._group_decorators.append(group)
161175
return group
162176

163177
def add_mutually_exclusive_group(self, *args, **kwargs):
@@ -169,7 +183,8 @@ def add_mutually_exclusive_group(self, *args, **kwargs):
169183
"""
170184
group = self.__class__(
171185
self._parser.add_mutually_exclusive_group(*args, **kwargs))
172-
self._nested_decorators.append(group)
186+
self._nested_decorators_.append(group)
187+
self._group_decorators.append(group)
173188
return group
174189

175190
def add_parser(self, *args, **kwargs):
@@ -181,7 +196,8 @@ def add_parser(self, *args, **kwargs):
181196
"""
182197
parser = self.__class__(
183198
self._parser.add_parser(*args, **kwargs))
184-
self._nested_decorators.append(parser)
199+
self._nested_decorators_.append(parser)
200+
self._recursive_decorators.append(parser)
185201
return parser
186202

187203
def add_subparsers(self, *args, **kwargs):
@@ -193,7 +209,8 @@ def add_subparsers(self, *args, **kwargs):
193209
"""
194210
subparser = self.__class__(
195211
self._parser.add_subparsers(*args, **kwargs))
196-
self._nested_decorators.append(subparser)
212+
self._nested_decorators_.append(subparser)
213+
self._recursive_decorators.append(subparser)
197214
return subparser
198215

199216

colcon_core/argument_parser/destination_collector.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ def __init__(self, parser, **kwargs):
2222
_destinations=OrderedDict(),
2323
**kwargs)
2424

25-
def get_destinations(self):
25+
def get_destinations(self, *, recursive=True):
2626
"""
2727
Get destinations for all added arguments.
2828
29+
:param bool recursive: The flag if destinations from added parsers /
30+
subparsers should be included, destinations from grouped arguments
31+
are always included
2932
:returns: The destination names
3033
:rtype: OrderedDict
3134
"""
3235
destinations = OrderedDict()
3336
destinations.update(self._destinations)
34-
for d in self._nested_decorators:
37+
for d in self._group_decorators:
3538
destinations.update(d.get_destinations())
39+
if recursive:
40+
for d in self._recursive_decorators:
41+
destinations.update(d.get_destinations())
3642
return destinations
3743

3844
def add_argument(self, *args, **kwargs):

test/spell_check.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ setuppy
4242
setuptools
4343
sigint
4444
stdeb
45+
subparser
4546
subparsers
4647
symlink
4748
sysconfig

test/test_destination_collector.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ def test_destination_collector_decorator():
2121
group.add_argument('--other-option', action='store_true')
2222
assert decorator.get_destinations() == OrderedDict([
2323
('option', 'option'), ('other-option', 'other_option')])
24+
25+
subparser = decorator.add_subparsers(title='subs', dest='dest')
26+
parser = subparser.add_parser('parser')
27+
parser.add_argument('--another-option', action='store_true')
28+
assert decorator.get_destinations() == OrderedDict([
29+
('option', 'option'), ('other-option', 'other_option'),
30+
('another-option', 'another_option')])
31+
assert decorator.get_destinations(recursive=False) == OrderedDict([
32+
('option', 'option'), ('other-option', 'other_option')])

0 commit comments

Comments
 (0)