Exemple #1
0
def addArguments(group, options, required, topGroup=None):
    """
        Registers a list of options to the specified group. Nodes
        are either an instance of ParseArgument or a list of
        ParseArguments. The list form is considered to be a
        mutually exclusive group of arguments.
    """
    for option in options:
        if isinstance(option, parsing.MutuallyExclusiveGroup):
            exGrp = (topGroup or group).add_mutually_exclusive_group()
            parsing.registerParserHelpers(exGrp)
            addArguments(exGrp, option.arguments, required, topGroup=group)
        else:
            assert not required in option.kwargs
            if option.args[0][0] == '-':
                group.add_argument(*(option.args),
                                   required=required,
                                   **(option.kwargs))
            else:
                if required:
                    group.add_argument(*(option.args), **(option.kwargs))
                else:
                    group.add_argument(*(option.args),
                                       nargs='?',
                                       **(option.kwargs))
def addArguments(group, options, required, topGroup=None):
    """
        Registers a list of options to the specified group. Nodes
        are either an instance of ParseArgument or a list of
        ParseArguments. The list form is considered to be a
        mutually exclusive group of arguments.
    """
    for option in options:
        if isinstance(option, parsing.MutuallyExclusiveGroup):
            exGrp = (topGroup or group).add_mutually_exclusive_group()
            parsing.registerParserHelpers(exGrp)
            addArguments(exGrp, option.arguments, required, topGroup=group)
        else:
            assert not required in option.kwargs
            if option.args[0][0] == '-':
                group.add_argument(*(option.args), required=required, **(option.kwargs))
            else:
                if required:
                    group.add_argument(*(option.args), **(option.kwargs))
                else:
                    group.add_argument(*(option.args), nargs='?', **(option.kwargs))
