def test_add_verbose_option(self): """test add_verbose option""" options.add_verbose_option(self.parser, "verbose") # if nothing is passed it should be False parsed = self.parser.parse_args("") self.assertEqual(argparse.Namespace(verbose=False), parsed) # if you pass the option, then the value should be True parsed = self.parser.parse_args(["--verbose"]) self.assertEqual(argparse.Namespace(verbose=True), parsed) # if it receives no values, parser should exit with error code 2 with self.assertRaises(SystemExit) as err: self.parser.parse_args(["--verbose=yes"]) self.assertEqual(err.exception.code, 2)
# Create argument parser. parser = options.GadgetsArgumentParser() parser.add_argument('tests', metavar='TEST_NAME', type=str, nargs='*', help='Unit test to execute. If none are specified ' 'then all unit tests will be executed.') # Add version. options.add_version_option(parser) # Add verbose option. options.add_verbose_option(parser) # Add failfast option. parser.add_argument("--failfast", "-f", action="store_true", dest="failfast", help="Stop tests execution on first failure") # Add server. options.add_append_connection_option(parser, ["-s", "--server"], "server", required=False) # Add option to read passwords from stdin. options.add_stdin_password_option(parser)
if __name__ == "__main__": # retrieve logger _LOGGER = logging.getLogger(_SCRIPT_NAME) # add version options.add_version_option(_PARSER) # add command subparsers subparsers = _PARSER.add_subparsers(dest="command") # Create parent parsers for the different scenarios # create common parent parser with common options common_parser = options.GadgetsArgumentParser() # add verbose to common_parser parser options.add_verbose_option(common_parser) # Add the command argument common_parser.add_argument('--log-format', dest='log_format', type=str.upper, default="text", choices=["TEXT", "JSON"], help=_LOG_FORMAT_HELP) # option for log file common_parser.add_argument("--log-file", action="store", const="{}.log".format(_SCRIPT_NAME), nargs="?", default=None, dest="log_file", help=_LOG_FILE_HELP)
if __name__ == '__main__': # Check connector/python required version. check_connector_python() # Create argument parser. parser = options.GadgetsArgumentParser() parser.add_argument('tests', metavar='TEST_NAME', type=str, nargs='*', help='Unit test to execute. If none are specified ' 'then all unit tests will be executed.') # Add version. options.add_version_option(parser) # Add verbose option. options.add_verbose_option(parser) # Add failfast option. parser.add_argument("--failfast", "-f", action="store_true", dest="failfast", help="Stop tests execution on first failure") # Add server. options.add_append_connection_option(parser, ["-s", "--server"], "server", required=False) # Add option to read passwords from stdin. options.add_stdin_password_option(parser) parser.add_argument("--skip-tests", "-i", action="store", dest="skip_tests", type=str, default=None,