Python argparse.PARSER() Examples

The following are code examples for showing how to use argparse.PARSER(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: pathspider   Author: mami-project   File: observe.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 2
Project: argparseinator   Author: ellethee   File: utils.py    (license) View Source Project 5 votes vote down vote up
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 
Example 3
Project: pathspider   Author: mami-project   File: measure.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 4
Project: pathspider   Author: mami-project   File: base.py    (license) View Source Project 5 votes vote down vote up
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() 
Example 5
Project: zippy   Author: securesystemslab   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 6
Project: oil   Author: oilshell   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 7
Project: python2-tracer   Author: extremecoders-re   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 8
Project: web_ctp   Author: molebot   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 9
Project: pefile.pypy   Author: cloudtracer   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 10
Project: ouroboros   Author: pybee   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 11
Project: ndk-python   Author: gittor   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 12
Project: kbe_server   Author: xiaohaoppy   File: test_argparse.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 13
Project: drill   Author: rr-   File: __main__.py    (license) View Source Project 5 votes vote down vote up
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)))