Example #1
0
def check_builtin(option, opt, value):
    (cvt, what) = _builtin_cvt[option.type]
    try:
        return cvt(value)
    except ValueError:
        raise OptionValueError(
            _("option %s: invalid %s value: %r") % (opt, what, value))
Example #2
0
def check_choice(option, opt, value):
    if value in option.choices:
        return value
    else:
        choices = ", ".join(map(repr, option.choices))
        raise OptionValueError(
            _("option %s: invalid choice: %r (choose from %s)") %
            (opt, value, choices))
Example #3
0
 def set_usage(self, usage):
     if usage is None:
         self.usage = _("%prog [options]")
     elif usage is SUPPRESS_USAGE:
         self.usage = None
     # For backwards compatibility with Optik 1.3 and earlier.
     elif usage.lower().startswith("usage: "):
         self.usage = usage[7:]
     else:
         self.usage = usage
Example #4
0
 def set_usage(self, usage):
     if usage is None:
         self.usage = _("%prog [options]")
     elif usage is SUPPRESS_USAGE:
         self.usage = None
     # For backwards compatibility with Optik 1.3 and earlier.
     elif usage.lower().startswith("usage: "):
         self.usage = usage[7:]
     else:
         self.usage = usage
Example #5
0
    def _process_short_opts(self, rargs, values):
        arg = rargs.pop(0)
        stop = False
        i = 1
        for ch in arg[1:]:
            opt = "-" + ch
            option = self._short_opt.get(opt)
            i += 1  # we have consumed a character

            if not option:
                raise BadOptionError(opt)
            if option.takes_value():
                # Any characters left in arg?  Pretend they're the
                # next arg, and stop consuming characters of arg.
                if i < len(arg):
                    rargs.insert(0, arg[i:])
                    stop = True

                nargs = option.nargs
                if len(rargs) < nargs:
                    if nargs == 1:
                        self.error(_("%s option requires an argument") % opt)
                    else:
                        self.error(
                            _("%s option requires %d arguments") %
                            (opt, nargs))
                elif nargs == 1:
                    value = rargs.pop(0)
                else:
                    value = tuple(rargs[0:nargs])
                    del rargs[0:nargs]

            else:  # option doesn't take a value
                value = None

            option.process(opt, value, values, self)

            if stop:
                break
Example #6
0
    def _process_short_opts(self, rargs, values):
        arg = rargs.pop(0)
        stop = False
        i = 1
        for ch in arg[1:]:
            opt = "-" + ch
            option = self._short_opt.get(opt)
            i += 1                      # we have consumed a character

            if not option:
                raise BadOptionError(opt)
            if option.takes_value():
                # Any characters left in arg?  Pretend they're the
                # next arg, and stop consuming characters of arg.
                if i < len(arg):
                    rargs.insert(0, arg[i:])
                    stop = True

                nargs = option.nargs
                if len(rargs) < nargs:
                    if nargs == 1:
                        self.error(_("%s option requires an argument") % opt)
                    else:
                        self.error(_("%s option requires %d arguments")
                                   % (opt, nargs))
                elif nargs == 1:
                    value = rargs.pop(0)
                else:
                    value = tuple(rargs[0:nargs])
                    del rargs[0:nargs]

            else:                       # option doesn't take a value
                value = None

            option.process(opt, value, values, self)

            if stop:
                break
Example #7
0
    def _process_long_opt(self, rargs, values):
        arg = rargs.pop(0)

        # Value explicitly attached to arg?  Pretend it's the next
        # argument.
        if "=" in arg:
            (opt, next_arg) = arg.split("=", 1)
            rargs.insert(0, next_arg)
            had_explicit_value = True
        else:
            opt = arg
            had_explicit_value = False

        opt = self._match_long_opt(opt)
        option = self._long_opt[opt]
        if option.takes_value():
            nargs = option.nargs
            if len(rargs) < nargs:
                if nargs == 1:
                    self.error(_("%s option requires an argument") % opt)
                else:
                    self.error(
                        _("%s option requires %d arguments") % (opt, nargs))
            elif nargs == 1:
                value = rargs.pop(0)
            else:
                value = tuple(rargs[0:nargs])
                del rargs[0:nargs]

        elif had_explicit_value:
            self.error(_("%s option does not take a value") % opt)

        else:
            value = None

        option.process(opt, value, values, self)