Exemple #3
0
    def parse(self, argv, fromfile_prefix='+'):
        if len(argv) <= 1 or argv[1] == '--help' or argv[1] == '-h':
            raise exceptions.UsageError(
                "TradeDangerous provides a set of trade database "
                "facilities for Elite:Dangerous.", self.usage(argv))

        ### TODO: Break this model up a bit more so that
        ### we just try and import the command you specify,
        ### and only worry about an index when that fails or
        ### the user requests usage.
        cmdName, cmdModule = argv[1].casefold(), None
        try:
            cmdModule = commandIndex[cmdName]
        except KeyError:
            pass

        if not cmdModule:
            candidates = []
            for name, module in commandIndex.items():
                if name.startswith(cmdName):
                    candidates.append([name, module])
            if not candidates:
                raise exceptions.CommandLineError(
                    "Unrecognized command, '{}'".format(cmdName),
                    self.usage(argv))
            if len(candidates) > 1:
                raise exceptions.CommandLineError(
                    "Ambiguous command, '{}', "
                    "could match: {}".format(
                        cmdName, ', '.join(c[0] for c in candidates)),
                    self.usage(argv))
            argv[1] = cmdName = candidates[0][0]
            cmdModule = candidates[0][1]

        class ArgParser(argparse.ArgumentParser):
            def error(self, message):
                raise exceptions.CommandLineError(message, self.format_usage())

        parser = ArgParser(
            description="TradeDangerous: " + cmdName,
            add_help=False,
            epilog='Use {prog} {cmd} -h for more help'.format(prog=argv[0],
                                                              cmd=argv[1]),
            fromfile_prefix_chars=fromfile_prefix,
        )
        parser.set_defaults(_editing=False)
        parsing.registerParserHelpers(parser)

        subParsers = parser.add_subparsers(title='Command Options')
        subParser = subParsers.add_parser(
            cmdModule.name,
            help=cmdModule.help,
            add_help=False,
            epilog=cmdModule.epilog,
        )
        parsing.registerParserHelpers(subParser)

        arguments = cmdModule.arguments
        if arguments:
            argParser = subParser.add_argument_group('Required Arguments')
            addArguments(argParser, arguments, True)

        switches = cmdModule.switches
        if switches:
            switchParser = subParser.add_argument_group('Optional Switches')
            addArguments(switchParser, switches, False)

        # Arguments common to all subparsers.
        stdArgs = subParser.add_argument_group('Common Switches')
        stdArgs.add_argument(
            '-h',
            '--help',
            help='Show this help message and exit.',
            action=HelpAction,
            nargs=0,
        )
        stdArgs.add_argument(
            '--debug',
            '-w',
            help='Enable/raise level of diagnostic output.',
            default=0,
            required=False,
            action='count',
        )
        stdArgs.add_argument(
            '--detail',
            '-v',
            help='Increase level  of detail in output.',
            default=0,
            required=False,
            action='count',
        )
        stdArgs.add_argument(
            '--quiet',
            '-q',
            help='Reduce level of detail in output.',
            default=0,
            required=False,
            action='count',
        )
        stdArgs.add_argument(
            '--db',
            help='Specify location of the SQLite database.',
            default=None,
            dest='dbFilename',
            type=str,
        )
        stdArgs.add_argument(
            '--cwd',
            '-C',
            help='Change the working directory file accesses are made from.',
            type=str,
            required=False,
        )
        stdArgs.add_argument(
            '--link-ly',
            '-L',
            help='Maximum lightyears between systems to be considered linked.',
            type=float,
            default=None,
            dest='maxSystemLinkLy',
        )

        fromfilePath = _findFromFile(cmdModule.name)
        if fromfilePath:
            argv.insert(2, '{}{}'.format(fromfile_prefix, fromfilePath))
        properties = parser.parse_args(argv[1:])

        parsed = CommandEnv(properties, argv, cmdModule)
        parsed.DEBUG0("Command line was: {}", argv)

        return parsed
    def parse(self, argv, fromfile_prefix='+'):
        if len(argv) <= 1 or argv[1] == '--help' or argv[1] == '-h':
            raise exceptions.UsageError(
                    "TradeDangerous provides a set of trade database "
                    "facilities for Elite:Dangerous.", self.usage(argv))

        ### TODO: Break this model up a bit more so that
        ### we just try and import the command you specify,
        ### and only worry about an index when that fails or
        ### the user requests usage.
        cmdName, cmdModule = argv[1].casefold(), None
        try:
            cmdModule = commandIndex[cmdName]
        except KeyError:
            pass

        if not cmdModule:
            candidates = []
            for name, module in commandIndex.items():
                if name.startswith(cmdName):
                    candidates.append([name, module])
            if not candidates:
                raise exceptions.CommandLineError(
                        "Unrecognized command, '{}'".format(cmdName),
                        self.usage(argv)
                )
            if len(candidates) > 1:
                raise exceptions.CommandLineError(
                        "Ambiguous command, '{}', "
                        "could match: {}".format(
                            cmdName,
                            ', '.join(c[0] for c in candidates)
                        ),
                        self.usage(argv)
                )
            argv[1] = cmdName = candidates[0][0]
            cmdModule = candidates[0][1]

        class ArgParser(argparse.ArgumentParser):
            def error(self, message):
                raise exceptions.CommandLineError(message, self.format_usage())

        parser = ArgParser(
                    description="TradeDangerous: "+cmdName,
                    add_help=False,
                    epilog='Use {prog} {cmd} -h for more help'.format(
                            prog=argv[0], cmd=argv[1]
                        ),
                    fromfile_prefix_chars=fromfile_prefix,
                )
        parser.set_defaults(_editing=False)
        parsing.registerParserHelpers(parser)

        subParsers = parser.add_subparsers(title='Command Options')
        subParser = subParsers.add_parser(cmdModule.name,
                                    help=cmdModule.help,
                                    add_help=False,
                                    epilog=cmdModule.epilog,
                                    )
        parsing.registerParserHelpers(subParser)

        arguments = cmdModule.arguments
        if arguments:
            argParser = subParser.add_argument_group('Required Arguments')
            addArguments(argParser, arguments, True)

        switches = cmdModule.switches
        if switches:
            switchParser = subParser.add_argument_group('Optional Switches')
            addArguments(switchParser, switches, False)

        # Arguments common to all subparsers.
        stdArgs = subParser.add_argument_group('Common Switches')
        stdArgs.add_argument('-h', '--help',
                    help='Show this help message and exit.',
                    action=HelpAction, nargs=0,
                )
        stdArgs.add_argument('--debug', '-w',
                    help='Enable/raise level of diagnostic output.',
                    default=0, required=False, action='count',
                )
        stdArgs.add_argument('--detail', '-v',
                    help='Increase level  of detail in output.',
                    default=0,required=False, action='count',
                )
        stdArgs.add_argument('--quiet', '-q',
                    help='Reduce level of detail in output.',
                    default=0, required=False, action='count',
                )
        stdArgs.add_argument('--db',
                    help='Specify location of the SQLite database.',
                    default=None, dest='dbFilename', type=str,
                )
        stdArgs.add_argument('--cwd', '-C',
                    help='Change the working directory file accesses are made from.',
                    type=str, required=False,
                )
        stdArgs.add_argument('--link-ly', '-L',
                    help='Maximum lightyears between systems to be considered linked.',
                    type=float,
                    default=None, dest='maxSystemLinkLy',
                )

        fromfilePath = _findFromFile(cmdModule.name)
        if fromfilePath:
            argv.insert(2, '{}{}'.format(fromfile_prefix, fromfilePath))
        properties = parser.parse_args(argv[1:])

        parsed = CommandEnv(properties, argv, cmdModule)
        parsed.DEBUG0("Command line was: {}", argv)

        return parsed