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

grit: add --help support to subtools

This makes `grit build --help` work too.

Bug: 747171
Change-Id: I5297019a34446196e625635318f2373620ffd87f
Reviewed-on: https://chromium-review.googlesource.com/c/1339341
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610320}
parent 8a7f841c
...@@ -223,7 +223,7 @@ def Main(args): ...@@ -223,7 +223,7 @@ def Main(args):
print ("Help for 'grit %s' (for general help, run 'grit help'):\n" print ("Help for 'grit %s' (for general help, run 'grit help'):\n"
% (tool)) % (tool))
print _GetToolInfo(tool)[_FACTORY]().__doc__ _GetToolInfo(tool)[_FACTORY]().ShowUsage()
return 0 return 0
if not _GetToolInfo(tool): if not _GetToolInfo(tool):
print "No such tool. Try running 'grit help' for a list of tools." print "No such tool. Try running 'grit help' for a list of tools."
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
import getopt import getopt
import os.path import os.path
import StringIO import StringIO
import sys
from xml.dom import Node from xml.dom import Node
import xml.dom.minidom import xml.dom.minidom
...@@ -124,7 +125,8 @@ OPTIONS may be any of the following: ...@@ -124,7 +125,8 @@ OPTIONS may be any of the following:
Android2Grd._HEADER_DIR_FLAG, Android2Grd._HEADER_DIR_FLAG,
Android2Grd._XTB_DIR_FLAG, Android2Grd._XTB_DIR_FLAG,
Android2Grd._XML_DIR_FLAG, ] Android2Grd._XML_DIR_FLAG, ]
(opts, args) = getopt.getopt(args, None, ['%s=' % o for o in flags]) (opts, args) = getopt.getopt(
args, None, ['%s=' % o for o in flags] + ['help'])
for key, val in opts: for key, val in opts:
# Get rid of the preceding hypens. # Get rid of the preceding hypens.
...@@ -143,6 +145,9 @@ OPTIONS may be any of the following: ...@@ -143,6 +145,9 @@ OPTIONS may be any of the following:
self.xtb_dir = val self.xtb_dir = val
elif k == Android2Grd._XML_DIR_FLAG: elif k == Android2Grd._XML_DIR_FLAG:
self.xml_res_dir = val self.xml_res_dir = val
elif k == 'help':
self.ShowUsage()
sys.exit(0)
return args return args
def Run(self, opts, args): def Run(self, opts, args):
......
...@@ -163,6 +163,7 @@ are exported to translation interchange files (e.g. XMB files), etc. ...@@ -163,6 +163,7 @@ are exported to translation interchange files (e.g. XMB files), etc.
replace_ellipsis = True replace_ellipsis = True
(own_opts, args) = getopt.getopt(args, 'a:p:o:D:E:f:w:t:', (own_opts, args) = getopt.getopt(args, 'a:p:o:D:E:f:w:t:',
('depdir=','depfile=','assert-file-list=', ('depdir=','depfile=','assert-file-list=',
'help',
'output-all-resource-defines', 'output-all-resource-defines',
'no-output-all-resource-defines', 'no-output-all-resource-defines',
'no-replace-ellipsis', 'no-replace-ellipsis',
...@@ -209,6 +210,9 @@ are exported to translation interchange files (e.g. XMB files), etc. ...@@ -209,6 +210,9 @@ are exported to translation interchange files (e.g. XMB files), etc.
js_minifier = val js_minifier = val
elif key == '--whitelist-support': elif key == '--whitelist-support':
whitelist_support = True whitelist_support = True
elif key == '--help':
self.ShowUsage()
sys.exit(0)
if len(args): if len(args):
print 'This tool takes no tool-specific arguments.' print 'This tool takes no tool-specific arguments.'
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import getopt import getopt
import os import os
import sys
from grit import grd_reader from grit import grd_reader
from grit.node import structure from grit.node import structure
...@@ -32,10 +33,13 @@ The output directory is used for display only. ...@@ -32,10 +33,13 @@ The output directory is used for display only.
def Run(self, opts, args): def Run(self, opts, args):
"""Main method for the buildinfo tool.""" """Main method for the buildinfo tool."""
self.output_directory = '.' self.output_directory = '.'
(own_opts, args) = getopt.getopt(args, 'o:') (own_opts, args) = getopt.getopt(args, 'o:', ('help',))
for (key, val) in own_opts: for (key, val) in own_opts:
if key == '-o': if key == '-o':
self.output_directory = val self.output_directory = val
elif key == '--help':
self.ShowUsage()
sys.exit(0)
if len(args) > 0: if len(args) > 0:
print 'This tool takes exactly one argument: the output directory via -o' print 'This tool takes exactly one argument: the output directory via -o'
return 2 return 2
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
'''Count number of occurrences of a given message ID.''' '''Count number of occurrences of a given message ID.'''
import getopt import getopt
import sys
from grit import grd_reader from grit import grd_reader
from grit.tool import interface from grit.tool import interface
...@@ -21,7 +22,11 @@ class CountMessage(interface.Tool): ...@@ -21,7 +22,11 @@ class CountMessage(interface.Tool):
def ParseOptions(self, args): def ParseOptions(self, args):
"""Set this objects and return all non-option arguments.""" """Set this objects and return all non-option arguments."""
own_opts, args = getopt.getopt(args, '') own_opts, args = getopt.getopt(args, '', ('help',))
for key, val in own_opts:
if key == '--help':
self.ShowUsage()
sys.exit(0)
return args return args
def Run(self, opts, args): def Run(self, opts, args):
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import os import os
import getopt import getopt
import sys
import tempfile import tempfile
from grit.node import structure from grit.node import structure
...@@ -48,7 +49,7 @@ class DiffStructures(interface.Tool): ...@@ -48,7 +49,7 @@ class DiffStructures(interface.Tool):
def Run(self, global_opts, args): def Run(self, global_opts, args):
(opts, args) = getopt.getopt(args, 's:e:t:', (opts, args) = getopt.getopt(args, 's:e:t:',
['left_encoding=', 'right_encoding=']) ('help', 'left_encoding=', 'right_encoding='))
for key, val in opts: for key, val in opts:
if key == '-s': if key == '-s':
self.section = val self.section = val
...@@ -61,6 +62,9 @@ class DiffStructures(interface.Tool): ...@@ -61,6 +62,9 @@ class DiffStructures(interface.Tool):
self.left_encoding = val self.left_encoding = val
elif key == '--right_encoding': elif key == '--right_encoding':
self.right_encoding == val self.right_encoding == val
elif key == '--help':
self.ShowUsage()
sys.exit(0)
if len(args) != 2: if len(args) != 2:
print "Incorrect usage - 'grit help sdiff' for usage details." print "Incorrect usage - 'grit help sdiff' for usage details."
......
...@@ -38,6 +38,10 @@ class Tool(object): ...@@ -38,6 +38,10 @@ class Tool(object):
def __init__(self): def __init__(self):
self.o = None self.o = None
def ShowUsage(self):
'''Show usage text for this tool.'''
print self.__doc__
def SetOptions(self, opts): def SetOptions(self, opts):
self.o = opts self.o = opts
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
''' '''
import getopt import getopt
import sys
from grit.tool import interface from grit.tool import interface
from grit import constants from grit import constants
...@@ -63,7 +64,11 @@ where in the file.''' ...@@ -63,7 +64,11 @@ where in the file.'''
def ParseOptions(self, args): def ParseOptions(self, args):
"""Set this objects and return all non-option arguments.""" """Set this objects and return all non-option arguments."""
own_opts, args = getopt.getopt(args, '') own_opts, args = getopt.getopt(args, '', ('help',))
for key, val in own_opts:
if key == '--help':
self.ShowUsage()
sys.exit(0)
return args return args
def Run(self, opts, args): def Run(self, opts, args):
......
...@@ -9,6 +9,7 @@ import os.path ...@@ -9,6 +9,7 @@ import os.path
import getopt import getopt
import re import re
import StringIO import StringIO
import sys
import types import types
import grit.node.empty import grit.node.empty
...@@ -156,11 +157,12 @@ C preprocessor on the .rc file or manually edit it before using this tool. ...@@ -156,11 +157,12 @@ C preprocessor on the .rc file or manually edit it before using this tool.
self.pre_process = None self.pre_process = None
self.post_process = None self.post_process = None
def ParseOptions(self, args): def ParseOptions(self, args, help_func=None):
'''Given a list of arguments, set this object's options and return '''Given a list of arguments, set this object's options and return
all non-option arguments. all non-option arguments.
''' '''
(own_opts, args) = getopt.getopt(args, 'e:h:u:n:r', ['pre=', 'post=']) (own_opts, args) = getopt.getopt(args, 'e:h:u:n:r',
('help', 'pre=', 'post='))
for (key, val) in own_opts: for (key, val) in own_opts:
if key == '-e': if key == '-e':
self.input_encoding = val self.input_encoding = val
...@@ -176,6 +178,12 @@ C preprocessor on the .rc file or manually edit it before using this tool. ...@@ -176,6 +178,12 @@ C preprocessor on the .rc file or manually edit it before using this tool.
self.pre_process = val self.pre_process = val
elif key == '--post': elif key == '--post':
self.post_process = val self.post_process = val
elif key == '--help':
if help_func is None:
self.ShowUsage()
else:
help_func()
sys.exit(0)
return args return args
def Run(self, opts, args): def Run(self, opts, args):
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import getopt import getopt
import os import os
import sys
from grit import grd_reader from grit import grd_reader
from grit import pseudo from grit import pseudo
...@@ -193,7 +194,7 @@ near the top of the file, before you open it in Visual Studio. ...@@ -193,7 +194,7 @@ near the top of the file, before you open it in Visual Studio.
def Run(self, opts, args): def Run(self, opts, args):
self.SetOptions(opts) self.SetOptions(opts)
own_opts, args = getopt.getopt(args, 'l:f:c:D:') own_opts, args = getopt.getopt(args, 'l:f:c:D:', ('help',))
for key, val in own_opts: for key, val in own_opts:
if key == '-l': if key == '-l':
self.SetLanguage(val) self.SetLanguage(val)
...@@ -205,6 +206,9 @@ near the top of the file, before you open it in Visual Studio. ...@@ -205,6 +206,9 @@ near the top of the file, before you open it in Visual Studio.
if key == '-D': if key == '-D':
name, val = util.ParseDefine(val) name, val = util.ParseDefine(val)
self.defines[name] = val self.defines[name] = val
elif key == '--help':
self.ShowUsage()
sys.exit(0)
res_tree = grd_reader.Parse(opts.input, debug=opts.extra_verbose) res_tree = grd_reader.Parse(opts.input, debug=opts.extra_verbose)
res_tree.OnlyTheseTranslations([self.lang]) res_tree.OnlyTheseTranslations([self.lang])
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
'''The 'grit transl2tc' tool. '''The 'grit transl2tc' tool.
''' '''
import sys
from grit import grd_reader from grit import grd_reader
from grit import util from grit import util
...@@ -59,7 +60,7 @@ Bulk Translation Upload tool. ...@@ -59,7 +60,7 @@ Bulk Translation Upload tool.
if len(args) and args[0] == '-l': if len(args) and args[0] == '-l':
self.limits = util.ReadFile(args[1], util.RAW_TEXT).split('\n') self.limits = util.ReadFile(args[1], util.RAW_TEXT).split('\n')
args = args[2:] args = args[2:]
return self.rc2grd.ParseOptions(args) return self.rc2grd.ParseOptions(args, help_func=self.ShowUsage)
def Run(self, globopt, args): def Run(self, globopt, args):
args = self.Setup(globopt, args) args = self.Setup(globopt, args)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
'''GRIT tool that runs the unit test suite for GRIT.''' '''GRIT tool that runs the unit test suite for GRIT.'''
import getopt import getopt
import sys
import unittest import unittest
import grit.test_suite_all import grit.test_suite_all
...@@ -20,7 +21,11 @@ This happens in the environment that is set up by the basic GRIT runner.''' ...@@ -20,7 +21,11 @@ This happens in the environment that is set up by the basic GRIT runner.'''
def ParseOptions(self, args): def ParseOptions(self, args):
"""Set this objects and return all non-option arguments.""" """Set this objects and return all non-option arguments."""
own_opts, args = getopt.getopt(args, '') own_opts, args = getopt.getopt(args, '', ('help',))
for key, val in own_opts:
if key == '--help':
self.ShowUsage()
sys.exit(0)
return args return args
def Run(self, opts, args): def Run(self, opts, args):
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import getopt import getopt
import os import os
import sys
from xml.sax import saxutils from xml.sax import saxutils
...@@ -171,7 +172,7 @@ Other options: ...@@ -171,7 +172,7 @@ Other options:
limit_file = None limit_file = None
limit_is_grd = False limit_is_grd = False
limit_file_dir = None limit_file_dir = None
own_opts, args = getopt.getopt(args, 'l:D:ih') own_opts, args = getopt.getopt(args, 'l:D:ih', ('help',))
for key, val in own_opts: for key, val in own_opts:
if key == '-l': if key == '-l':
limit_file = open(val, 'r') limit_file = open(val, 'r')
...@@ -187,6 +188,9 @@ Other options: ...@@ -187,6 +188,9 @@ Other options:
elif key == '-E': elif key == '-E':
(env_name, env_value) = val.split('=', 1) (env_name, env_value) = val.split('=', 1)
os.environ[env_name] = env_value os.environ[env_name] = env_value
elif key == '--help':
self.ShowUsage()
sys.exit(0)
if not len(args) == 1: if not len(args) == 1:
print ('grit xmb takes exactly one argument, the path to the XMB file ' print ('grit xmb takes exactly one argument, the path to the XMB file '
'to output.') 'to output.')
......
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