Commit bda95d84 authored by Mike Frysinger's avatar Mike Frysinger Committed by Commit Bot

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: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608681}
parent 4085e15a
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
'''Count number of occurrences of a given message ID.''' '''Count number of occurrences of a given message ID.'''
import getopt
from grit import grd_reader from grit import grd_reader
from grit.tool import interface from grit.tool import interface
...@@ -17,7 +19,17 @@ class CountMessage(interface.Tool): ...@@ -17,7 +19,17 @@ class CountMessage(interface.Tool):
def ShortDescription(self): def ShortDescription(self):
return 'Count the number of times a given message ID is used.' 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): 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) self.SetOptions(opts)
id = args[0] id = args[0]
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
'''Tool to create a new, empty .grd file with all the basic sections. '''Tool to create a new, empty .grd file with all the basic sections.
''' '''
import getopt
from grit.tool import interface from grit.tool import interface
from grit import constants from grit import constants
from grit import util from grit import util
...@@ -59,8 +61,14 @@ where in the file.''' ...@@ -59,8 +61,14 @@ where in the file.'''
def ShortDescription(self): def ShortDescription(self):
return 'Create a new empty .grd file.' return 'Create a new empty .grd file.'
def Run(self, global_options, my_arguments): def ParseOptions(self, args):
if not len(my_arguments) == 1: """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.' print 'This tool requires exactly one argument, the name of the output file.'
return 2 return 2
filename = my_arguments[0] filename = my_arguments[0]
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
'''GRIT tool that runs the unit test suite for GRIT.''' '''GRIT tool that runs the unit test suite for GRIT.'''
import getopt
import unittest import unittest
import grit.test_suite_all import grit.test_suite_all
...@@ -18,6 +18,16 @@ This happens in the environment that is set up by the basic GRIT runner.''' ...@@ -18,6 +18,16 @@ This happens in the environment that is set up by the basic GRIT runner.'''
def ShortDescription(self): def ShortDescription(self):
return 'Use this tool to run all the unit tests for GRIT.' 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): 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( return unittest.TextTestRunner(verbosity=2).run(
grit.test_suite_all.TestSuiteAll()) grit.test_suite_all.TestSuiteAll())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment