def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) if argument_values is not SUPPRESS \ or isinstance(action, _SubParsersAction): try: action(self, namespace, argument_values, option_string) except: # Begin added by argcomplete # When a subparser action is taken and fails due to incomplete arguments, it does not merge the # contents of its parsed namespace into the parent namespace. Do that here to allow completers to # access the partially parsed arguments for the subparser. if isinstance(action, _SubParsersAction): subnamespace = action._name_parser_map[argument_values[0]]._argcomplete_namespace for key, value in vars(subnamespace).items(): setattr(namespace, key, value) # End added by argcomplete raise
def _add_conf_file_option(self): """Add the '--configuration-file' option.""" default_prefix = '-' if '-' in self.prefix_chars else self.prefix_chars[0] self.add_argument( default_prefix+'conf', default_prefix*2+'config-file', metavar="FILE", help=_('Specify a configuration file from which to read options.'))
def patch(self, parser, namespace, values, option_string=None): """patch original __call__ method for argparse 1.1 (fix)""" from argparse import _UNRECOGNIZED_ARGS_ATTR from argparse import SUPPRESS from argparse import _ parser_name = values[0] arg_strings = values[1:] # set the parser name if requested if self.dest is not SUPPRESS: setattr(namespace, self.dest, parser_name) # select the parser try: parser = self._name_parser_map[parser_name] except KeyError: tup = parser_name, ', '.join(self._name_parser_map) msg = _('unknown parser %r (choices: %s)') % tup raise ArgumentError(self, msg) # parse all the remaining options into the namespace # store any unrecognized options on the object, so that the top # level parser can decide what to do with them namespace, arg_strings = parser.parse_known_args(arg_strings, namespace) if arg_strings: vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
def __init__(self, *args, **kwargs): kwargs['add_help'] = False super(Parser, self).__init__(*args, **kwargs) # Help only as --help (-h is used for --headers). self.add_argument('--help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit'))
def __call__(self, parser, namespace, values, option_string=None): """override stdlib argparse to revert subparser namespace changes Reverts the broken upstream change made in issue #9351 which causes issue #23058. This can be dropped when the problem is fixed upstream. """ parser_name = values[0] arg_strings = values[1:] # set the parser name if requested if self.dest is not argparse.SUPPRESS: setattr(namespace, self.dest, parser_name) # select the parser try: parser = self._name_parser_map[parser_name] except KeyError: tup = parser_name, ', '.join(self._name_parser_map) msg = _('unknown parser %r (choices: %s)') % tup raise argparse.ArgumentError(self, msg) # parse all the remaining options into the namespace # store any unrecognized options on the object, so that the top # level parser can decide what to do with them namespace, arg_strings = parser.parse_known_args(arg_strings, namespace) if arg_strings: vars(namespace).setdefault(argparse._UNRECOGNIZED_ARGS_ATTR, []) getattr(namespace, argparse._UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
def add_standard_arguments(self): grp = self.add_argument_group('Standard options') default_prefix = '-' grp.add_argument( default_prefix+'h', default_prefix*2+'help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) if self.version: grp.add_argument( default_prefix+'v', default_prefix*2+'version', action='version', default=argparse.SUPPRESS, version=self.version, help=argparse._("show program's version number and exit")) grp.add_argument('--print-args', type=bool, default=True, help='Print the command line arguments (to stderr)') #grp.add_argument('--config', action=AddConfig, help='Configuration file with options') grp.add_argument('--config', default=argparse.SUPPRESS, help='Configuration file with options')
def add_help_argument(self, *args, **kwargs): if not args: prefix_chars = self.prefix_chars default_prefix = "-" if "-" in prefix_chars else prefix_chars[0] args = [default_prefix + "h", default_prefix * 2 + "help"] kwargs.setdefault("action", "help") kwargs.setdefault("default", _argparse.SUPPRESS) kwargs.setdefault("help", _argparse._("show this help message and exit")) self.add_argument(*args, **kwargs)
def add_version_argument(self, *args, **kwargs): if not args: prefix_chars = self.prefix_chars default_prefix = "-" if "-" in prefix_chars else prefix_chars[0] args = [default_prefix * 2 + "version"] kwargs.setdefault("action", "version") kwargs.setdefault("default", _argparse.SUPPRESS) kwargs.setdefault("help", _argparse._("show program's version number and exit")) self.add_argument(*args, **kwargs)
def soft_error(self, message): """ Same as error, without the dying in a fire part. """ self.print_usage(sys.stderr) args = {'prog': self.prog, 'message': message} self._print_message( _('%(prog)s: error: %(message)s\n') % args, sys.stderr)
def add_help_argument(self, *args, **kwargs): if not args: prefix_chars = self.prefix_chars default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] args = [default_prefix + 'h', default_prefix*2 + 'help'] kwargs.setdefault('action', 'help') kwargs.setdefault('default', _argparse.SUPPRESS) kwargs.setdefault('help', _argparse._('show this help message and exit')) self.add_argument(*args, **kwargs)
def add_version_argument(self, *args, **kwargs): if not args: prefix_chars = self.prefix_chars default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] args = [default_prefix*2 + 'version'] kwargs.setdefault('action', 'version') kwargs.setdefault('default', _argparse.SUPPRESS) kwargs.setdefault('help', _argparse._("show program's version number and exit")) self.add_argument(*args, **kwargs)
def parse_args(self, args=None, namespace=None): """allow splitting of positional arguments""" args, argv = self.parse_known_args(args, namespace) if argv: for arg in argv: if arg and arg[0] == '-': msg = argparse._('unrecognized arguments: %s') self.error(msg % ' '.join(argv)) getattr(args, FILE_OR_DIR).extend(argv) return args
def dispatch_command(self, ns): # Argh the horrror! # # Since CPython revision cab204a79e09 (landed for python3.3) # http://hg.python.org/cpython/diff/cab204a79e09/Lib/argparse.py # the argparse module behaves differently than it did in python3.2 # # In practical terms subparsers are now optional in 3.3 so all of the # commands are no longer required parameters. # # To compensate, on python3.3 and beyond, when the user just runs # plainbox without specifying the command, we manually, explicitly do # what python3.2 did: call parser.error(_('too few arguments')) if (sys.version_info[:2] >= (3, 3) and getattr(ns, "command", None) is None): self._parser.error(argparse._("too few arguments")) else: return ns.command.invoked(ns)
def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) if argument_values is not SUPPRESS: action(self, namespace, argument_values, option_string)
def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _("not allowed with argument %s") action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) # argcomplete: we should walk through sub-commands. if argument_values is not SUPPRESS or isinstance(action, _SubParsersAction): action(self, namespace, argument_values, option_string)
def _check_value(self, action, value): # converted value must be one of the choices (if specified) if action.choices is not None and value not in action.choices: tup = value, ', '.join([repr(x) for x in sorted(action.choices) if x != 'pony']) msg = argparse._('invalid choice: %r (choose from %s)') % tup raise argparse.ArgumentError(action, msg)
import logging import threading, os try: import queue except ImportError: import Queue as queue # -------------------------------------------------------------------------------- argDescription = """ -- Simple MQTT logger v.01 - JavierFG -- The program will subscribe to the MQTT mqttBroker topic and logg all messages received using internal buffers and multithreading to optimize the data collection procedure. Files will be creaged under the logg directory with the following format yymmddHHMMSS-mqttMessages.log""" parser = argparse.ArgumentParser( description=argDescription, formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False) parser.add_argument("--help", action="help", default=argparse.SUPPRESS, help=argparse._("Show this help message")) parser.add_argument("-h", help="MQTT host address (default=localhost)", type=str, default="localhost", dest="mqttHost") parser.add_argument("-p", help="MQTT port number (default=1883)", type=int, default=1883, dest="mqttPort") parser.add_argument("-u", help="MQTT username", default=None, dest="mqttUser") parser.add_argument("-P", help="MQTT password", default=None, dest="mqttPass") parser.add_argument("-t", help="MQTT topic (default=All toypics)", default="#", dest="mqttTopic") parser.add_argument("-l", help="Logg data MQTT messages directory (default=data-logs)", default="data-logs", dest="logsFolder") parser.add_argument("-r", help="Number of logss per file (default=unlimited)", type=int, default=None, dest="numLogs") parser.add_argument("-nl", help="Add new line char on every mqtt messages", dest="newLine", action="store_true") parser.add_argument("-ats", help="Append timestamp to saved mqtt message", dest="addTime", action="store_true") parser.add_argument("-at", help="Append topic to saved mqtt message", dest="addTopic", action="store_true") parser.add_argument("-v", help="Show program verbose output", dest="verbose", action="store_true") args = parser.parse_args()
def cmdError(name, code=0, exitNow=False): msg="FAILURE - %s%s" % (name, ("" if code == 0 else (" returned error code %d" % code))) if exitNow: errorExit(msg, True) else: Print(msg) TEST_OUTPUT_DEFAULT="test_output_0.txt" LOCAL_HOST="localhost" DEFAULT_PORT=8888 parser = argparse.ArgumentParser(add_help=False) # Override default help argument so that only --help (and not -h) can call help parser.add_argument('-?', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) parser.add_argument("-o", "--output", type=str, help="output file", default=TEST_OUTPUT_DEFAULT) parser.add_argument("-h", "--host", type=str, help="%s host name" % (testUtils.Utils.EosServerName), default=LOCAL_HOST) parser.add_argument("-p", "--port", type=int, help="%s host port" % testUtils.Utils.EosServerName, default=DEFAULT_PORT) parser.add_argument("--inita_prvt_key", type=str, help="Inita private key.", default=testUtils.Cluster.initaAccount.ownerPrivateKey) parser.add_argument("--initb_prvt_key", type=str, help="Initb private key.", default=testUtils.Cluster.initbAccount.ownerPrivateKey) parser.add_argument("--mongodb", help="Configure a MongoDb instance", action='store_true') parser.add_argument("--dump-error-details", help="Upon error print tn_data_*/config.ini and tn_data_*/stderr.log to stdout", action='store_true') parser.add_argument("--dont-launch", help="Don't launch own node. Assume node is already running.", action='store_true')
def _parse_known_args(self, arg_strings, namespace): self.active_actions = [] # Added by argcomplete # replace arg strings that are file references if self.fromfile_prefix_chars is not None: arg_strings = self._read_args_from_files(arg_strings) # map all mutually exclusive arguments to the other arguments # they can't occur with action_conflicts = {} for mutex_group in self._mutually_exclusive_groups: group_actions = mutex_group._group_actions for i, mutex_action in enumerate(mutex_group._group_actions): conflicts = action_conflicts.setdefault(mutex_action, []) conflicts.extend(group_actions[:i]) conflicts.extend(group_actions[i + 1:]) # find all option indices, and determine the arg_string_pattern # which has an 'O' if there is an option at an index, # an 'A' if there is an argument, or a '-' if there is a '--' option_string_indices = {} arg_string_pattern_parts = [] arg_strings_iter = iter(arg_strings) for i, arg_string in enumerate(arg_strings_iter): # all args after -- are non-options if arg_string == '--': arg_string_pattern_parts.append('-') for arg_string in arg_strings_iter: arg_string_pattern_parts.append('A') # otherwise, add the arg to the arg strings # and note the index if it was an option else: option_tuple = self._parse_optional(arg_string) if option_tuple is None: pattern = 'A' else: option_string_indices[i] = option_tuple pattern = 'O' arg_string_pattern_parts.append(pattern) # join the pieces together to form the pattern arg_strings_pattern = ''.join(arg_string_pattern_parts) # converts arg strings to the appropriate and then takes the action seen_actions = set() seen_non_default_actions = set() def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) # argcomplete: we should walk through sub-commands. if argument_values is not SUPPRESS \ or isinstance(action, _SubParsersAction): action(self, namespace, argument_values, option_string) # function to convert arg_strings into an optional action def consume_optional(start_index): # get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple # identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True: # if we found no optional action, skip it if action is None: extras.append(arg_strings[start_index]) return start_index + 1 # if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A') # if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) char = option_string[0] option_string = char + explicit_arg[0] new_explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] explicit_arg = new_explicit_arg else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break # error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] self.active_actions = [action] # Added by argcomplete action.num_consumed_args = 0 # Added by argcomplete arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] # Begin added by argcomplete # If the pattern is not open (e.g. no + at the end), remove the action from active actions (since # it wouldn't be able to consume any more args) if not action_is_open(action): self.active_actions.remove(action) elif action.nargs == OPTIONAL and len(args) == 1: self.active_actions.remove(action) action.num_consumed_args = len(args) # End added by argcomplete action_tuples.append((action, args, option_string)) break # add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop # the list of Positionals left to be parsed; this is modified # by consume_positionals() positionals = self._get_positional_actions() # function to convert arg_strings into positional actions def consume_positionals(start_index): # match as many Positionals as possible match_partial = self._match_arguments_partial selected_pattern = arg_strings_pattern[start_index:] arg_counts = match_partial(positionals, selected_pattern) # slice off the appropriate arg strings for each Positional # and add the Positional and its args to the list for action, arg_count in zip(positionals, arg_counts): if arg_count > 0: # Added by argcomplete self.active_actions = [action] # Added by argcomplete else: # Added by argcomplete self.active_actions.append(action) # Added by argcomplete args = arg_strings[start_index: start_index + arg_count] start_index += arg_count action.num_consumed_args = len(args) # Added by argcomplete take_action(action, args) # slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped positionals[:] = positionals[len(arg_counts):] return start_index # consume Positionals and Optionals alternately, until we have # passed the last option string extras = [] start_index = 0 if option_string_indices: max_option_string_index = max(option_string_indices) else: max_option_string_index = -1 while start_index <= max_option_string_index: # consume any Positionals preceding the next option next_option_string_index = min([ index for index in option_string_indices if index >= start_index]) if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index) # only try to parse the next optional if we didn't consume # the option string during the positionals parsing if positionals_end_index > start_index: start_index = positionals_end_index continue else: start_index = positionals_end_index # if we consumed all the positionals we could and we're not # at the index of an option string, there were extra arguments if start_index not in option_string_indices: strings = arg_strings[start_index:next_option_string_index] extras.extend(strings) start_index = next_option_string_index # consume the next optional and any arguments for it start_index = consume_optional(start_index) # consume any positionals following the last Optional stop_index = consume_positionals(start_index) # if we didn't consume all the argument strings, there were extras extras.extend(arg_strings[stop_index:]) # if we didn't use all the Positional objects, there were too few # arg strings supplied. if positionals: self.active_actions.append(positionals[0]) # Added by argcomplete # self.error(_('too few arguments')) # Disabled by argcomplete # Begin disabled by arg complete # # make sure all required actions were present # for action in self._actions: # if action.required: # if action not in seen_actions: # name = _get_action_name(action) # self.error(_('argument %s is required') % name) # End disabled by argcomplete # make sure all required groups had one option present for group in self._mutually_exclusive_groups: if group.required: for action in group._group_actions: if action in seen_non_default_actions: break # if no actions were used, report the error else: names = [_get_action_name(action) for action in group._group_actions if action.help is not SUPPRESS] msg = _('one of the arguments %s is required') self.error(msg % ' '.join(names)) # return the updated namespace and the extra arguments return namespace, extras
def parse_args(includeArgs, applicationSpecificArgs=AppArgs()): """Accepts set of arguments, builds argument parser and returns parse_args() output.""" assert (includeArgs) assert (isinstance(includeArgs, set)) assert (isinstance(applicationSpecificArgs, AppArgs)) parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-?', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) if "-p" in includeArgs: parser.add_argument("-p", type=int, help="producing nodes count", default=1) if "-n" in includeArgs: parser.add_argument("-n", type=int, help="total nodes", default=0) if "-d" in includeArgs: parser.add_argument("-d", type=int, help="delay between nodes startup", default=1) if "--nodes-file" in includeArgs: parser.add_argument( "--nodes-file", type=str, help="File containing nodes info in JSON format.") if "-s" in includeArgs: parser.add_argument("-s", type=str, help="topology", choices=["mesh"], default="mesh") if "-c" in includeArgs: parser.add_argument("-c", type=str, help="chain strategy", choices=[ Utils.SyncResyncTag, Utils.SyncNoneTag, Utils.SyncHardReplayTag ], default=Utils.SyncResyncTag) if "--kill-sig" in includeArgs: parser.add_argument("--kill-sig", type=str, choices=[Utils.SigKillTag, Utils.SigTermTag], help="kill signal.", default=Utils.SigKillTag) if "--kill-count" in includeArgs: parser.add_argument("--kill-count", type=int, help="enunode instances to kill", default=-1) if "--p2p-plugin" in includeArgs: parser.add_argument( "--p2p-plugin", choices=["net", "bnet"], help="select a p2p plugin to use. Defaults to net.", default="net") if "--seed" in includeArgs: parser.add_argument("--seed", type=int, help="random seed", default=1) if "--host" in includeArgs: parser.add_argument("-h", "--host", type=str, help="%s host name" % (Utils.EnuServerName), default=TestHelper.LOCAL_HOST) if "--port" in includeArgs: parser.add_argument("-p", "--port", type=int, help="%s host port" % Utils.EnuServerName, default=TestHelper.DEFAULT_PORT) if "--wallet-host" in includeArgs: parser.add_argument("--wallet-host", type=str, help="%s host" % Utils.EnuWalletName, default=TestHelper.LOCAL_HOST) if "--wallet-port" in includeArgs: parser.add_argument("--wallet-port", type=int, help="%s port" % Utils.EnuWalletName, default=TestHelper.DEFAULT_WALLET_PORT) if "--prod-count" in includeArgs: parser.add_argument("-c", "--prod-count", type=int, help="Per node producer count", default=1) if "--defproducera_prvt_key" in includeArgs: parser.add_argument("--defproducera_prvt_key", type=str, help="defproducera private key.") if "--defproducerb_prvt_key" in includeArgs: parser.add_argument("--defproducerb_prvt_key", type=str, help="defproducerb private key.") if "--mongodb" in includeArgs: parser.add_argument("--mongodb", help="Configure a MongoDb instance", action='store_true') if "--dump-error-details" in includeArgs: parser.add_argument( "--dump-error-details", help= "Upon error print etc/enumivo/node_*/config.ini and var/lib/node_*/stderr.log to stdout", action='store_true') if "--dont-launch" in includeArgs: parser.add_argument( "--dont-launch", help="Don't launch own node. Assume node is already running.", action='store_true') if "--keep-logs" in includeArgs: parser.add_argument( "--keep-logs", help="Don't delete var/lib/node_* folders upon test completion", action='store_true') if "-v" in includeArgs: parser.add_argument("-v", help="verbose logging", action='store_true') if "--leave-running" in includeArgs: parser.add_argument( "--leave-running", help="Leave cluster running after test finishes", action='store_true') if "--only-bios" in includeArgs: parser.add_argument("--only-bios", help="Limit testing to bios node.", action='store_true') if "--clean-run" in includeArgs: parser.add_argument( "--clean-run", help="Kill all enunode and enuwallet instances", action='store_true') if "--sanity-test" in includeArgs: parser.add_argument( "--sanity-test", help= "Validates enunode and enuwallet are in path and can be started up.", action='store_true') for arg in applicationSpecificArgs.args: parser.add_argument(arg.flag, type=arg.type, help=arg.help, choices=arg.choices, default=arg.default) args = parser.parse_args() return args
def try_parse_args(self, args=None, namespace=None): args, argv = self.try_parse_known_args(args, namespace, ) if argv: msg = argparse._('unrecognized arguments: %s') self.error(msg % ' '.join(argv)) return args
def unrecognized_arguments_error(self, args): self.soft_error(_('unrecognized arguments: %s') % ' '.join(args))
def consume_optional(start_index): # get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple # identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True: # if we found no optional action, skip it if action is None: extras.append(arg_strings[start_index]) return start_index + 1 # if we match help options, skip them for now so subparsers # show up in the help output if arg_strings[start_index] in ('-h', '--help'): extras.append(arg_strings[start_index]) return start_index + 1 # if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A') # if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) char = option_string[0] option_string = char + explicit_arg[0] new_explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] explicit_arg = new_explicit_arg else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break # error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] action_tuples.append((action, args, option_string)) break # add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop
def _format_usage(self, usage, actions, groups, prefix): if prefix is None: prefix = argparse._('usage: ') # if usage is specified, use that if usage is not None: usage = usage % dict(prog=self._prog) # if no optionals or positionals are available, usage is just prog elif usage is None and not actions: usage = '%(prog)s' % dict(prog=self._prog) # if optionals and positionals are available, calculate usage elif usage is None: prog = '%(prog)s' % dict(prog=self._prog) # split optionals from positionals optionals = [] positionals = [] for action in actions: if action.option_strings: optionals.append(action) else: positionals.append(action) # build full usage string format = self._format_actions_usage action_usage = format(optionals + positionals, groups) usage = ' '.join([s for s in [prog, action_usage] if s]) # wrap the usage parts if it's too long text_width = self._width - self._current_indent if len(prefix) + len(usage) > text_width: # break usage into wrappable parts part_regexp = r'\(.*?\)+|\[.*?\]+|\S+' opt_usage = format(optionals, groups) pos_usage = format(positionals, groups) opt_parts = argparse._re.findall(part_regexp, opt_usage) pos_parts = argparse._re.findall(part_regexp, pos_usage) assert ' '.join(opt_parts) == opt_usage assert ' '.join(pos_parts) == pos_usage # helper for wrapping lines def get_lines(parts, indent, prefix=None): lines = [] line = [] if prefix is not None: line_len = len(prefix) - 1 else: line_len = len(indent) - 1 for part in parts: if line_len + 1 + len(part) > text_width: lines.append(indent + ' '.join(line)) line = [] line_len = len(indent) - 1 line.append(part) line_len += len(part) + 1 if line: lines.append(indent + ' '.join(line)) if prefix is not None: lines[0] = lines[0][len(indent):] return lines # if prog is short, follow it with optionals or positionals if len(prefix) + len(prog) <= 0.75 * text_width: indent = ' ' * (len(prefix) + len(prog) + 1) if opt_parts: lines = get_lines([prog] + opt_parts, indent, prefix) lines.extend(get_lines(pos_parts, indent)) elif pos_parts: lines = get_lines([prog] + pos_parts, indent, prefix) else: lines = [prog] # if prog is long, put it on its own line else: indent = ' ' * len(prefix) parts = opt_parts + pos_parts lines = get_lines(parts, indent) if len(lines) > 1: lines = [] lines.extend(get_lines(opt_parts, indent)) lines.extend(get_lines(pos_parts, indent)) lines = [prog] + lines # join lines into usage usage = '\n'.join(lines) # prefix with 'usage:' return '%s%s\n\n' % (prefix, usage)
def error(self, message): if message != _('too few arguments'): super(ArgumentParser, self).error(message)
def parse_args(): parser = argparse.ArgumentParser(description='UoA NeCTar IPAM queries', add_help=False, formatter_class=RawTextHelpFormatter) parser.add_argument('-?', '--help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) parser.add_argument('-i', '--ip', dest='host_ip', help='action against this host ip') parser.add_argument('-h', '--hostname', dest='hostname', help='host name') parser.add_argument( '-H', '--re_hostname', dest='re_hostname', help='host name as regular expression (ensure you use ^ and $)') parser.add_argument('-a', '--alias', dest='alias', help='alias for dns entry') parser.add_argument('-c', '--cname', dest='cname', help='cname for dns entry') parser.add_argument('-n', '--new', dest='new_entry', action='store_true', help='create new dns entry') parser.add_argument('-m', '--modify', dest='modify_entry', action='store_true', help='modify existing dns entry') parser.add_argument('-d', '--delete', dest='delete_entry', help='delete dns entry') parser.add_argument('-r', '--rack', dest='tdc_rack', help='TDC Rack (when using autogeneration)') parser.add_argument('-u', '--u', dest='tdc_rack_u', help='TDC Rack U (when using autogeneration)') parser.add_argument('-x', '--switch', dest='tdc_rack_x', action='store_true', help='TDC Rack U is a switch (so name is <rack>x<u>)') parser.add_argument( '--mgmt', dest='management', action='store_true', help='Generate management address. Needs --rack and --u') parser.add_argument( '--prov', dest='provisioning', action='store_true', help='Generate provisioning address. Needs --rack and --u') parser.add_argument('--api', dest='api', action='store_true', help='Generate api address. Needs --rack and --u') parser.add_argument( '--ceph', dest='ceph', action='store_true', help='Generate ceph storage network address. Needs --rack and --u') parser.add_argument( '--repl', dest='replication', action='store_true', help='Generate replication address. Needs --rack and --u') return parser.parse_args()
def __init__(self, prog=None, *, add_help=False, description=None, formatter_class=argparse.ArgumentDefaultsHelpFormatter, **kwargs ): if prog is None and __name__ != "__main__": prog = __name__ if description is None: description = _(__doc__) kwargs["prog"] = prog kwargs["add_help"] = add_help kwargs["description"] = description kwargs["formatter_class"] = formatter_class super().__init__(**kwargs) self._exitstack = None self.add_argument("archive", help=_("Path to the destination archive file")) self.add_argument("files", nargs="*", help=_("A list of files to add to the archive")) self.add_argument("-d", "--directory", metavar="DIR", type=self._open_directory, default=os.curdir, help=_("Added file paths are relative to this directory.")) self.add_argument("-Z", "--compression-method", choices=ZipFile.supported_compressors, default=ZipFile.supported_compressors[1], help=_("Select a compression method.")) self.add_argument("--compression-level", metavar="N", dest="compression_level", type=self._parse_compression_level, default=-1, help="Set the compression level: 0 indicates no compression (store all " "files), lower indicates the fastest compression speed (less " "compression), higher indicates the slowest compression (optimal " "compression) and -1 indicates the default for the chosen compression " "method.") for level in range(ZipFile.compression_level_max + 1): self.add_argument("-" + format(level, "d"), action="store_const", dest="compression_level", const=level, help=argparse.SUPPRESS) self.add_argument("-y", "--symlinks", action="store_true", help=_("Add symbolic links instead of their targets to the archive.")) default_interpreter = None executable_help = _( "Turn the archive into an executable Python ZIP application.") if sys.executable: default_interpreter = self._parse_executable(sys.executable, split=False) executable_help = " ".join((executable_help, _("If you specify an argument, its value is used as the interpreter " "instead of {!r}.").format(sys.executable))) self.add_argument("--executable", metavar="INTERPRETER", nargs=("?", 1)[default_interpreter is None], type=self._parse_executable, const=default_interpreter, help=executable_help) read_file_type = argparse.FileType() names_file_group = self.add_mutually_exclusive_group() names_file_group.add_argument("--names-file", metavar="FILE", type=read_file_type, help=_("Take the list of files to add from this file, one per line " "(after those specified on the command-line).")) names_file_group.add_argument("--names-file0", metavar="FILE", type=read_file_type, help=_("Like above but with NUL as line delimiter.")) self.add_argument("-q", "--quiet", action="store_true", help=_("Don't print status notifications or the list of added files.")) if not add_help: self.add_argument("-h", "--help", action="help", dest=argparse.SUPPRESS, help=_("Show this help and exit."))
def error(self, message): if self._usage_on_error and message == argparse._('too few arguments'): self.print_help() print() self.exit(2, argparse._('%s: error: %s\n') % (self.prog, message)) super(Parser, self).error(message)
def parse_args(includeArgs): """Accepts set of arguments, builds argument parser and returns parse_args() output.""" assert(includeArgs) assert(isinstance(includeArgs, set)) parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-?', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) if "-p" in includeArgs: parser.add_argument("-p", type=int, help="producing nodes count", default=1) if "-n" in includeArgs: parser.add_argument("-n", type=int, help="total nodes", default=0) if "-d" in includeArgs: parser.add_argument("-d", type=int, help="delay between nodes startup", default=1) if "--nodes-file" in includeArgs: parser.add_argument("--nodes-file", type=str, help="File containing nodes info in JSON format.") if "-s" in includeArgs: parser.add_argument("-s", type=str, help="topology", choices=["mesh"], default="mesh") if "-c" in includeArgs: parser.add_argument("-c", type=str, help="chain strategy", choices=[Utils.SyncResyncTag, Utils.SyncNoneTag, Utils.SyncHardReplayTag], default=Utils.SyncResyncTag) if "--kill-sig" in includeArgs: parser.add_argument("--kill-sig", type=str, choices=[Utils.SigKillTag, Utils.SigTermTag], help="kill signal.", default=Utils.SigKillTag) if "--kill-count" in includeArgs: parser.add_argument("--kill-count", type=int, help="nodeos instances to kill", default=-1) if "--p2p-plugin" in includeArgs: parser.add_argument("--p2p-plugin", choices=["net", "bnet"], help="select a p2p plugin to use. Defaults to net.", default="net") if "--seed" in includeArgs: parser.add_argument("--seed", type=int, help="random seed", default=1) if "--host" in includeArgs: parser.add_argument("-h", "--host", type=str, help="%s host name" % (Utils.EosServerName), default=TestHelper.LOCAL_HOST) if "--port" in includeArgs: parser.add_argument("-p", "--port", type=int, help="%s host port" % Utils.EosServerName, default=TestHelper.DEFAULT_PORT) if "--prod-count" in includeArgs: parser.add_argument("-c", "--prod-count", type=int, help="Per node producer count", default=1) if "--defproducera_prvt_key" in includeArgs: parser.add_argument("--defproducera_prvt_key", type=str, help="defproducera private key.") if "--defproducerb_prvt_key" in includeArgs: parser.add_argument("--defproducerb_prvt_key", type=str, help="defproducerb private key.") if "--mongodb" in includeArgs: parser.add_argument("--mongodb", help="Configure a MongoDb instance", action='store_true') if "--dump-error-details" in includeArgs: parser.add_argument("--dump-error-details", help="Upon error print etc/eosio/node_*/config.ini and var/lib/node_*/stderr.log to stdout", action='store_true') if "--dont-launch" in includeArgs: parser.add_argument("--dont-launch", help="Don't launch own node. Assume node is already running.", action='store_true') if "--keep-logs" in includeArgs: parser.add_argument("--keep-logs", help="Don't delete var/lib/node_* folders upon test completion", action='store_true') if "-v" in includeArgs: parser.add_argument("-v", help="verbose logging", action='store_true') if "--leave-running" in includeArgs: parser.add_argument("--leave-running", help="Leave cluster running after test finishes", action='store_true') if "--only-bios" in includeArgs: parser.add_argument("--only-bios", help="Limit testing to bios node.", action='store_true') if "--clean-run" in includeArgs: parser.add_argument("--clean-run", help="Kill all nodeos and kleos instances", action='store_true') if "--sanity-test" in includeArgs: parser.add_argument("--sanity-test", help="Validates nodeos and kleos are in path and can be started up.", action='store_true') args = parser.parse_args() return args
def buildParserFromArgs(): argParser = argparse.ArgumentParser(description="""IMS Neural Parser""", add_help=False) parserArgs = argParser.add_argument_group('parser') parserArgs.add_argument("--parser", help="which parser to use", choices=[ "GRAPH", "TRANS" ], required=True) parserArgs.add_argument("--loglevel", help="which log level to use", choices=[ "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL" ], required=False, default="DEBUG") # files filesArgs = argParser.add_argument_group('files') filesArgs.add_argument("--train", help="training file", type=str, required=False) filesArgs.add_argument("--dev", help="development file", type=str, required=False) filesArgs.add_argument("--model", help="load model from the file", type=str, required=False) filesArgs.add_argument("--test", help="test file", type=str, required=False) filesArgs.add_argument("--output", help="output predictions to the file", type=str, required=False) filesArgs.add_argument("--save", help="save model to the file", type=str, required=False) filesArgs.add_argument("--saveMax", help="save the best model to the file", type=str, required=False) filesArgs.add_argument("--reportTrain", help="reports training accuracy", type=str, required=False, default="False") # format formatArgs = argParser.add_argument_group('format') formatArgs.add_argument("--format", help="file format", choices=[ "conll06", "conllu" ], type=str, required=False, default="conllu") formatArgs.add_argument("--normalize", help="normalize the words", choices=[ "True", "False" ], type=str, required=False, default="True") # training trainingArgs = argParser.add_argument_group('training') trainingArgs.add_argument("--patience", help="patience for the early update", type=int, required=False) trainingArgs.add_argument("--seed", help="random seed (different than dynet-seed)", type=int, required=False, default=42) trainingArgs.add_argument("--epochs", help="nr of epochs", type=int, required=False, default=30) # dynet dynetArgs = argParser.add_argument_group('dynet') dynetArgs.add_argument("--dynet-gpu-ids", help="turns on dynet", required=False) dynetArgs.add_argument("--dynet-devices", help="turns on dynet", required=False) dynetArgs.add_argument("--dynet-autobatch", help="turns on dynet-autobatch", required=False) dynetArgs.add_argument("--dynet-seed", help="sets random seed generator for dynet", required=False) # parse for the first time to only get the parser name args, _ = argParser.parse_known_args() # set logging level lformat = '[%(levelname)s] %(asctime)s %(name)s# %(message)s' logging.basicConfig(stream=sys.stdout, level=logging.getLevelName(args.loglevel), format=lformat) # parse the second time to get all the args options.addParserCmdArguments(args.parser, argParser) argParser.add_argument('--help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) args = argParser.parse_args() # this still leaves dynet-seed being random random.seed(args.seed) logging.info("Freezing random seed: %i" % args.seed) opts = utils.NParserOptions() if args.model != None: # all the args fields will get overwritten opts.load(args.model + ".args") opts.logOptions() options.fillParserOptions(args, opts) opts.logOptions() parser = builder.buildParser(opts) if args.save != None: opts.save(args.save + ".args") return args, opts, parser
def parse_args(self, args=None, namespace=None, generate_dir=True): args, argv = self.parse_known_args(args, namespace, generate_dir) if argv: msg = argparse._('unrecognized arguments: %s') self.error(msg % ' '.join(argv)) return args
import argparse from argparse import _ parser = argparse.ArgumentParser(add_help=False) parser.add_argument( "-h", "--help", action="store_true", help=_("show this help message and exit"), ) parser.add_argument("--foo") args, rest = parser.parse_known_args() print(args, rest) print("**END**")
def _format_usage(self, usage, actions, groups, prefix) -> str: if prefix is None: prefix = _('Usage: ') # if usage is specified, use that if usage is not None: usage %= dict(prog=self._prog) # if no optionals or positionals are available, usage is just prog elif usage is None and not actions: usage = '%(prog)s' % dict(prog=self._prog) # if optionals and positionals are available, calculate usage elif usage is None: prog = '%(prog)s' % dict(prog=self._prog) # split optionals from positionals optionals = [] positionals = [] # Begin cmd2 customization (separates required and optional, applies to all changes in this function) required_options = [] for action in actions: if action.option_strings: if action.required: required_options.append(action) else: optionals.append(action) else: positionals.append(action) # End cmd2 customization # build full usage string format = self._format_actions_usage action_usage = format(required_options + optionals + positionals, groups) usage = ' '.join([s for s in [prog, action_usage] if s]) # wrap the usage parts if it's too long text_width = self._width - self._current_indent if len(prefix) + len(usage) > text_width: # Begin cmd2 customization # break usage into wrappable parts part_regexp = r'\(.*?\)+|\[.*?\]+|\S+' req_usage = format(required_options, groups) opt_usage = format(optionals, groups) pos_usage = format(positionals, groups) req_parts = re.findall(part_regexp, req_usage) opt_parts = re.findall(part_regexp, opt_usage) pos_parts = re.findall(part_regexp, pos_usage) assert ' '.join(req_parts) == req_usage assert ' '.join(opt_parts) == opt_usage assert ' '.join(pos_parts) == pos_usage # End cmd2 customization # helper for wrapping lines # noinspection PyMissingOrEmptyDocstring,PyShadowingNames def get_lines(parts, indent, prefix=None): lines = [] line = [] if prefix is not None: line_len = len(prefix) - 1 else: line_len = len(indent) - 1 for part in parts: if line_len + 1 + len(part) > text_width and line: lines.append(indent + ' '.join(line)) line = [] line_len = len(indent) - 1 line.append(part) line_len += len(part) + 1 if line: lines.append(indent + ' '.join(line)) if prefix is not None: lines[0] = lines[0][len(indent):] return lines # if prog is short, follow it with optionals or positionals if len(prefix) + len(prog) <= 0.75 * text_width: indent = ' ' * (len(prefix) + len(prog) + 1) # Begin cmd2 customization if req_parts: lines = get_lines([prog] + req_parts, indent, prefix) lines.extend(get_lines(opt_parts, indent)) lines.extend(get_lines(pos_parts, indent)) elif opt_parts: lines = get_lines([prog] + opt_parts, indent, prefix) lines.extend(get_lines(pos_parts, indent)) elif pos_parts: lines = get_lines([prog] + pos_parts, indent, prefix) else: lines = [prog] # End cmd2 customization # if prog is long, put it on its own line else: indent = ' ' * len(prefix) # Begin cmd2 customization parts = req_parts + opt_parts + pos_parts lines = get_lines(parts, indent) if len(lines) > 1: lines = [] lines.extend(get_lines(req_parts, indent)) lines.extend(get_lines(opt_parts, indent)) lines.extend(get_lines(pos_parts, indent)) # End cmd2 customization lines = [prog] + lines # join lines into usage usage = '\n'.join(lines) # prefix with 'Usage:' return '%s%s\n\n' % (prefix, usage)
help="indicate the server IP address", required=True) parser.add_argument("-p", "--port", type=int, help="port number for client to connect to server", required=True) parser.add_argument("-l", "--logfile", help="name of logfile", required=True) parser.add_argument("-n", "--name", help="indicates client name", required=True) parser.add_argument("--help", action="help", default=argparse.SUPPRESS, help=argparse._("show this help message and exit")) args = parser.parse_args() # Global variables CLIENT_NAME = args.name # Global output file outputFile = open(args.logfile, 'w') # On exit, log to file @atexit.register def exitCleanUp(): logToFile("terminating client...") # Logging method def logToFile(msg):
def consume_optional(start_index): # get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple # identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True: # if we found no optional action, skip it if action is None: extras.append(arg_strings[start_index]) return start_index + 1 # if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A') # if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) char = option_string[0] option_string = char + explicit_arg[0] new_explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] explicit_arg = new_explicit_arg else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break # error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] self.active_actions = [action] # Added by argcomplete action.num_consumed_args = 0 # Added by argcomplete arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] # Begin added by argcomplete # If the pattern is not open (e.g. no + at the end), remove the action from active actions (since # it wouldn't be able to consume any more args) if not action_is_open(action): self.active_actions.remove(action) elif action.nargs == OPTIONAL and len(args) == 1: self.active_actions.remove(action) action.num_consumed_args = len(args) # End added by argcomplete action_tuples.append((action, args, option_string)) break # add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop
hosts = Remote.hosts ports = Remote.ports TEST_OUTPUT_DEFAULT = "p2p_network_test_log.txt" DEFAULT_HOST = hosts[0] DEFAULT_PORT = ports[0] parser = argparse.ArgumentParser(add_help=False) Print = testUtils.Utils.Print errorExit = testUtils.Utils.errorExit # Override default help argument so that only --help (and not -h) can call help parser.add_argument('-?', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) parser.add_argument("-o", "--output", type=str, help="output file", default=TEST_OUTPUT_DEFAULT) parser.add_argument("--inita_prvt_key", type=str, help="Inita private key.", default=testUtils.Cluster.initaAccount.ownerPrivateKey) parser.add_argument("--initb_prvt_key", type=str, help="Initb private key.", default=testUtils.Cluster.initbAccount.ownerPrivateKey) parser.add_argument("--wallet_host", type=str,
def update_event(self, inp=-1): self.set_output_val(0, argparse._(self.input(0)))
def test_error(self): stub_stdouts(self) parser = ArgumentParser() parser.error(_('too few arguments')) self.assertEqual('', sys.stdout.getvalue()) self.assertEqual('', sys.stderr.getvalue())
def cli(): parser = argparse.ArgumentParser( description=description, formatter_class=argparse.RawTextHelpFormatter, add_help=False) # Override default help argument so that only --help (and not -h) can call # help parser.add_argument( '--help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit')) parser.add_argument( 'database', metavar='DATABASE', type=str, help='Name source database.') parser.add_argument( '-v', '--verbose', action='store_true', help='Print detailed move information.') parser.add_argument( '-h', "--host", required=False, type=str, default='127.0.0.1', help="Default: '127.0.0.1'") parser.add_argument( '-u', "--user", required=False, type=str, default='root', help="Default: 'root'") parser.add_argument( '-p', "--password", metavar='PW', required=False, type=str, default='', help="Default: ''") parser.add_argument( "-l", "--limit", metavar='LIM', type=int, default=0, help="Limit number of docs. Meant for debugging.") args = parser.parse_args() if args.database not in ['declassification_frus', 'declassification_statedeptcables', 'declassification_ddrs', 'declassification_kissinger']: err_msg = ("Database must be one of frus, statedeptcables, ddrs or " "kissinger.") raise Exception(err_msg) dbinfo = { 'host': args.host, 'user': args.user, 'passwd': args.password, 'database': args.database} if args.limit == 0: args.limit = None return (dbinfo, args.limit, args.verbose)
def error(self, message): self.print_usage(argparse._sys.stderr) raise ValueError(argparse._("%s: error: %s\n") % (self.prog, message))
def clean(self): if self.category.name == "videos" and self.video is None: raise ValidationError( _('Video must not be null for video category'))
def args_help(parser): parser.add_argument('--help', action='help', default=argparse.SUPPRESS, help=argparse._('Show this help message and exit'))
def _parse_known_args(self, arg_strings, namespace): self.active_actions = [] # Added by argcomplete # replace arg strings that are file references if self.fromfile_prefix_chars is not None: arg_strings = self._read_args_from_files(arg_strings) # map all mutually exclusive arguments to the other arguments # they can't occur with action_conflicts = {} for mutex_group in self._mutually_exclusive_groups: group_actions = mutex_group._group_actions for i, mutex_action in enumerate(mutex_group._group_actions): conflicts = action_conflicts.setdefault(mutex_action, []) conflicts.extend(group_actions[:i]) conflicts.extend(group_actions[i + 1:]) # find all option indices, and determine the arg_string_pattern # which has an 'O' if there is an option at an index, # an 'A' if there is an argument, or a '-' if there is a '--' option_string_indices = {} arg_string_pattern_parts = [] arg_strings_iter = iter(arg_strings) for i, arg_string in enumerate(arg_strings_iter): # all args after -- are non-options if arg_string == '--': arg_string_pattern_parts.append('-') for arg_string in arg_strings_iter: arg_string_pattern_parts.append('A') # otherwise, add the arg to the arg strings # and note the index if it was an option else: option_tuple = self._parse_optional(arg_string) if option_tuple is None: pattern = 'A' else: option_string_indices[i] = option_tuple pattern = 'O' arg_string_pattern_parts.append(pattern) # join the pieces together to form the pattern arg_strings_pattern = ''.join(arg_string_pattern_parts) # converts arg strings to the appropriate and then takes the action seen_actions = set() seen_non_default_actions = set() def take_action(action, argument_strings, option_string=None): seen_actions.add(action) argument_values = self._get_values(action, argument_strings) # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): if conflict_action in seen_non_default_actions: msg = _('not allowed with argument %s') action_name = _get_action_name(conflict_action) raise ArgumentError(action, msg % action_name) # take the action if we didn't receive a SUPPRESS value # (e.g. from a default) if argument_values is not SUPPRESS: action(self, namespace, argument_values, option_string) # function to convert arg_strings into an optional action def consume_optional(start_index): # get the optional identified at this index option_tuple = option_string_indices[start_index] action, option_string, explicit_arg = option_tuple # identify additional optionals in the same arg string # (e.g. -xyz is the same as -x -y -z if no args are required) match_argument = self._match_argument action_tuples = [] while True: # if we found no optional action, skip it if action is None: extras.append(arg_strings[start_index]) return start_index + 1 # if there is an explicit argument, try to match the # optional's string arguments to only this if explicit_arg is not None: arg_count = match_argument(action, 'A') # if the action is a single-dash option and takes no # arguments, try to parse more single-dash options out # of the tail of the option string chars = self.prefix_chars if arg_count == 0 and option_string[1] not in chars: action_tuples.append((action, [], option_string)) char = option_string[0] option_string = char + explicit_arg[0] new_explicit_arg = explicit_arg[1:] or None optionals_map = self._option_string_actions if option_string in optionals_map: action = optionals_map[option_string] explicit_arg = new_explicit_arg else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: stop = start_index + 1 args = [explicit_arg] action_tuples.append((action, args, option_string)) break # error if a double-dash option did not use the # explicit argument else: msg = _('ignored explicit argument %r') raise ArgumentError(action, msg % explicit_arg) # if there is no explicit argument, try to match the # optional's string arguments with the following strings # if successful, exit the loop else: start = start_index + 1 selected_patterns = arg_strings_pattern[start:] self.active_actions = [action] # Added by argcomplete action.num_consumed_args = 0 # Added by argcomplete arg_count = match_argument(action, selected_patterns) stop = start + arg_count args = arg_strings[start:stop] # Begin added by argcomplete # If the pattern is not open (e.g. no + at the end), remove the action from active actions (since # it wouldn't be able to consume any more args) if not action_is_open(action): self.active_actions.remove(action) elif action.nargs == OPTIONAL and len(args) == 1: self.active_actions.remove(action) action.num_consumed_args = len(args) # End added by argcomplete action_tuples.append((action, args, option_string)) break # add the Optional to the list and return the index at which # the Optional's string args stopped assert action_tuples for action, args, option_string in action_tuples: take_action(action, args, option_string) return stop # the list of Positionals left to be parsed; this is modified # by consume_positionals() positionals = self._get_positional_actions() # function to convert arg_strings into positional actions def consume_positionals(start_index): # match as many Positionals as possible match_partial = self._match_arguments_partial selected_pattern = arg_strings_pattern[start_index:] arg_counts = match_partial(positionals, selected_pattern) # slice off the appropriate arg strings for each Positional # and add the Positional and its args to the list for action, arg_count in zip(positionals, arg_counts): if arg_count > 0: # Added by argcomplete self.active_actions = [action] # Added by argcomplete else: # Added by argcomplete self.active_actions.append(action) # Added by argcomplete args = arg_strings[start_index: start_index + arg_count] start_index += arg_count action.num_consumed_args = len(args) take_action(action, args) # slice off the Positionals that we just parsed and return the # index at which the Positionals' string args stopped positionals[:] = positionals[len(arg_counts):] return start_index # consume Positionals and Optionals alternately, until we have # passed the last option string extras = [] start_index = 0 if option_string_indices: max_option_string_index = max(option_string_indices) else: max_option_string_index = -1 while start_index <= max_option_string_index: # consume any Positionals preceding the next option next_option_string_index = min([ index for index in option_string_indices if index >= start_index]) if start_index != next_option_string_index: positionals_end_index = consume_positionals(start_index) # only try to parse the next optional if we didn't consume # the option string during the positionals parsing if positionals_end_index > start_index: start_index = positionals_end_index continue else: start_index = positionals_end_index # if we consumed all the positionals we could and we're not # at the index of an option string, there were extra arguments if start_index not in option_string_indices: strings = arg_strings[start_index:next_option_string_index] extras.extend(strings) start_index = next_option_string_index # consume the next optional and any arguments for it start_index = consume_optional(start_index) # consume any positionals following the last Optional stop_index = consume_positionals(start_index) # if we didn't consume all the argument strings, there were extras extras.extend(arg_strings[stop_index:]) # if we didn't use all the Positional objects, there were too few # arg strings supplied. if positionals: self.active_actions.append(positionals[0]) # Added by argcomplete self.error(_('too few arguments')) # make sure all required actions were present for action in self._actions: if action.required: if action not in seen_actions: name = _get_action_name(action) self.error(_('argument %s is required') % name) # make sure all required groups had one option present for group in self._mutually_exclusive_groups: if group.required: for action in group._group_actions: if action in seen_non_default_actions: break # if no actions were used, report the error else: names = [_get_action_name(action) for action in group._group_actions if action.help is not SUPPRESS] msg = _('one of the arguments %s is required') self.error(msg % ' '.join(names)) # return the updated namespace and the extra arguments return namespace, extras
def main(): parser = argparse.ArgumentParser( add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( '--help', action='help', default=argparse.SUPPRESS, help=argparse._('show this help message and exit') ) parser.add_argument( '-v', '--verbose', action='store_true', help='Display debug messages' ) parser.add_argument('-h', '--host', type=str, default='127.0.0.1', help='Listen host') parser.add_argument('-p', '--port', type=int, default=8080, help='Listen port') parser.add_argument('-w', '--workers', type=int, default=max(1, multiprocessing.cpu_count() - 1), help='Number of workers') parser.add_argument('--sentry-dsn', type=str, help='Sentry DSN') parser.add_argument('--redis-host', type=str, default='127.0.0.1', help='Redis host') parser.add_argument('--redis-port', type=str, default=6379, help='Redis port') parser.add_argument('--redis-password', type=str, default=None, help='Redis password') parser.add_argument('--disable-fluent', action='store_true', default=False, help='If set, do not send logs to fluent') parser.add_argument('--fluent-host', type=str, default='127.0.0.1', help='Fluentd host') parser.add_argument('--fluent-port', type=int, default=24224, help='Fluentd port') parser.add_argument('--auth-enabled', action='store_true', default=False, help='Enable authentication') parser.add_argument('--api-url', type=str, default='http://127.0.0.1:5000', help='APITaxi URL, used when authentication is enabled to retrieve users') args = parser.parse_args() if args.sentry_dsn: sentry_sdk.init(args.sentry_dsn, traces_sample_rate=1.0) loglevel = logging.DEBUG if args.verbose else logging.INFO logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'default': { '()': FormatWithPID, 'format': '%(asctime)s (pid %(pid)s) %(message)s' } }, 'handlers': { 'console': { 'level': loglevel, 'class': 'logging.StreamHandler', 'formatter': 'default', } }, 'loggers': { '': { 'handlers': ['console'], 'level': loglevel, } } }) if not args.auth_enabled: logger.warning('Authentication is not enabled') api_key = os.getenv('API_KEY') if args.auth_enabled and not api_key: parser.error('--enable-auth is set but API_KEY environment variable is not set') if args.disable_fluent: fluent = None else: fluent = FluentSender('geotaxi', host=args.fluent_host, port=args.fluent_port) redis = Redis( host=args.redis_host, port=args.redis_port, password=args.redis_password, socket_keepalive=True, ) worker = Worker( redis, fluent=fluent, auth_enabled=args.auth_enabled, api_url=args.api_url, api_key=api_key ) run_server(args.workers, args.host, args.port, worker)
def format_helptext(txt): return argparse._(txt + '\n\n')