summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggleton <paul.eggleton@linux.intel.com>2015-12-22 17:02:54 +1300
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-12-28 09:25:12 +0000
commit8e0a84c90175cc2fedff0272abda073ee270b876 (patch)
treecc5e7a7778ffe12e0376cb7c06f1c1d69b79dfe9
parent548d4332e827cc9ae13fd57517a42fa0f139880f (diff)
downloadpoky-8e0a84c90175cc2fedff0272abda073ee270b876.tar.gz
scripts: print usage in argparse-using scripts when a command-line error occurs
For scripts that use Python's standard argparse module to parse command-line arguments, create a subclass which will show the usage the usage information when a command-line parsing error occurs. The most common case would be when the script is run with no arguments; at least then the user immediately gets to see what arguments they might need to pass instead of just an error message. (From OE-Core rev: d62fe7c9bc2df6a4464440a3cae0539074bf99aa) Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rwxr-xr-xscripts/contrib/devtool-stress.py5
-rwxr-xr-xscripts/devtool7
-rw-r--r--scripts/lib/argparse_oe.py11
-rw-r--r--scripts/lib/scriptutils.py1
-rwxr-xr-xscripts/oe-pkgdata-util5
-rwxr-xr-xscripts/oe-publish-sdk5
-rwxr-xr-xscripts/oe-selftest3
-rwxr-xr-xscripts/recipetool7
-rwxr-xr-xscripts/send-error-report6
-rwxr-xr-xscripts/test-remote-image3
10 files changed, 38 insertions, 15 deletions
diff --git a/scripts/contrib/devtool-stress.py b/scripts/contrib/devtool-stress.py
index 4b35fc9d0e..8cf92ca2fe 100755
--- a/scripts/contrib/devtool-stress.py
+++ b/scripts/contrib/devtool-stress.py
@@ -35,6 +35,7 @@ import fnmatch
35scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib')) 35scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
36sys.path.insert(0, scripts_lib_path) 36sys.path.insert(0, scripts_lib_path)
37import scriptutils 37import scriptutils
38import argparse_oe
38logger = scriptutils.logger_create('devtool-stress') 39logger = scriptutils.logger_create('devtool-stress')
39 40
40def select_recipes(args): 41def select_recipes(args):
@@ -204,8 +205,8 @@ def stress_modify(args):
204 205
205 206
206def main(): 207def main():
207 parser = argparse.ArgumentParser(description="devtool stress tester", 208 parser = argparse_oe.ArgumentParser(description="devtool stress tester",
208 epilog="Use %(prog)s <subcommand> --help to get help on a specific command") 209 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
209 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') 210 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
210 parser.add_argument('-r', '--resume-from', help='Resume from specified recipe', metavar='PN') 211 parser.add_argument('-r', '--resume-from', help='Resume from specified recipe', metavar='PN')
211 parser.add_argument('-o', '--only', help='Only test specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST') 212 parser.add_argument('-o', '--only', help='Only test specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST')
diff --git a/scripts/devtool b/scripts/devtool
index 9d3287c6d3..955495ea33 100755
--- a/scripts/devtool
+++ b/scripts/devtool
@@ -37,6 +37,7 @@ lib_path = scripts_path + '/lib'
37sys.path = sys.path + [lib_path] 37sys.path = sys.path + [lib_path]
38from devtool import DevtoolError, setup_tinfoil 38from devtool import DevtoolError, setup_tinfoil
39import scriptutils 39import scriptutils
40import argparse_oe
40logger = scriptutils.logger_create('devtool') 41logger = scriptutils.logger_create('devtool')
41 42
42plugins = [] 43plugins = []
@@ -185,9 +186,9 @@ def main():
185 break 186 break
186 pth = os.path.dirname(pth) 187 pth = os.path.dirname(pth)
187 188
188 parser = argparse.ArgumentParser(description="OpenEmbedded development tool", 189 parser = argparse_oe.ArgumentParser(description="OpenEmbedded development tool",
189 add_help=False, 190 add_help=False,
190 epilog="Use %(prog)s <subcommand> --help to get help on a specific command") 191 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
191 parser.add_argument('--basepath', help='Base directory of SDK / build directory') 192 parser.add_argument('--basepath', help='Base directory of SDK / build directory')
192 parser.add_argument('--bbpath', help='Explicitly specify the BBPATH, rather than getting it from the metadata') 193 parser.add_argument('--bbpath', help='Explicitly specify the BBPATH, rather than getting it from the metadata')
193 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') 194 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
diff --git a/scripts/lib/argparse_oe.py b/scripts/lib/argparse_oe.py
new file mode 100644
index 0000000000..c2fee6de05
--- /dev/null
+++ b/scripts/lib/argparse_oe.py
@@ -0,0 +1,11 @@
1import sys
2import argparse
3
4class ArgumentParser(argparse.ArgumentParser):
5 """Our own version of argparse's ArgumentParser"""
6
7 def error(self, message):
8 sys.stderr.write('ERROR: %s\n' % message)
9 self.print_help()
10 sys.exit(2)
11
diff --git a/scripts/lib/scriptutils.py b/scripts/lib/scriptutils.py
index 3366882635..4dd7ef2a0d 100644
--- a/scripts/lib/scriptutils.py
+++ b/scripts/lib/scriptutils.py
@@ -19,6 +19,7 @@ import sys
19import os 19import os
20import logging 20import logging
21import glob 21import glob
22import argparse
22 23
23def logger_create(name): 24def logger_create(name):
24 logger = logging.getLogger(name) 25 logger = logging.getLogger(name)
diff --git a/scripts/oe-pkgdata-util b/scripts/oe-pkgdata-util
index afdceaae29..8e22e020e7 100755
--- a/scripts/oe-pkgdata-util
+++ b/scripts/oe-pkgdata-util
@@ -33,6 +33,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
33lib_path = scripts_path + '/lib' 33lib_path = scripts_path + '/lib'
34sys.path = sys.path + [lib_path] 34sys.path = sys.path + [lib_path]
35import scriptutils 35import scriptutils
36import argparse_oe
36logger = scriptutils.logger_create('pkgdatautil') 37logger = scriptutils.logger_create('pkgdatautil')
37 38
38def tinfoil_init(): 39def tinfoil_init():
@@ -417,8 +418,8 @@ def find_path(args):
417 418
418 419
419def main(): 420def main():
420 parser = argparse.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package", 421 parser = argparse_oe.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package",
421 epilog="Use %(prog)s <subcommand> --help to get help on a specific command") 422 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
422 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') 423 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
423 parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)') 424 parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)')
424 subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') 425 subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
diff --git a/scripts/oe-publish-sdk b/scripts/oe-publish-sdk
index c4c35bda11..c8c79c213c 100755
--- a/scripts/oe-publish-sdk
+++ b/scripts/oe-publish-sdk
@@ -24,6 +24,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
24lib_path = scripts_path + '/lib' 24lib_path = scripts_path + '/lib'
25sys.path = sys.path + [lib_path] 25sys.path = sys.path + [lib_path]
26import scriptutils 26import scriptutils
27import argparse_oe
27logger = scriptutils.logger_create('sdktool') 28logger = scriptutils.logger_create('sdktool')
28 29
29def mkdir(d): 30def mkdir(d):
@@ -113,8 +114,8 @@ def publish(args):
113 114
114 115
115def main(): 116def main():
116 parser = argparse.ArgumentParser(description="OpenEmbedded development tool", 117 parser = argparse_oe.ArgumentParser(description="OpenEmbedded development tool",
117 epilog="Use %(prog)s <subcommand> --help to get help on a specific command") 118 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
118 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') 119 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
119 parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') 120 parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
120 121
diff --git a/scripts/oe-selftest b/scripts/oe-selftest
index bc50b2a435..f989e87010 100755
--- a/scripts/oe-selftest
+++ b/scripts/oe-selftest
@@ -36,6 +36,7 @@ sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/lib')
36import scriptpath 36import scriptpath
37scriptpath.add_bitbake_lib_path() 37scriptpath.add_bitbake_lib_path()
38scriptpath.add_oe_lib_path() 38scriptpath.add_oe_lib_path()
39import argparse_oe
39 40
40import oeqa.selftest 41import oeqa.selftest
41import oeqa.utils.ftools as ftools 42import oeqa.utils.ftools as ftools
@@ -65,7 +66,7 @@ log = logger_create()
65 66
66def get_args_parser(): 67def get_args_parser():
67 description = "Script that runs unit tests agains bitbake and other Yocto related tools. The goal is to validate tools functionality and metadata integrity. Refer to https://wiki.yoctoproject.org/wiki/Oe-selftest for more information." 68 description = "Script that runs unit tests agains bitbake and other Yocto related tools. The goal is to validate tools functionality and metadata integrity. Refer to https://wiki.yoctoproject.org/wiki/Oe-selftest for more information."
68 parser = argparse.ArgumentParser(description=description) 69 parser = argparse_oe.ArgumentParser(description=description)
69 group = parser.add_mutually_exclusive_group(required=True) 70 group = parser.add_mutually_exclusive_group(required=True)
70 group.add_argument('--run-tests', required=False, action='store', nargs='*', dest="run_tests", default=None, help='Select what tests to run (modules, classes or test methods). Format should be: <module>.<class>.<test_method>') 71 group.add_argument('--run-tests', required=False, action='store', nargs='*', dest="run_tests", default=None, help='Select what tests to run (modules, classes or test methods). Format should be: <module>.<class>.<test_method>')
71 group.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False, help='Run all (unhidden) tests') 72 group.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False, help='Run all (unhidden) tests')
diff --git a/scripts/recipetool b/scripts/recipetool
index 791a66aaca..1198cc25d7 100755
--- a/scripts/recipetool
+++ b/scripts/recipetool
@@ -27,6 +27,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
27lib_path = scripts_path + '/lib' 27lib_path = scripts_path + '/lib'
28sys.path = sys.path + [lib_path] 28sys.path = sys.path + [lib_path]
29import scriptutils 29import scriptutils
30import argparse_oe
30logger = scriptutils.logger_create('recipetool') 31logger = scriptutils.logger_create('recipetool')
31 32
32plugins = [] 33plugins = []
@@ -45,9 +46,9 @@ def main():
45 logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)") 46 logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)")
46 sys.exit(1) 47 sys.exit(1)
47 48
48 parser = argparse.ArgumentParser(description="OpenEmbedded recipe tool", 49 parser = argparse_oe.ArgumentParser(description="OpenEmbedded recipe tool",
49 add_help=False, 50 add_help=False,
50 epilog="Use %(prog)s <subcommand> --help to get help on a specific command") 51 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
51 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') 52 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
52 parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') 53 parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
53 parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR') 54 parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR')
diff --git a/scripts/send-error-report b/scripts/send-error-report
index 1a1b96580d..a29feff325 100755
--- a/scripts/send-error-report
+++ b/scripts/send-error-report
@@ -15,6 +15,10 @@ import subprocess
15import argparse 15import argparse
16import logging 16import logging
17 17
18scripts_lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')
19sys.path.insert(0, scripts_lib_path)
20import argparse_oe
21
18version = "0.3" 22version = "0.3"
19 23
20log = logging.getLogger("send-error-report") 24log = logging.getLogger("send-error-report")
@@ -143,7 +147,7 @@ def send_data(data, args):
143 147
144 148
145if __name__ == '__main__': 149if __name__ == '__main__':
146 arg_parse = argparse.ArgumentParser(description="This scripts will send an error report to your specified error-report-web server.") 150 arg_parse = argparse_oe.ArgumentParser(description="This scripts will send an error report to your specified error-report-web server.")
147 151
148 arg_parse.add_argument("error_file", 152 arg_parse.add_argument("error_file",
149 help="Generated error report file location", 153 help="Generated error report file location",
diff --git a/scripts/test-remote-image b/scripts/test-remote-image
index f3a44ebe51..97d03d7a78 100755
--- a/scripts/test-remote-image
+++ b/scripts/test-remote-image
@@ -38,6 +38,7 @@ lib_path = scripts_path + '/lib'
38sys.path = sys.path + [lib_path] 38sys.path = sys.path + [lib_path]
39 39
40import scriptpath 40import scriptpath
41import argparse_oe
41 42
42# Add meta/lib to sys.path 43# Add meta/lib to sys.path
43scriptpath.add_oe_lib_path() 44scriptpath.add_oe_lib_path()
@@ -82,7 +83,7 @@ log = logger_create()
82# Define and return the arguments parser for the script 83# Define and return the arguments parser for the script
83def get_args_parser(): 84def get_args_parser():
84 description = "This script is used to run automated runtime tests using remotely published image files. You should prepare the build environment just like building local images and running the tests." 85 description = "This script is used to run automated runtime tests using remotely published image files. You should prepare the build environment just like building local images and running the tests."
85 parser = argparse.ArgumentParser(description=description) 86 parser = argparse_oe.ArgumentParser(description=description)
86 parser.add_argument('--image-types', required=True, action="store", nargs='*', dest="image_types", default=None, help='The image types to test(ex: core-image-minimal).') 87 parser.add_argument('--image-types', required=True, action="store", nargs='*', dest="image_types", default=None, help='The image types to test(ex: core-image-minimal).')
87 parser.add_argument('--repo-link', required=True, action="store", type=str, dest="repo_link", default=None, help='The link to the remote images repository.') 88 parser.add_argument('--repo-link', required=True, action="store", type=str, dest="repo_link", default=None, help='The link to the remote images repository.')
88 parser.add_argument('--required-packages', required=False, action="store", nargs='*', dest="required_packages", default=None, help='Required packages for the tests. They will be built before the testing begins.') 89 parser.add_argument('--required-packages', required=False, action="store", nargs='*', dest="required_packages", default=None, help='Required packages for the tests. They will be built before the testing begins.')