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>
This commit is contained in:
parent
548d4332e8
commit
8e0a84c901
|
@ -35,6 +35,7 @@ import fnmatch
|
||||||
scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
|
scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
|
||||||
sys.path.insert(0, scripts_lib_path)
|
sys.path.insert(0, scripts_lib_path)
|
||||||
import scriptutils
|
import scriptutils
|
||||||
|
import argparse_oe
|
||||||
logger = scriptutils.logger_create('devtool-stress')
|
logger = scriptutils.logger_create('devtool-stress')
|
||||||
|
|
||||||
def select_recipes(args):
|
def select_recipes(args):
|
||||||
|
@ -204,8 +205,8 @@ def stress_modify(args):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="devtool stress tester",
|
parser = argparse_oe.ArgumentParser(description="devtool stress tester",
|
||||||
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
||||||
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
||||||
parser.add_argument('-r', '--resume-from', help='Resume from specified recipe', metavar='PN')
|
parser.add_argument('-r', '--resume-from', help='Resume from specified recipe', metavar='PN')
|
||||||
parser.add_argument('-o', '--only', help='Only test specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST')
|
parser.add_argument('-o', '--only', help='Only test specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST')
|
||||||
|
|
|
@ -37,6 +37,7 @@ lib_path = scripts_path + '/lib'
|
||||||
sys.path = sys.path + [lib_path]
|
sys.path = sys.path + [lib_path]
|
||||||
from devtool import DevtoolError, setup_tinfoil
|
from devtool import DevtoolError, setup_tinfoil
|
||||||
import scriptutils
|
import scriptutils
|
||||||
|
import argparse_oe
|
||||||
logger = scriptutils.logger_create('devtool')
|
logger = scriptutils.logger_create('devtool')
|
||||||
|
|
||||||
plugins = []
|
plugins = []
|
||||||
|
@ -185,9 +186,9 @@ def main():
|
||||||
break
|
break
|
||||||
pth = os.path.dirname(pth)
|
pth = os.path.dirname(pth)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="OpenEmbedded development tool",
|
parser = argparse_oe.ArgumentParser(description="OpenEmbedded development tool",
|
||||||
add_help=False,
|
add_help=False,
|
||||||
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
||||||
parser.add_argument('--basepath', help='Base directory of SDK / build directory')
|
parser.add_argument('--basepath', help='Base directory of SDK / build directory')
|
||||||
parser.add_argument('--bbpath', help='Explicitly specify the BBPATH, rather than getting it from the metadata')
|
parser.add_argument('--bbpath', help='Explicitly specify the BBPATH, rather than getting it from the metadata')
|
||||||
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
class ArgumentParser(argparse.ArgumentParser):
|
||||||
|
"""Our own version of argparse's ArgumentParser"""
|
||||||
|
|
||||||
|
def error(self, message):
|
||||||
|
sys.stderr.write('ERROR: %s\n' % message)
|
||||||
|
self.print_help()
|
||||||
|
sys.exit(2)
|
||||||
|
|
|
@ -19,6 +19,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import glob
|
import glob
|
||||||
|
import argparse
|
||||||
|
|
||||||
def logger_create(name):
|
def logger_create(name):
|
||||||
logger = logging.getLogger(name)
|
logger = logging.getLogger(name)
|
||||||
|
|
|
@ -33,6 +33,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
lib_path = scripts_path + '/lib'
|
lib_path = scripts_path + '/lib'
|
||||||
sys.path = sys.path + [lib_path]
|
sys.path = sys.path + [lib_path]
|
||||||
import scriptutils
|
import scriptutils
|
||||||
|
import argparse_oe
|
||||||
logger = scriptutils.logger_create('pkgdatautil')
|
logger = scriptutils.logger_create('pkgdatautil')
|
||||||
|
|
||||||
def tinfoil_init():
|
def tinfoil_init():
|
||||||
|
@ -417,8 +418,8 @@ def find_path(args):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package",
|
parser = argparse_oe.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package",
|
||||||
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
||||||
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
||||||
parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)')
|
parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)')
|
||||||
subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
|
subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
|
||||||
|
|
|
@ -24,6 +24,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
lib_path = scripts_path + '/lib'
|
lib_path = scripts_path + '/lib'
|
||||||
sys.path = sys.path + [lib_path]
|
sys.path = sys.path + [lib_path]
|
||||||
import scriptutils
|
import scriptutils
|
||||||
|
import argparse_oe
|
||||||
logger = scriptutils.logger_create('sdktool')
|
logger = scriptutils.logger_create('sdktool')
|
||||||
|
|
||||||
def mkdir(d):
|
def mkdir(d):
|
||||||
|
@ -113,8 +114,8 @@ def publish(args):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="OpenEmbedded development tool",
|
parser = argparse_oe.ArgumentParser(description="OpenEmbedded development tool",
|
||||||
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
||||||
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
||||||
parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
|
parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/lib')
|
||||||
import scriptpath
|
import scriptpath
|
||||||
scriptpath.add_bitbake_lib_path()
|
scriptpath.add_bitbake_lib_path()
|
||||||
scriptpath.add_oe_lib_path()
|
scriptpath.add_oe_lib_path()
|
||||||
|
import argparse_oe
|
||||||
|
|
||||||
import oeqa.selftest
|
import oeqa.selftest
|
||||||
import oeqa.utils.ftools as ftools
|
import oeqa.utils.ftools as ftools
|
||||||
|
@ -65,7 +66,7 @@ log = logger_create()
|
||||||
|
|
||||||
def get_args_parser():
|
def get_args_parser():
|
||||||
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."
|
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."
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse_oe.ArgumentParser(description=description)
|
||||||
group = parser.add_mutually_exclusive_group(required=True)
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
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>')
|
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>')
|
||||||
group.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False, help='Run all (unhidden) tests')
|
group.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False, help='Run all (unhidden) tests')
|
||||||
|
|
|
@ -27,6 +27,7 @@ scripts_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
lib_path = scripts_path + '/lib'
|
lib_path = scripts_path + '/lib'
|
||||||
sys.path = sys.path + [lib_path]
|
sys.path = sys.path + [lib_path]
|
||||||
import scriptutils
|
import scriptutils
|
||||||
|
import argparse_oe
|
||||||
logger = scriptutils.logger_create('recipetool')
|
logger = scriptutils.logger_create('recipetool')
|
||||||
|
|
||||||
plugins = []
|
plugins = []
|
||||||
|
@ -45,9 +46,9 @@ def main():
|
||||||
logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)")
|
logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="OpenEmbedded recipe tool",
|
parser = argparse_oe.ArgumentParser(description="OpenEmbedded recipe tool",
|
||||||
add_help=False,
|
add_help=False,
|
||||||
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
|
||||||
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
|
||||||
parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
|
parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
|
||||||
parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR')
|
parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR')
|
||||||
|
|
|
@ -15,6 +15,10 @@ import subprocess
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
scripts_lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')
|
||||||
|
sys.path.insert(0, scripts_lib_path)
|
||||||
|
import argparse_oe
|
||||||
|
|
||||||
version = "0.3"
|
version = "0.3"
|
||||||
|
|
||||||
log = logging.getLogger("send-error-report")
|
log = logging.getLogger("send-error-report")
|
||||||
|
@ -143,7 +147,7 @@ def send_data(data, args):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
arg_parse = argparse.ArgumentParser(description="This scripts will send an error report to your specified error-report-web server.")
|
arg_parse = argparse_oe.ArgumentParser(description="This scripts will send an error report to your specified error-report-web server.")
|
||||||
|
|
||||||
arg_parse.add_argument("error_file",
|
arg_parse.add_argument("error_file",
|
||||||
help="Generated error report file location",
|
help="Generated error report file location",
|
||||||
|
|
|
@ -38,6 +38,7 @@ lib_path = scripts_path + '/lib'
|
||||||
sys.path = sys.path + [lib_path]
|
sys.path = sys.path + [lib_path]
|
||||||
|
|
||||||
import scriptpath
|
import scriptpath
|
||||||
|
import argparse_oe
|
||||||
|
|
||||||
# Add meta/lib to sys.path
|
# Add meta/lib to sys.path
|
||||||
scriptpath.add_oe_lib_path()
|
scriptpath.add_oe_lib_path()
|
||||||
|
@ -82,7 +83,7 @@ log = logger_create()
|
||||||
# Define and return the arguments parser for the script
|
# Define and return the arguments parser for the script
|
||||||
def get_args_parser():
|
def get_args_parser():
|
||||||
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."
|
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."
|
||||||
parser = argparse.ArgumentParser(description=description)
|
parser = argparse_oe.ArgumentParser(description=description)
|
||||||
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).')
|
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).')
|
||||||
parser.add_argument('--repo-link', required=True, action="store", type=str, dest="repo_link", default=None, help='The link to the remote images repository.')
|
parser.add_argument('--repo-link', required=True, action="store", type=str, dest="repo_link", default=None, help='The link to the remote images repository.')
|
||||||
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.')
|
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.')
|
||||||
|
|
Loading…
Reference in New Issue