aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-25 05:51:26 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-25 05:51:26 +0300
commit20add003b16bdbaba279ef6286a0f0025658d1e2 (patch)
treec73888320c21d9213babe6e4ef87d73ee25e1dd5
parentsort algorithms in usage messages (diff)
downloadsorting-algorithms-20add003b16bdbaba279ef6286a0f0025658d1e2.tar.gz
sorting-algorithms-20add003b16bdbaba279ef6286a0f0025658d1e2.zip
improve command line parsing errors
Diffstat (limited to '')
-rw-r--r--plot.py16
-rw-r--r--test.py7
2 files changed, 16 insertions, 7 deletions
diff --git a/plot.py b/plot.py
index 2681628..35fd6ed 100644
--- a/plot.py
+++ b/plot.py
@@ -29,22 +29,28 @@ def plot_algorithm(algorithm, input_kind=_DEFAULT_INPUT_KIND,
params.plot_running_time(output_path)
def _parse_natural_number(s):
- n = int(s)
+ try:
+ n = int(s)
+ except ValueError:
+ raise argparse.ArgumentTypeError('must be a non-negative integer: ' + str(s))
if n < 0:
- raise argparse.ArgumentTypeError('must not be a negative number')
+ raise argparse.ArgumentTypeError('must be a non-negative integer')
return n
def _parse_positive_number(s):
- n = int(s)
+ try:
+ n = int(s)
+ except ValueError:
+ raise argparse.ArgumentTypeError('must be a positive integer: ' + str(s))
if n < 1:
- raise argparse.ArgumentTypeError('must be positive')
+ raise argparse.ArgumentTypeError('must be a positive integer')
return n
def _parse_input_kind(s):
try:
return InputKind(s)
except ValueError:
- raise argparse.ArgumentTypeError('invalid input_kind: ' + str(s))
+ raise argparse.ArgumentTypeError('invalid input kind: ' + str(s))
def _format_algorithm(codename):
return '* {}: {}'.format(codename, registry.get(codename).display_name)
diff --git a/test.py b/test.py
index 0341bf3..a9bf401 100644
--- a/test.py
+++ b/test.py
@@ -22,9 +22,12 @@ def test(algorithm, input_kind=_DEFAULT_INPUT_KIND, length=_DEFAULT_LENGTH):
print(output)
def _parse_natural_number(s):
- n = int(s)
+ try:
+ n = int(s)
+ except ValueError:
+ raise argparse.ArgumentTypeError('must be a non-negative integer: ' + str(s))
if n < 0:
- raise argparse.ArgumentTypeError('must not be a negative number')
+ raise argparse.ArgumentTypeError('must be a non-negative integer')
return n
def _parse_input_kind(s):