Example #8
0
    def _process_long_opt(self, rargs, values):
        arg = rargs.pop(0)

        # Value explicitly attached to arg?  Pretend it's the next
        # argument.
        if "=" in arg:
            (opt, next_arg) = arg.split("=", 1)
            rargs.insert(0, next_arg)
            had_explicit_value = True
        else:
            opt = arg
            had_explicit_value = False

        opt = self._match_long_opt(opt)
        option = self._long_opt[opt]
        if option.takes_value():
            nargs = option.nargs
            if len(rargs) < nargs:
                if nargs == 1:
                    self.error(_("%s option requires an argument") % opt)
                else:
                    self.error(_("%s option requires %d arguments")
                               % (opt, nargs))
            elif nargs == 1:
                value = rargs.pop(0)
            else:
                value = tuple(rargs[0:nargs])
                del rargs[0:nargs]

        elif had_explicit_value:
            self.error(_("%s option does not take a value") % opt)

        else:
            value = None

        option.process(opt, value, values, self)
Example #9
0
 def format_option_help(self, formatter=None):
     if formatter is None:
         formatter = self.formatter
     formatter.store_option_strings(self)
     result = []
     result.append(formatter.format_heading(_("Options")))
     formatter.indent()
     if self.option_list:
         result.append(OptionContainer.format_option_help(self, formatter))
         result.append("\n")
     for group in self.option_groups:
         result.append(group.format_help(formatter))
         result.append("\n")
     formatter.dedent()
     # Drop the last "\n", or the header if no options or option groups:
     return "".join(result[:-1])
Example #10
0
 def format_option_help(self, formatter=None):
     if formatter is None:
         formatter = self.formatter
     formatter.store_option_strings(self)
     result = []
     result.append(formatter.format_heading(_("Options")))
     formatter.indent()
     if self.option_list:
         result.append(OptionContainer.format_option_help(self, formatter))
         result.append("\n")
     for group in self.option_groups:
         result.append(group.format_help(formatter))
         result.append("\n")
     formatter.dedent()
     # Drop the last "\n", or the header if no options or option groups:
     return "".join(result[:-1])
Example #11
0
 def _add_version_option(self):
     self.add_option("--version",
                     action="version",
                     help=_("show program's version number and exit"))
Example #12
0
def check_builtin(option, opt, value):
    (cvt, what) = _builtin_cvt[option.type]
    try:
        return cvt(value)
    except ValueError:
        raise OptionValueError(_("option %s: invalid %s value: %r") % (opt, what, value))
Example #13
0
    else:  # decimal
        radix = 10

    return type(val, radix)


def _parse_int(val):
    return _parse_num(val, int)


def _parse_long(val):
    return _parse_num(val, long)


_builtin_cvt = {
    "int": (_parse_int, _("integer")),
    "long": (_parse_long, _("long integer")),
    "float": (float, _("floating-point")),
    "complex": (complex, _("complex")),
}


def check_builtin(option, opt, value):
    (cvt, what) = _builtin_cvt[option.type]
    try:
        return cvt(value)
    except ValueError:
        raise OptionValueError(_("option %s: invalid %s value: %r") % (opt, what, value))


def check_choice(option, opt, value):
Example #14
0
 def _add_version_option(self):
     self.add_option("--version",
                     action="version",
                     help=_("show program's version number and exit"))
Example #15
0
def check_choice(option, opt, value):
    if value in option.choices:
        return value
    else:
        choices = ", ".join(map(repr, option.choices))
        raise OptionValueError(_("option %s: invalid choice: %r (choose from %s)") % (opt, value, choices))
Example #16
0
 def _add_help_option(self):
     self.add_option("-h",
                     "--help",
                     action="help",
                     help=_("show this help message and exit"))
Example #17
0
 def format_usage(self, usage):
     return "%s  %s\n" % (self.format_heading(_("Usage")), usage)
Example #18
0
 def format_usage(self, usage):
     return _("Usage: %s\n") % usage
Example #19
0
    else:  # decimal
        radix = 10

    return type(val, radix)


def _parse_int(val):
    return _parse_num(val, int)


def _parse_long(val):
    return _parse_num(val, long)


_builtin_cvt = {
    "int": (_parse_int, _("integer")),
    "long": (_parse_long, _("long integer")),
    "float": (float, _("floating-point")),
    "complex": (complex, _("complex"))
}


def check_builtin(option, opt, value):
    (cvt, what) = _builtin_cvt[option.type]
    try:
        return cvt(value)
    except ValueError:
        raise OptionValueError(
            _("option %s: invalid %s value: %r") % (opt, what, value))

Example #20
0
 def format_usage(self, usage):
     return _("Usage: %s\n") % usage
Example #21
0
 def format_usage(self, usage):
     return "%s  %s\n" % (self.format_heading(_("Usage")), usage)
Example #22
0
 def _add_help_option(self):
     self.add_option("-h", "--help",
                     action="help",
                     help=_("show this help message and exit"))