blob: 1a52ace9b8964ec0de9fd08d95dadd34303b7ad7 [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
3r"""
4This module provides valuable argument processing functions like
5gen_get_options and sprint_args.
6"""
7
8import sys
9
10import gen_print as gp
11
12
13
14###############################################################################
15def valid_value(var_value,
16 invalid_values=[""],
17 valid_values=[]):
18
19 r"""
20 Return True if var_value is a valid value. Otherwise, return False and
21 print an error message to stderr.
22
23 Description of arguments:
24 var_value The value being validated.
25 invalid_values A list of invalid values. If var_value is
26 equal to any of these, it is invalid.
27 Note that if you specify anything for
28 invalid_values (below), the valid_values
29 list is not even processed.
30 valid_values A list of invalid values. var_value must
31 be equal to one of these values to be
32 considered valid.
33 """
34
35 len_valid_values = len(valid_values)
36 len_invalid_values = len(invalid_values)
37 if len_valid_values > 0 and len_invalid_values > 0:
38 gp.print_error_report("Programmer error - You must provide either an" +
39 " invalid_values list or a valid_values" +
40 " list but NOT both.")
41 return False
42
43 if len_valid_values > 0:
44 # Processing the valid_values list.
45 if var_value in valid_values:
46 return True
47 var_name = gp.get_arg_name(0, 1, 2)
48 gp.print_error_report("The following variable has an invalid" +
49 " value:\n" +
50 gp.sprint_varx(var_name, var_value) +
51 "\nIt must be one of the following values:\n" +
52 gp.sprint_varx("valid_values", valid_values))
53 return False
54
55 if len_invalid_values == 0:
56 gp.print_error_report("Programmer error - You must provide either an" +
57 " invalid_values list or a valid_values" +
58 " list. Both are empty.")
59 return False
60
61 # Assertion: We have an invalid_values list. Processing it now.
62 if var_value not in invalid_values:
63 return True
64
65 var_name = gp.get_arg_name(0, 1, 2)
66 gp.print_error_report("The following variable has an invalid value:\n" +
67 gp.sprint_varx(var_name, var_value) + "\nIt must" +
68 " NOT be one of the following values:\n" +
69 gp.sprint_varx("invalid_values", invalid_values))
70 return False
71
72###############################################################################
73
74
75###############################################################################
76def valid_integer(var_value):
77
78 r"""
79 Return True if var_value is a valid integer. Otherwise, return False and
80 print an error message to stderr.
81
82 Description of arguments:
83 var_value The value being validated.
84 """
85
86 # This currently allows floats which is not good.
87
88 try:
89 if type(int(var_value)) is int:
90 return True
91 except ValueError:
92 pass
93
94 # If we get to this point, the validation has failed.
95
96 var_name = gp.get_arg_name(0, 1, 2)
97 gp.print_varx("var_name", var_name)
98
99 gp.print_error_report("Invalid integer value:\n" +
100 gp.sprint_varx(var_name, var_value))
101
102 return False
103
104###############################################################################