我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用argparse.PARSER。
def register_args(subparsers): class SubcommandHelpFormatter(argparse.RawDescriptionHelpFormatter): def _format_action(self, action): parts = super()._format_action(action) if action.nargs == argparse.PARSER: parts = "\n".join([line for line in parts.split("\n")[1:]]) parts += "\n\nSpider safely!" return parts parser = subparsers.add_parser(name='observe', help="Passively observe network traffic", formatter_class=SubcommandHelpFormatter) parser.add_argument('--list-chains', help="Prints a list of available chains", action='store_true') parser.add_argument('-i', '--interface', default="eth0", help="The interface to use for the observer. (Default: eth0)") parser.add_argument('--output', default='/dev/stdout', metavar='OUTPUTFILE', help=("The file to output results data to. " "Defaults to standard output.")) parser.add_argument('chains', nargs='*', help="Observer chains to use") # Set the command entry point parser.set_defaults(cmd=run_observer)
def _format_action(self, action): parts = super(SubcommandHelpFormatter, self)._format_action(action) if action.nargs == argparse.PARSER: parts = "\n".join(parts.split("\n")[1:]) return parts
def register_args(subparsers): class SubcommandHelpFormatter(argparse.RawDescriptionHelpFormatter): def _format_action(self, action): parts = super()._format_action(action) if action.nargs == argparse.PARSER: parts = "\n".join([line for line in parts.split("\n")[1:]]) parts += "\n\nSpider safely!" return parts parser = subparsers.add_parser(name='measure', help="Perform a PATHspider measurement", formatter_class=SubcommandHelpFormatter) parser.add_argument('-i', '--interface', default="eth0", help="The interface to use for the observer. (Default: eth0)") parser.add_argument('-w', '--workers', type=int, default=100, help="Number of workers to use. (Default: 100)") parser.add_argument('--input', default='/dev/stdin', metavar='INPUTFILE', help=("A file containing a list of PATHspider jobs. " "Defaults to standard input.")) parser.add_argument('--csv-input', action='store_true', help=("Indicate CSV format.")) parser.add_argument('--output', default='/dev/stdout', metavar='OUTPUTFILE', help=("The file to output results data to. " "Defaults to standard output.")) parser.add_argument('--output-flows', action='store_true', help="Include flow results in output.") # Set the command entry point parser.set_defaults(cmd=run_measurement) # Add plugins plugin_subparsers = parser.add_subparsers(title="Plugins", description="The following plugins are available for use:", metavar='PLUGIN', help='plugin to use') for plugin in plugins: plugin.register_args(plugin_subparsers)
def handle_args(argv): class SubcommandHelpFormatter(argparse.RawDescriptionHelpFormatter): def _format_action(self, action): parts = super()._format_action(action) if action.nargs == argparse.PARSER: parts = "\n".join([line for line in parts.split("\n")[1:]]) parts += "\n\nSpider safely!" return parts parser = argparse.ArgumentParser(description=('PATHspider will spider the ' 'paths.'), formatter_class=SubcommandHelpFormatter) parser.add_argument("--verbose", action="store_true", help="Enable verbose logging") # Add commands subparsers = parser.add_subparsers(title="Commands", metavar='COMMAND', help='command to run') # Register commands arguments for cmd in cmds: cmd.register_args(subparsers) args = parser.parse_args(argv[1:]) # Set up logging logging.basicConfig() if args.verbose: logging.getLogger().setLevel(logging.DEBUG) else: logging.getLogger().setLevel(logging.INFO) # If it's a valid command, run it, or help the user if not if hasattr(args, 'cmd'): args.cmd(args) else: parser.print_help()
def test_invalid_add_argument(self): parser = ErrorRaisingArgumentParser() group = parser.add_mutually_exclusive_group() add_argument = group.add_argument raises = self.assertRaises raises(ValueError, add_argument, '--foo', required=True) raises(ValueError, add_argument, 'bar') raises(ValueError, add_argument, 'bar', nargs='+') raises(ValueError, add_argument, 'bar', nargs=1) raises(ValueError, add_argument, 'bar', nargs=argparse.PARSER)
def _format_action_invocation(self, action): if action.nargs == argparse.PARSER: return '' if not action.option_strings or action.nargs == 0: return super()._format_action_invocation(action) return '%s %s' % ( ', '.join(action.option_strings), self._format_args( action, self._get_default_metavar_for_optional(action)))