Skip to content

Commit 3fd6618

Browse files
committed
Small hotfixes
1 parent 4e4e3a7 commit 3fd6618

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

src/matej/argparse.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, *args, **kw):
2828

2929
def add_arg(self, arg):
3030
""" Add an :class:`Arg` instance to this parser. """
31-
arg.add_to_ap(self)
31+
return arg.add_to_ap(self)
3232

3333
def add_str_arg(self, *args, **kw):
3434
"""
@@ -188,18 +188,16 @@ def __init__(self, *flags, nullable=None, null_phrases=('', 'none'), **kw):
188188
Parameters
189189
----------
190190
nullable : Optional[bool]
191-
Whether the argument can be `None`. If not provided, will be `False`, unless `default` is passed as `None`.
191+
Whether the argument can be `None`. If not provided, will be `False`, unless `default` is explicitly passed as `None`.
192192
null_phrases : Collection[str], default=('', 'none')
193193
Phrases that will be interpreted as `None` if the argument is nullable (case-insensitive).
194194
"""
195-
self.nullable = (kw.get('default') is None) if nullable is None else nullable
195+
default_is_none = 'default' in kw and kw['default'] is None
196+
if default_is_none and nullable is False:
197+
raise ValueError("Cannot have a None default value for a non-nullable argument")
198+
self.nullable = default_is_none if nullable is None else nullable
196199
self.null_phrases = tmap(str.lower, null_phrases)
197-
if 'default' in kw and kw['default'] is None:
198-
if self.nullable is False:
199-
raise ValueError("Cannot have a None default value for a non-nullable argument")
200-
self.nullable = True
201200
kw.setdefault('type', self._type)
202-
kw.setdefault('nargs', '?')
203201
super().__init__(*flags, **kw)
204202

205203
def __init_subclass__(cls, type=str):
@@ -212,12 +210,29 @@ def _type(self, s):
212210
return self.type(s)
213211

214212

213+
# This used to have nargs='?' by default, so if you get errors or weird behaviour, pass nargs='?' when constructing this argument
215214
class StrArg(NullableArg):
216-
""" String argument. By default this argument is optional and accepts a single string as its value. """
215+
""" String argument. """
216+
def __init__(self, *flags, lowercase=False, **kw):
217+
"""
218+
Initialise the argument.
219+
220+
Parameters
221+
----------
222+
lowercase : bool, default=False
223+
Whether to convert the argument value to lowercase. Useful when the argument value is used for case-insensitive purposes.
224+
"""
225+
super().__init__(*flags, **kw)
226+
self.lowercase = lowercase
227+
228+
def _type(self, s):
229+
s = super()._type(s)
230+
return s.lower() if self.lowercase and s is not None else s
217231

218232

233+
# This used to have nargs='?' by default, so if you get errors or weird behaviour, pass nargs='?' when constructing this argument
219234
class PathArg(NullableArg, type=Path):
220-
""" Path argument. By default this argument is optional and accepts a single path as its value. """
235+
""" Path argument. """
221236

222237

223238
class BoolArg(Arg):

0 commit comments

Comments
 (0)