blob: b82265b3ff4c6b98f84ace2401a26fa0f9334002 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001Improve error reporting in smart
2
3Add code to check proper command line arguments for various
4smart commands. Exit with error if erroneous/additional arguments
5are given in the command line.
6
7Upstream-Status: Pending
8
9Signed-off-by: Bogdan Marinescu <bogdan.a.marinescu@intel.com>
10
11diff --git a/smart/util/optparse.py b/smart/util/optparse.py
12index 6fff1bc..f445a3b 100644
13--- a/smart/util/optparse.py
14+++ b/smart/util/optparse.py
15@@ -70,6 +70,8 @@ import sys, os
16 import types
17 import textwrap
18 from gettext import gettext as _
19+from smart import Error
20+import re
21
22 def _repr(self):
23 return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), self)
24@@ -710,6 +712,12 @@ class Option:
25 self.action, self.dest, opt, value, values, parser)
26
27 def take_action(self, action, dest, opt, value, values, parser):
28+ # Keep all the options in the command line in the '_given_opts' array
29+ # This will be used later to validate the command line
30+ given_opts = getattr(parser.values, "_given_opts", [])
31+ user_opt = re.sub(r"^\-*", "", opt).replace("-", "_")
32+ given_opts.append(user_opt)
33+ setattr(parser.values, "_given_opts", given_opts)
34 if action == "store":
35 setattr(values, dest, value)
36 elif action == "store_const":
37@@ -821,6 +829,54 @@ class Values:
38 setattr(self, attr, value)
39 return getattr(self, attr)
40
41+ # Check if the given option has the specified number of arguments
42+ # Raise an error if the option has an invalid number of arguments
43+ # A negative number for 'nargs' means "at least |nargs| arguments are needed"
44+ def check_args_of_option(self, opt, nargs, err=None):
45+ given_opts = getattr(self, "_given_opts", [])
46+ if not opt in given_opts:
47+ return
48+ values = getattr(self, opt, [])
49+ if type(values) != type([]):
50+ return
51+ if nargs < 0:
52+ nargs = -nargs
53+ if len(values) >= nargs:
54+ return
55+ if not err:
56+ if nargs == 1:
57+ err = _("Option '%s' requires at least one argument") % opt
58+ else:
59+ err = _("Option '%s' requires at least %d arguments") % (opt, nargs)
60+ raise Error, err
61+ elif nargs == 0:
62+ if len( values ) == 0:
63+ return
64+ raise Error, err
65+ else:
66+ if len(values) == nargs:
67+ return
68+ if not err:
69+ if nargs == 1:
70+ err = _("Option '%s' requires one argument") % opt
71+ else:
72+ err = _("Option '%s' requires %d arguments") % (opt, nargs)
73+ raise Error, err
74+
75+ # Check that at least one of the options in 'actlist' was given as an argument
76+ # to the command 'cmdname'
77+ def ensure_action(self, cmdname, actlist):
78+ given_opts = getattr(self, "_given_opts", [])
79+ for action in actlist:
80+ if action in given_opts:
81+ return
82+ raise Error, _("No action specified for command '%s'") % cmdname
83+
84+ # Check if there are any other arguments left after parsing the command line and
85+ # raise an error if such arguments are found
86+ def check_remaining_args(self):
87+ if self.args:
88+ raise Error, _("Invalid argument(s) '%s'" % str(self.args))
89
90 class OptionContainer:
91