Skip to content

Commit 55cfcad

Browse files
authored
Merge pull request #81 from ShaneEverittM/positional-args-required
Fix help text around positional arguments.
2 parents 0304007 + 2771a8e commit 55cfcad

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

tap/tap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
get_origin,
2222
GitInfo,
2323
is_option_arg,
24+
is_positional_arg,
2425
type_to_str,
2526
get_literals,
2627
boolean_type,
@@ -159,7 +160,7 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
159160
kwargs['help'] += type_to_str(self._annotations[variable]) + ', '
160161

161162
# Required/default
162-
if kwargs.get('required', False):
163+
if kwargs.get('required', False) or is_positional_arg(*name_or_flags):
163164
kwargs['help'] += 'required'
164165
else:
165166
kwargs['help'] += f'default={kwargs.get("default", None)}'

tap/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ def is_option_arg(*name_or_flags) -> bool:
171171
return any(name_or_flag.startswith('-') for name_or_flag in name_or_flags)
172172

173173

174+
def is_positional_arg(*name_or_flags) -> bool:
175+
"""Returns whether the argument is a positional arg (as opposed to an optional arg).
176+
177+
:param name_or_flags: Either a name or a list of option strings, e.g. foo or -f, --foo.
178+
:return: True if the argument is a positional arg, False otherwise.
179+
"""
180+
return not is_option_arg(*name_or_flags)
181+
182+
174183
def tokenize_source(obj: object) -> Generator:
175184
"""Returns a generator for the tokens of the object's source code."""
176185
source = inspect.getsource(obj)

tests/test_actions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ def configure(self):
211211
args = ExtendListIntTap().parse_args('--arg 1 2 --arg 3 --arg 4 5'.split())
212212
self.assertEqual(args.arg, [0, 1, 2, 3, 4, 5])
213213

214+
def test_positional_default(self):
215+
class PositionalDefault(Tap):
216+
arg: str
217+
218+
def configure(self):
219+
self.add_argument('arg')
220+
221+
help_regex = r'.*positional arguments:\n.*arg\s*\(str, required\).*'
222+
help_text = PositionalDefault().format_help()
223+
self.assertRegexpMatches(help_text, help_regex)
224+
214225

215226
if __name__ == '__main__':
216227
unittest.main()

0 commit comments

Comments
 (0)