grit: count/newgrd/unit: sanity check the command line
Unlike the other grit tools, these three weren't really checking the
command line options that closely. Formalize it a bit so we reject
all unknown options and too few/many arguments.
Change-Id: Ifdd58be1b723ca8b09d5add0c3dcde7243a07ecc
Reviewed-on: https://chromium-review.googlesource.com/c/1338469
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608681}
diff --git a/tools/grit/grit/tool/count.py b/tools/grit/grit/tool/count.py
index 7921523..61225986 100644
--- a/tools/grit/grit/tool/count.py
+++ b/tools/grit/grit/tool/count.py
@@ -4,6 +4,8 @@
'''Count number of occurrences of a given message ID.'''
+import getopt
+
from grit import grd_reader
from grit.tool import interface
@@ -17,7 +19,17 @@
def ShortDescription(self):
return 'Count the number of times a given message ID is used.'
+ def ParseOptions(self, args):
+ """Set this objects and return all non-option arguments."""
+ own_opts, args = getopt.getopt(args, '')
+ return args
+
def Run(self, opts, args):
+ args = self.ParseOptions(args)
+ if len(args) != 1:
+ print ('This tool takes a single tool-specific argument, the message '
+ 'ID to count.')
+ return 2
self.SetOptions(opts)
id = args[0]
diff --git a/tools/grit/grit/tool/newgrd.py b/tools/grit/grit/tool/newgrd.py
index 38a0f752..e5169953 100644
--- a/tools/grit/grit/tool/newgrd.py
+++ b/tools/grit/grit/tool/newgrd.py
@@ -5,6 +5,8 @@
'''Tool to create a new, empty .grd file with all the basic sections.
'''
+import getopt
+
from grit.tool import interface
from grit import constants
from grit import util
@@ -59,8 +61,14 @@
def ShortDescription(self):
return 'Create a new empty .grd file.'
- def Run(self, global_options, my_arguments):
- if not len(my_arguments) == 1:
+ def ParseOptions(self, args):
+ """Set this objects and return all non-option arguments."""
+ own_opts, args = getopt.getopt(args, '')
+ return args
+
+ def Run(self, opts, args):
+ args = self.ParseOptions(args)
+ if len(args) != 1:
print 'This tool requires exactly one argument, the name of the output file.'
return 2
filename = my_arguments[0]
diff --git a/tools/grit/grit/tool/unit.py b/tools/grit/grit/tool/unit.py
index b3da7c1b7..2dba2d93 100644
--- a/tools/grit/grit/tool/unit.py
+++ b/tools/grit/grit/tool/unit.py
@@ -4,7 +4,7 @@
'''GRIT tool that runs the unit test suite for GRIT.'''
-
+import getopt
import unittest
import grit.test_suite_all
@@ -18,6 +18,16 @@
def ShortDescription(self):
return 'Use this tool to run all the unit tests for GRIT.'
+ def ParseOptions(self, args):
+ """Set this objects and return all non-option arguments."""
+ own_opts, args = getopt.getopt(args, '')
+ return args
+
def Run(self, opts, args):
+ args = self.ParseOptions(args)
+ if args:
+ print 'This tool takes no arguments.'
+ return 2
+
return unittest.TextTestRunner(verbosity=2).run(
grit.test_suite_all.TestSuiteAll())