2018-03-27 14:30:46 -04:00
|
|
|
"""Parser for command line arguments."""
|
2015-05-08 14:20:43 -04:00
|
|
|
|
2020-04-15 13:22:09 -04:00
|
|
|
import argparse
|
2020-06-17 17:41:54 +03:00
|
|
|
import shlex
|
2015-05-08 14:20:43 -04:00
|
|
|
|
2020-06-17 17:41:54 +03:00
|
|
|
from buildscripts.resmokelib import configure_resmoke
|
|
|
|
|
from buildscripts.resmokelib.hang_analyzer import HangAnalyzerPlugin
|
|
|
|
|
from buildscripts.resmokelib.run import RunPlugin
|
|
|
|
|
from buildscripts.resmokelib.undodb import UndoDbPlugin
|
2020-05-14 10:26:32 -04:00
|
|
|
|
|
|
|
|
_PLUGINS = [
|
|
|
|
|
RunPlugin(),
|
|
|
|
|
HangAnalyzerPlugin(),
|
|
|
|
|
UndoDbPlugin(),
|
|
|
|
|
]
|
2015-05-08 14:20:43 -04:00
|
|
|
|
2019-03-27 22:43:28 -04:00
|
|
|
|
2020-05-13 14:35:29 -04:00
|
|
|
def _add_subcommands():
|
2018-04-11 13:49:29 -04:00
|
|
|
"""Create and return the command line arguments parser."""
|
2020-04-15 13:22:09 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
subparsers = parser.add_subparsers(dest="command")
|
|
|
|
|
|
|
|
|
|
# Add sub-commands.
|
2020-05-14 10:26:32 -04:00
|
|
|
for plugin in _PLUGINS:
|
|
|
|
|
plugin.add_subcommand(subparsers)
|
2020-04-15 13:22:09 -04:00
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
2015-05-08 14:20:43 -04:00
|
|
|
|
2020-05-14 10:26:32 -04:00
|
|
|
def parse(sys_args):
|
2020-04-15 13:22:09 -04:00
|
|
|
"""Parse the CLI args."""
|
|
|
|
|
|
|
|
|
|
# Split out this function for easier testing.
|
2020-05-13 14:35:29 -04:00
|
|
|
parser = _add_subcommands()
|
2020-04-15 13:22:09 -04:00
|
|
|
parsed_args = parser.parse_args(sys_args)
|
|
|
|
|
|
|
|
|
|
return (parser, parsed_args)
|
2019-03-27 22:43:28 -04:00
|
|
|
|
2020-04-15 13:22:09 -04:00
|
|
|
|
|
|
|
|
def parse_command_line(sys_args, **kwargs):
|
|
|
|
|
"""Parse the command line arguments passed to resmoke.py and return the subcommand object to execute."""
|
2020-05-14 10:26:32 -04:00
|
|
|
parser, parsed_args = parse(sys_args)
|
2020-04-15 13:22:09 -04:00
|
|
|
|
2020-05-04 12:02:26 -04:00
|
|
|
subcommand = parsed_args.command
|
2020-05-13 14:35:29 -04:00
|
|
|
|
2020-05-14 10:26:32 -04:00
|
|
|
for plugin in _PLUGINS:
|
|
|
|
|
subcommand_obj = plugin.parse(subcommand, parser, parsed_args, **kwargs)
|
|
|
|
|
if subcommand_obj is not None:
|
|
|
|
|
return subcommand_obj
|
2020-05-04 12:02:26 -04:00
|
|
|
|
2020-05-14 10:26:32 -04:00
|
|
|
raise RuntimeError(f"Resmoke configuration has invalid subcommand: {subcommand}. Try '--help'")
|
2019-03-27 22:43:28 -04:00
|
|
|
|
2017-07-21 10:21:09 -04:00
|
|
|
|
2020-04-15 13:22:09 -04:00
|
|
|
def set_run_options(argstr=''):
|
|
|
|
|
"""Populate the config module variables for the 'run' subcommand with the default options."""
|
2020-05-14 10:26:32 -04:00
|
|
|
parser, parsed_args = parse(['run'] + shlex.split(argstr))
|
2020-04-15 13:22:09 -04:00
|
|
|
configure_resmoke.validate_and_update_config(parser, parsed_args)
|