Skip to content

Commit

Permalink
args.py: Show a proper usage error on invalid arguments.
Browse files Browse the repository at this point in the history
Addresses issue #277.
  • Loading branch information
Andy Chu committed Apr 17, 2019
1 parent 32eda5e commit d14cca9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions frontend/args.py
Expand Up @@ -169,6 +169,8 @@ def OnMatch(self, prefix, suffix, arg_r, out):

#log('SetToArg arg %r', arg)

# TODO: better location information for all these errors.

typ = self.arg_type
if isinstance(typ, list):
if arg not in typ:
Expand All @@ -179,9 +181,17 @@ def OnMatch(self, prefix, suffix, arg_r, out):
if typ == Str:
value = arg
elif typ == Int:
value = int(arg) # TODO: check errors
try:
value = int(arg)
except ValueError:
raise UsageError(
'Expected integer after %r, got %r' % ('-' + self.name, arg))
elif typ == Float:
value = float(arg) # TODO: check errors
try:
value = float(arg)
except ValueError:
raise UsageError(
'Expected numeric after %r, got %r' % ('-' + self.name, arg))
else:
raise AssertionError

Expand Down
7 changes: 7 additions & 0 deletions spec/builtin-io.test.sh
Expand Up @@ -398,3 +398,10 @@ argv.py "${arguments[@]}"
## END
## N-I dash/mksh/zsh/ash status: 2
## N-I dash/mksh/zsh/ash stdout-json: ""

#### read -n with invalid arg
read -n not_a_number
echo status=$?
## stdout: status=2
## OK bash stdout: status=1
## N-I zsh stdout-json: ""

0 comments on commit d14cca9

Please sign in to comment.