twister: Split up argument parsing into smaller functions

Split argument parsing into smaller functions to be leveraged
by west twister extension.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
This commit is contained in:
Al Semjonovs 2022-09-26 15:38:00 -06:00 committed by Marti Bolivar
commit 067ba65e10
3 changed files with 19 additions and 12 deletions

View file

@ -48,10 +48,11 @@ except subprocess.CalledProcessError as e:
canonical_zephyr_base = os.path.realpath(ZEPHYR_BASE)
canonical_topdir = os.path.realpath(topdir)
def parse_arguments(args):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
def add_parse_arguments(parser = None):
if parser is None:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.fromfile_prefix_chars = "+"
case_select = parser.add_argument_group("Test case selection",
@ -637,7 +638,12 @@ structure in the main Zephyr tree: boards/<arch>/<board_name>/""")
parser.add_argument("extra_test_args", nargs=argparse.REMAINDER,
help="Additional args following a '--' are passed to the test binary")
options = parser.parse_args(args)
return parser
def parse_arguments(parser, args, options = None):
if options is None:
options = parser.parse_args(args)
# Very early error handling
if options.short_build_path and not options.ninja:

View file

@ -14,7 +14,7 @@ sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts"))
from twisterlib.testplan import TestPlan
from twisterlib.testinstance import TestInstance
from twisterlib.environment import TwisterEnv, parse_arguments
from twisterlib.environment import TwisterEnv, add_parse_arguments, parse_arguments
def new_get_toolchain(*args, **kwargs):
return 'zephyr'
@ -35,7 +35,8 @@ def testsuites_directory():
@pytest.fixture(name='class_env')
def tesenv_obj(test_data, testsuites_dir, tmpdir_factory):
""" Pytest fixture to initialize and return the class TestPlan object"""
options = parse_arguments([])
parser = add_parse_arguments()
options = parse_arguments(parser, [])
env = TwisterEnv(options)
env.board_roots = [test_data +"board_config/1_level/2_level/"]
env.test_roots = [testsuites_dir + '/tests', testsuites_dir + '/samples']

View file

@ -196,7 +196,7 @@ sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/"))
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/build_helpers"))
from twisterlib.testplan import TestPlan
from twisterlib.environment import TwisterEnv, parse_arguments
from twisterlib.environment import TwisterEnv, add_parse_arguments, parse_arguments
from twisterlib.reports import Reporting
from twisterlib.hardwaremap import HardwareMap
from twisterlib.coverage import run_coverage
@ -241,11 +241,9 @@ def setup_logging(outdir, log_file, verbose, timestamps):
def init_color(colorama_strip):
colorama.init(strip=colorama_strip)
def main():
def main(options):
start_time = time.time()
options = parse_arguments(sys.argv[1:])
# Configure color output
color_strip = False if options.force_color else None
@ -413,7 +411,9 @@ def main():
if __name__ == "__main__":
ret = 0
try:
ret = main()
parser = add_parse_arguments()
options = parse_arguments(parser, sys.argv[1:])
ret = main(options)
finally:
if (os.name != "nt") and os.isatty(1):
# (OS is not Windows) and (stdout is interactive)