diff --git a/tools/build.py b/tools/build.py index e47cd6a411..e021f6ff2b 100644 --- a/tools/build.py +++ b/tools/build.py @@ -42,115 +42,115 @@ if __name__ == '__main__': # Parse Options parser = get_default_options_parser() - parser.add_option("--source", dest="source_dir", + parser.add_argument("--source", dest="source_dir", default=None, help="The source (input) directory", action="append") - parser.add_option("--build", dest="build_dir", + parser.add_argument("--build", dest="build_dir", default=None, help="The build (output) directory") - parser.add_option("--no-archive", dest="no_archive", action="store_true", + parser.add_argument("--no-archive", dest="no_archive", action="store_true", default=False, help="Do not produce archive (.ar) file, but rather .o") # Extra libraries - parser.add_option("-r", "--rtos", + parser.add_argument("-r", "--rtos", action="store_true", dest="rtos", default=False, help="Compile the rtos") - parser.add_option("--rpc", + parser.add_argument("--rpc", action="store_true", dest="rpc", default=False, help="Compile the rpc library") - parser.add_option("-e", "--eth", + parser.add_argument("-e", "--eth", action="store_true", dest="eth", default=False, help="Compile the ethernet library") - parser.add_option("-U", "--usb_host", + parser.add_argument("-U", "--usb_host", action="store_true", dest="usb_host", default=False, help="Compile the USB Host library") - parser.add_option("-u", "--usb", + parser.add_argument("-u", "--usb", action="store_true", dest="usb", default=False, help="Compile the USB Device library") - parser.add_option("-d", "--dsp", + parser.add_argument("-d", "--dsp", action="store_true", dest="dsp", default=False, help="Compile the DSP library") - parser.add_option("-F", "--fat", + parser.add_argument("-F", "--fat", action="store_true", dest="fat", default=False, help="Compile FS and SD card file system library") - parser.add_option("-b", "--ublox", + parser.add_argument("-b", "--ublox", action="store_true", dest="ublox", default=False, help="Compile the u-blox library") - parser.add_option("", "--cpputest", + parser.add_argument( "--cpputest", action="store_true", dest="cpputest_lib", default=False, help="Compiles 'cpputest' unit test library (library should be on the same directory level as mbed repository)") - parser.add_option("-D", "", + parser.add_argument("-D", action="append", dest="macros", help="Add a macro definition") - parser.add_option("-S", "--supported-toolchains", + parser.add_argument("-S", "--supported-toolchains", action="store_true", dest="supported_toolchains", default=False, help="Displays supported matrix of MCUs and toolchains") - parser.add_option('-f', '--filter', + parser.add_argument('-f', '--filter', dest='general_filter_regex', default=None, help='For some commands you can use filter to filter out results') - parser.add_option("", "--cppcheck", + parser.add_argument("--cppcheck", action="store_true", dest="cppcheck_validation", default=False, help="Forces 'cppcheck' static code analysis") - parser.add_option("-j", "--jobs", type="int", dest="jobs", + parser.add_argument("-j", "--jobs", type=int, dest="jobs", default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") - parser.add_option("-N", "--artifact-name", dest="artifact_name", + parser.add_argument("-N", "--artifact-name", dest="artifact_name", default=None, help="The built project's name") - parser.add_option("-v", "--verbose", + parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - parser.add_option("--silent", + parser.add_argument("--silent", action="store_true", dest="silent", default=False, help="Silent diagnostic output (no copy, compile notification)") - parser.add_option("-x", "--extra-verbose-notifications", + parser.add_argument("-x", "--extra-verbose-notifications", action="store_true", dest="extra_verbose_notify", default=False, help="Makes compiler more verbose, CI friendly.") - (options, args) = parser.parse_args() + options = parser.parse_args() # Only prints matrix of supported toolchains if options.supported_toolchains: diff --git a/tools/detect_targets.py b/tools/detect_targets.py index 1cf288dede..dd7be0ab91 100644 --- a/tools/detect_targets.py +++ b/tools/detect_targets.py @@ -42,24 +42,24 @@ if __name__ == '__main__': # Parse Options parser = get_default_options_parser() - parser.add_option("-S", "--supported-toolchains", + parser.add_argument("-S", "--supported-toolchains", action="store_true", dest="supported_toolchains", default=False, help="Displays supported matrix of targets and toolchains") - parser.add_option('-f', '--filter', + parser.add_argument('-f', '--filter', dest='general_filter_regex', default=None, help='Filter targets') - parser.add_option("-v", "--verbose", + parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - (options, args) = parser.parse_args() + options = parser.parse_args() # Only prints matrix of supported toolchains if options.supported_toolchains: diff --git a/tools/get_config.py b/tools/get_config.py index 9d9e7dc5fa..0a57426bd7 100644 --- a/tools/get_config.py +++ b/tools/get_config.py @@ -36,14 +36,14 @@ except: if __name__ == '__main__': # Parse Options parser = get_default_options_parser(add_clean=False, add_options=False) - parser.add_option("--source", dest="source_dir", + parser.add_argument("--source", dest="source_dir", default=None, help="The source (input) directory", action="append") - parser.add_option("--prefix", dest="prefix", action="append", + parser.add_argument("--prefix", dest="prefix", action="append", default=None, help="Restrict listing to parameters that have this prefix") - parser.add_option("-v", "--verbose", action="store_true", dest="verbose", + parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - (options, args) = parser.parse_args() + options = parser.parse_args() for path in options.source_dir : if not isdir(path) : diff --git a/tools/make.py b/tools/make.py index 7bc749985e..b895ad496d 100644 --- a/tools/make.py +++ b/tools/make.py @@ -50,133 +50,133 @@ except: if __name__ == '__main__': # Parse Options parser = get_default_options_parser() - parser.add_option("-p", - type="int", + parser.add_argument("-p", + type=int, dest="program", help="The index of the desired test program: [0-%d]" % (len(TESTS)-1)) - parser.add_option("-n", + parser.add_argument("-n", dest="program_name", help="The name of the desired test program") - parser.add_option("-j", "--jobs", - type="int", + parser.add_argument("-j", "--jobs", + type=int, dest="jobs", default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") - parser.add_option("-v", "--verbose", + parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - parser.add_option("--silent", + parser.add_argument("--silent", action="store_true", dest="silent", default=False, help="Silent diagnostic output (no copy, compile notification)") - parser.add_option("-D", "", + parser.add_argument("-D", action="append", dest="macros", help="Add a macro definition") - parser.add_option("-S", "--supported-toolchains", + parser.add_argument("-S", "--supported-toolchains", action="store_true", dest="supported_toolchains", default=False, help="Displays supported matrix of MCUs and toolchains") - parser.add_option('-f', '--filter', + parser.add_argument('-f', '--filter', dest='general_filter_regex', default=None, help='For some commands you can use filter to filter out results') # Local run - parser.add_option("--automated", action="store_true", dest="automated", + parser.add_argument("--automated", action="store_true", dest="automated", default=False, help="Automated test") - parser.add_option("--host", dest="host_test", + parser.add_argument("--host", dest="host_test", default=None, help="Host test") - parser.add_option("--extra", dest="extra", + parser.add_argument("--extra", dest="extra", default=None, help="Extra files") - parser.add_option("--peripherals", dest="peripherals", + parser.add_argument("--peripherals", dest="peripherals", default=None, help="Required peripherals") - parser.add_option("--dep", dest="dependencies", + parser.add_argument("--dep", dest="dependencies", default=None, help="Dependencies") - parser.add_option("--source", dest="source_dir", + parser.add_argument("--source", dest="source_dir", default=None, help="The source (input) directory", action="append") - parser.add_option("--duration", type="int", dest="duration", + parser.add_argument("--duration", type=int, dest="duration", default=None, help="Duration of the test") - parser.add_option("--build", dest="build_dir", + parser.add_argument("--build", dest="build_dir", default=None, help="The build (output) directory") - parser.add_option("-N", "--artifact-name", dest="artifact_name", + parser.add_argument("-N", "--artifact-name", dest="artifact_name", default=None, help="The built project's name") - parser.add_option("-d", "--disk", dest="disk", + parser.add_argument("-d", "--disk", dest="disk", default=None, help="The mbed disk") - parser.add_option("-s", "--serial", dest="serial", + parser.add_argument("-s", "--serial", dest="serial", default=None, help="The mbed serial port") - parser.add_option("-b", "--baud", type="int", dest="baud", + parser.add_argument("-b", "--baud", type=int, dest="baud", default=None, help="The mbed serial baud rate") - parser.add_option("-L", "--list-tests", action="store_true", dest="list_tests", + parser.add_argument("-L", "--list-tests", action="store_true", dest="list_tests", default=False, help="List available tests in order and exit") # Ideally, all the tests with a single "main" thread can be run with, or # without the rtos, eth, usb_host, usb, dsp, fat, ublox - parser.add_option("--rtos", + parser.add_argument("--rtos", action="store_true", dest="rtos", default=False, help="Link with RTOS library") - parser.add_option("--rpc", + parser.add_argument("--rpc", action="store_true", dest="rpc", default=False, help="Link with RPC library") - parser.add_option("--eth", + parser.add_argument("--eth", action="store_true", dest="eth", default=False, help="Link with Ethernet library") - parser.add_option("--usb_host", + parser.add_argument("--usb_host", action="store_true", dest="usb_host", default=False, help="Link with USB Host library") - parser.add_option("--usb", + parser.add_argument("--usb", action="store_true", dest="usb", default=False, help="Link with USB Device library") - parser.add_option("--dsp", + parser.add_argument("--dsp", action="store_true", dest="dsp", default=False, help="Link with DSP library") - parser.add_option("--fat", + parser.add_argument("--fat", action="store_true", dest="fat", default=False, help="Link with FS ad SD card file system library") - parser.add_option("--ublox", + parser.add_argument("--ublox", action="store_true", dest="ublox", default=False, help="Link with U-Blox library") - parser.add_option("--testlib", + parser.add_argument("--testlib", action="store_true", dest="testlib", default=False, help="Link with mbed test library") # Specify a different linker script - parser.add_option("-l", "--linker", dest="linker_script", + parser.add_argument("-l", "--linker", dest="linker_script", default=None, help="use the specified linker script") - (options, args) = parser.parse_args() + options = parser.parse_args() # Only prints matrix of supported toolchains if options.supported_toolchains: diff --git a/tools/options.py b/tools/options.py index 250e8e1948..d4e338b90d 100644 --- a/tools/options.py +++ b/tools/options.py @@ -14,33 +14,37 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -from optparse import OptionParser +from argparse import ArgumentParser from tools.toolchains import TOOLCHAINS from tools.targets import TARGET_NAMES +from utils import argparse_uppercase_type, argparse_lowercase_hyphen_type def get_default_options_parser(add_clean=True, add_options=True): - parser = OptionParser() + parser = ArgumentParser() targetnames = TARGET_NAMES targetnames.sort() toolchainlist = list(TOOLCHAINS) toolchainlist.sort() - parser.add_option("-m", "--mcu", - help="build for the given MCU (%s)" % ', '.join(targetnames), - metavar="MCU") + parser.add_argument("-m", "--mcu", + help="build for the given MCU (%s)" % ', '.join(targetnames), + metavar="MCU", + type=argparse_uppercase_type(targetnames, "MCU")) - parser.add_option("-t", "--tool", - help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist), - metavar="TOOLCHAIN") + parser.add_argument("-t", "--tool", + help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist), + metavar="TOOLCHAIN", + type=argparse_uppercase_type(toolchainlist, "toolchain")) if add_clean: - parser.add_option("-c", "--clean", action="store_true", default=False, + parser.add_argument("-c", "--clean", action="store_true", default=False, help="clean the build directory") if add_options: - parser.add_option("-o", "--options", action="append", - help='Add a build option ("save-asm": save the asm generated by the compiler, "debug-info": generate debugging information, "analyze": run Goanna static code analyzer")') + parser.add_argument("-o", "--options", action="append", + help='Add a build argument ("save-asm": save the asm generated by the compiler, "debug-info": generate debugging information, "analyze": run Goanna static code analyzer")', + type=argparse_lowercase_hyphen_type(['save-asm', 'debug-info', 'analyze'], "build option")) return parser diff --git a/tools/test.py b/tools/test.py index bdc9bb9c9d..3231a4da29 100644 --- a/tools/test.py +++ b/tools/test.py @@ -32,60 +32,61 @@ from tools.build_api import build_project, build_library from tools.targets import TARGET_MAP from tools.utils import mkdir, ToolException, NotSupportedException from tools.test_exporters import ReportExporter, ResultExporterType +from utils import argparse_lowercase_type if __name__ == '__main__': try: # Parse Options parser = get_default_options_parser() - parser.add_option("-D", "", + parser.add_argument("-D", "", action="append", dest="macros", help="Add a macro definition") - parser.add_option("-j", "--jobs", - type="int", + parser.add_argument("-j", "--jobs", + type=int, dest="jobs", default=0, help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)") - parser.add_option("--source", dest="source_dir", + parser.add_argument("--source", dest="source_dir", default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append") - parser.add_option("--build", dest="build_dir", + parser.add_argument("--build", dest="build_dir", default=None, help="The build (output) directory") - parser.add_option("-l", "--list", action="store_true", dest="list", + parser.add_argument("-l", "--list", action="store_true", dest="list", default=False, help="List (recursively) available tests in order and exit") - parser.add_option("-p", "--paths", dest="paths", + parser.add_argument("-p", "--paths", dest="paths", default=None, help="Limit the tests to those within the specified comma separated list of paths") format_choices = ["list", "json"] format_default_choice = "list" format_help = "Change the format in which tests are listed. Choices include: %s. Default: %s" % (", ".join(format_choices), format_default_choice) - parser.add_option("-f", "--format", type="choice", dest="format", - choices=format_choices, default=format_default_choice, help=format_help) + parser.add_argument("-f", "--format", type=argparse_lowercase_type(format_coices,"format"), dest="format", + default=format_default_choice, help=format_help) - parser.add_option("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail", + parser.add_argument("--continue-on-build-fail", action="store_true", dest="continue_on_build_fail", default=None, help="Continue trying to build all tests if a build failure occurs") - parser.add_option("-n", "--names", dest="names", + parser.add_argument("-n", "--names", dest="names", default=None, help="Limit the tests to a comma separated list of names") - parser.add_option("--test-spec", dest="test_spec", + parser.add_argument("--test-spec", dest="test_spec", default=None, help="Destination path for a test spec file that can be used by the Greentea automated test tool") - parser.add_option("--build-report-junit", dest="build_report_junit", + parser.add_argument("--build-report-junit", dest="build_report_junit", default=None, help="Destination path for a build report in the JUnit xml format") - parser.add_option("-v", "--verbose", + parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - (options, args) = parser.parse_args() + options = parser.parse_args() # Filter tests by path if specified if options.paths: