예제 #1
0
 def _OptInd(self):
     # type: () -> int
     """Returns OPTIND that's >= 1, or -1 if it's invalid."""
     # Note: OPTIND could be value.Int?
     try:
         result = state.GetInteger(self.mem, 'OPTIND')
     except error.Runtime as e:
         self.errfmt.Print_(e.UserErrorString())
         result = -1
     return result
예제 #2
0
    def Run(self, cmd_val):
        # type: (cmd_value__Argv) -> int
        arg_r = args.Reader(cmd_val.argv, spids=cmd_val.arg_spids)
        arg_r.Next()

        # NOTE: If first char is a colon, error reporting is different.  Alpine
        # might not use that?
        spec_str = arg_r.ReadRequired('requires an argspec')

        var_name, var_spid = arg_r.ReadRequired2(
            'requires the name of a variable to set')

        try:
            spec = self.spec_cache[spec_str]
        except KeyError:
            spec = _ParseOptSpec(spec_str)
            self.spec_cache[spec_str] = spec

        # TODO: OPTIND could be value.Int?
        try:
            optind = state.GetInteger(self.mem, 'OPTIND')
        except error.Runtime as e:
            self.errfmt.Print_(e.UserErrorString())
            return 1

        user_argv = self.mem.GetArgv() if arg_r.AtEnd() else arg_r.Rest()
        #util.log('user_argv %s', user_argv)
        status, opt_char, optarg, optind = _GetOpts(spec, user_argv, optind,
                                                    self.errfmt)

        # Bug fix: bash-completion uses a *local* OPTIND !  Not global.
        state.SetStringDynamic(self.mem, 'OPTARG', optarg)
        state.SetStringDynamic(self.mem, 'OPTIND', str(optind))
        if match.IsValidVarName(var_name):
            state.SetStringDynamic(self.mem, var_name, opt_char)
        else:
            # NOTE: The builtin has PARTIALLY filed.  This happens in all shells
            # except mksh.
            raise error.Usage('got invalid variable name %r' % var_name,
                              span_id=var_spid)
        return status