Skip to content

Commit 104c3dc

Browse files
committed
Make BoolArg nullable (still a bit untested)
1 parent c9d0b9b commit 104c3dc

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/matej/argparse.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def __init__(self, *flags, **kw):
131131
"""
132132
self.flags = flags
133133
self.kw = kw
134+
if 'nargs' in kw and (kw['nargs'] in ('*', '+') or isinstance(kw['nargs'], int) and kw['nargs'] > 1):
135+
self.kw.setdefault('default', [])
134136

135137
def add_to_ap(self, parser, **kw):
136138
""" Add this argument to the given parser. """
@@ -236,16 +238,14 @@ class PathArg(NullableArg, type=Path):
236238
""" Path argument. """
237239

238240

239-
class BoolArg(Arg):
241+
class BoolArg(NullableArg):
240242
""" Boolean argument. """
241-
def __init__(self, *flags, default=False, negative_flags=None, help="", negative_help=None, **kw):
243+
def __init__(self, *flags, negative_flags=None, help="", negative_help=None, **kw):
242244
"""
243245
Initialise the argument.
244246
245247
Parameters
246248
----------
247-
default : bool, default=False
248-
Default value for the argument.
249249
negative_flags : Collection[str], optional
250250
Flags that set the argument to `False`. By default, they will be automatically generated by prefixing the long flags with `--no-`˙.
251251
If `...` is passed inside the `negative_flags` list, the auto-generated flags will be included in the list at that position.
@@ -260,7 +260,7 @@ def __init__(self, *flags, default=False, negative_flags=None, help="", negative
260260
For other parameters, see the documentation of :meth:`Arg.__init__`.
261261
"""
262262
super().__init__(*flags, **kw)
263-
self.default = default
263+
self.default = self.kw.pop('default', False)
264264

265265
self.short_flags = [flag for flag in self.flags if flag[0] == '-' and flag[1] != '-']
266266
self.yes_flags = [flag for flag in self.flags if flag[:2] == '--']
@@ -277,9 +277,9 @@ def __init__(self, *flags, default=False, negative_flags=None, help="", negative
277277
self.short_help = help + " (this toggles the default if no value is passed)" if help else help
278278
self.yes_help = help
279279
self.no_help = negative_help if negative_help is not None else "Do not " + help[0].lower() + help[1:] if help else help
280-
if default:
280+
if self.default is True:
281281
self.yes_help += " <default>"
282-
else:
282+
elif self.default is False:
283283
self.no_help += " <default>"
284284

285285
def add_to_ap(self, parser, **kw):
@@ -296,23 +296,25 @@ def add_to_ap(self, parser, **kw):
296296
- toggle the default value if no argument is passed.
297297
"""
298298
kw = self.kw | kw
299+
typeless_kw = {k: v for k, v in kw.items() if k != 'type'} # Need this for yes and no flags, since type conflicts with store_x actions
299300
dest = kw.get('dest', (self.yes_flags[0] if self.yes_flags else self.flags[0]).lstrip('-')).replace('-', '_')
300301
group = parser.add_mutually_exclusive_group()
301302
result = None
302303
if self.yes_flags:
303-
result = group.add_argument(*self.yes_flags, dest=dest, default=argparse.SUPPRESS, action='store_true', help=self.yes_help, **kw)
304+
result = group.add_argument(*self.yes_flags, dest=dest, default=argparse.SUPPRESS, action='store_true', help=self.yes_help, **typeless_kw)
304305
if self.no_flags:
305-
group.add_argument(*self.no_flags, dest=dest, default=argparse.SUPPRESS, action='store_false', help=self.no_help, **kw)
306+
group.add_argument(*self.no_flags, dest=dest, default=argparse.SUPPRESS, action='store_false', help=self.no_help, **typeless_kw)
306307
if self.short_flags:
307-
result = group.add_argument(*self.short_flags, dest=dest, nargs='?', default=self.default, const=not self.default, type=self._str_to_bool, help=self.short_help, **kw)
308+
result = group.add_argument(*self.short_flags, dest=dest, nargs='?', default=bool(self.default), const=not self.default, help=self.short_help, **kw)
308309
return result
309310

310311
@staticmethod
311312
def _no_f(arg):
312313
return '--no-' + arg.lstrip('-')
313314

314-
@staticmethod
315-
def _str_to_bool(s):
315+
def _type(self, s):
316+
if super()._type(s) is None:
317+
return None
316318
return s.lower() in ('true', 'yes', 't', 'y', '1')
317319

318320

0 commit comments

Comments
 (0)