Skip to content

Commit 621e435

Browse files
Fixed failure when reading integer option value from short form (#8)
* settings * fix: fixed failure message to be passed to `MissingValueException` * feature: added unit-tests to exercise the observed failure * fix: falsy default values causing `MissingValueException` for option arguments Using a truthiness check `if given_value:` on option arguments causes valid, falsy default values (such as `0`, `0.0`, or `False`) to be treated as if they were missing or not set. This leads to an incorrect `MissingValueException` being raised during command-line argument parsing. Replacing this with an explicit `is not None` check allows falsy default values to be correctly recognized, processed, and validated according to their respective value types. * 0.8.12
1 parent f9de2ef commit 621e435

6 files changed

Lines changed: 119 additions & 5 deletions

File tree

‎.vscode/settings.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"editor.renderWhitespace": "all",
1818
"editor.rulers": [ 76 ],
1919
"editor.tabSize": 2,
20+
"files.insertFinalNewline": true,
21+
"files.trimTrailingWhitespace": true,
22+
"git.mergeEditor": false,
2023
"files.exclude": {
2124
"**/__pycache__": true,
2225
"**/*.egg-info": true,

‎CHANGES.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# **CLASP.Python** Changes <!-- omit from toc -->
22

33

4+
## 0.8.12 - 28th July 2026
5+
6+
* fixed falsy default values causing `MissingValueException` for option arguments;
7+
* fixed failure message to be passed to `MissingValueException`;
8+
9+
410
## 0.8.11 - 27th July 2026
511

612
* ~ packaging & boilerplate improvements:

‎pyclasp/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
__license__ = "BSD-3-Clause"
1111
__maintainer__ = "Matt Wilson"
1212
__status__ = "Beta"
13-
__version__ = "0.8.11"
13+
__version__ = "0.8.12"
1414

1515
from .exceptions import *
1616
from .flag_specification import FlagSpecification, flag

‎pyclasp/option_argument.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _set_value(self, value, from_ctor=False):
8585

8686
given_value = arg_spec.default_value
8787

88-
if given_value:
88+
if given_value is not None:
8989

9090

9191
if arg_spec.value_type:
@@ -148,7 +148,23 @@ def _set_value(self, value, from_ctor=False):
148148
pass
149149
else:
150150

151-
raise MissingValueException("the '%s' option does not have a value to be interpreted as an integer" % (self.name))
151+
if arg_spec.value_type == bool:
152+
153+
type_name = "boolean"
154+
elif arg_spec.value_type == float:
155+
156+
type_name = "a number"
157+
elif arg_spec.value_type == int:
158+
159+
type_name = "an integer"
160+
elif _SUPPORT_long and arg_spec.value_type == long:
161+
162+
type_name = "a long integer"
163+
else:
164+
165+
type_name = "a value"
166+
167+
raise MissingValueException("the '%s' option does not have a value to be interpreted as %s" % (self.name, type_name))
152168

153169

154170
self.given_value = given_value

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
name="pyclasp",
77
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, !=3.6.*, !=3.7.*',
8-
version='0.8.11',
8+
version='0.8.12',
99

1010
author="Matt Wilson",
1111
author_email="matthew@synesis.com.au",

‎tests/test_typed_values.py‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,44 @@ def test_empty_option_value_of_type_int(self):
303303

304304
argv = ( "myprog", "--length=" )
305305

306-
with self.assertRaises(clasp.MissingValueException):
306+
with self.assertRaises(clasp.MissingValueException) as ctx:
307307

308308
clasp.parse(argv, specifications)
309309

310+
self.assertEqual("the '--length' option does not have a value to be interpreted as an integer", str(ctx.exception))
311+
312+
313+
def test_empty_option_value_of_type_float(self):
314+
315+
specifications = (
316+
317+
clasp.option('--length', alias='-l', value_type=float),
318+
)
319+
320+
argv = ( 'myprog', '--length=' )
321+
322+
with self.assertRaises(clasp.MissingValueException) as ctx:
323+
324+
clasp.parse(argv, specifications)
325+
326+
self.assertEqual("the '--length' option does not have a value to be interpreted as a number", str(ctx.exception))
327+
328+
329+
def test_empty_option_value_of_type_bool(self):
330+
331+
specifications = (
332+
333+
clasp.option('--length', alias='-l', value_type=bool),
334+
)
335+
336+
argv = ( 'myprog', '--length=' )
337+
338+
with self.assertRaises(clasp.MissingValueException) as ctx:
339+
340+
clasp.parse(argv, specifications)
341+
342+
self.assertEqual("the '--length' option does not have a value to be interpreted as boolean", str(ctx.exception))
343+
310344

311345
def test_empty_option_value_of_type_int_2(self):
312346

@@ -442,6 +476,61 @@ def test_flags_of_flags_and_options_combined(self):
442476
self.assertEqual(0, len(args.values))
443477

444478

479+
def test_reproduce_falsy_default_issue(self):
480+
481+
specifications = (
482+
clasp.option(
483+
"--depth",
484+
alias="-d",
485+
default=0,
486+
value_type=int,
487+
),
488+
clasp.option(
489+
"--exchange",
490+
alias="-e",
491+
required=True,
492+
value_type=str,
493+
),
494+
)
495+
496+
argv = ('liquidity-assessor', '-e', 'bybit', '-d', '10')
497+
args = clasp.parse(argv, specifications)
498+
499+
self.assertEqual(2, len(args.options))
500+
501+
depth_option = next(opt for opt in args.options if opt.name == '--depth')
502+
self.assertEqual(10, depth_option.value)
503+
504+
exchange_option = next(opt for opt in args.options if opt.name == '--exchange')
505+
self.assertEqual('bybit', exchange_option.value)
506+
507+
508+
def test_reproduce_falsy_default_issue_with_default_used(self):
509+
510+
specifications = (
511+
clasp.option(
512+
"--depth",
513+
alias="-d",
514+
default=0,
515+
value_type=int,
516+
),
517+
clasp.option(
518+
"--exchange",
519+
alias="-e",
520+
required=True,
521+
value_type=str,
522+
),
523+
)
524+
525+
argv = ('liquidity-assessor', '-e', 'bybit')
526+
args = clasp.parse(argv, specifications)
527+
528+
# In CLASP, options with defaults are not automatically added to args.options unless specified on the CLI or if we lookup/access them?
529+
# Wait, let's verify if they are added or not.
530+
self.assertEqual(1, len(args.options))
531+
exchange_option = args.options[0]
532+
self.assertEqual('bybit', exchange_option.value)
533+
445534

446535
if '__main__' == __name__:
447536

0 commit comments

Comments
 (0)