コード例 #1
0
	def __init__(self, infile):
		self.description = ""
		self.opt_list = []
		self.usage_line = []
		self.arg_list = []
		self.vararg = None
		self.version = []
		self.settings = settings.BopSettings()

		current_block = None
		optional_arg_given = False

		settings_block_given = False
		description_block_given = False
		version_block_given = False
		options_block_given = False
		arguments_block_given = False

		insert_space_in_description = False
		insert_space_in_version = False

		opt_reader = preparser.LinePreParser()

		cnt = err.ParseCount()

		for l in infile.readlines():
			cnt.inc()
			cnt.line = l
			line = opt_reader.parse_line(cnt, l)
			cnt.line = line
			if len(line) == 0:
				continue;

			if current_block == None:
				if len(line) > 1:
					raise err.InvalidBlockBeginLine(cnt, line)
				line[0] = line[0].upper()
				if line[0] == "SETTINGS_BEGIN":
					test(not settings_block_given, err.DuplicateBlock, (cnt, "SETTINGS"))
					current_block = "SETTINGS_BLOCK"
					settings_block_given = True
				elif line[0] == "DESCRIPTION_BEGIN":
					test(not description_block_given, err.DuplicateBlock, (cnt, "DESCRIPTION"))
					current_block = "DESCRIPTION_BLOCK"
					description_block_given = True
				elif line[0] == "VERSION_BEGIN":
					test(not version_block_given, err.DuplicateBlock, (cnt, "VERSION"))
					current_block = "VERSION_BLOCK"
					version_block_given = True
				elif line[0] == "OPTIONS_BEGIN":
					test(not options_block_given, err.DuplicateBlock, (cnt, "OPTIONS"))
					current_block = "OPTIONS_BLOCK"
					options_block_given = True
				elif line[0] == "ARGUMENTS_BEGIN":
					test(not arguments_block_given, err.DuplicateBlock, (cnt, "ARGUMENTS"))
					current_block = "ARGUMENTS_BLOCK"
					arguments_block_given = True
				else:
					raise err.UnknownDescriptor(cnt, line[0])
			elif current_block == "SETTINGS_BLOCK":
				if line[0] == "SETTINGS_END":
					current_block = None
					continue
				if line[0] == "REQUIRED_VERSION":
					test(len(line) == 2, err.InvalidBopMinVersLine, (cnt, len(line)))
					settings.BopRequiredVersionChecker(cnt, *(line[1:]))
				elif line[0] == "WRAP_WIDTH":
					test(len(line) == 2, err.InvalidBopWrapWidthLine, (cnt, len(line)))
					try:
						self.settings.wrap_width = int(line[1])
					except:
						raise err.InvalidBopWrapWidth(cnt, line[1])
					test(self.settings.wrap_width >= 30, err.InvalidBopWrapWidth, (cnt, self.settings.wrap_width))
				elif line[0] == "AUTO_SHORT_OPTS":
					test(len(line) == 2, err.InvalidBopAutoShortOptsLine, (cnt, len(line)))
					try:
						exec("self.settings.auto_short_opts = " + line[1].capitalize())
					except (NameError, SyntaxError):
						raise err.InvalidBopAutoShortOpts(cnt, line[1])
					test(isinstance(self.settings.auto_short_opts, bool), err.InvalidBopAutoShortOpts, (cnt, line[1]))
				elif line[0] == "ONE_DASH_LONG_OPTS":
					test(len(line) == 2, err.InvalidBopOneDashLongOptsLine, (cnt, len(line)))
					try:
						exec("self.settings.one_dash_long_opts = " + line[1].capitalize())
					except (NameError, SyntaxError):
						raise err.InvalidBopOneDashLongOpts(cnt, line[1])
					test(isinstance(self.settings.one_dash_long_opts, bool), err.InvalidBopOneDashLongOpts, (cnt, line[1]))
				elif line[0] == "IN_FUNCTION":
					test(len(line) == 2, err.InvalidBopInFunctionLine, (cnt, len(line)))
					try:
						exec("self.settings.in_function = " + line[1].capitalize())
					except (NameError, SyntaxError):
						raise err.InvalidBopInFunction(cnt, line[1])
					test(isinstance(self.settings.in_function, bool), err.InvalidBopInFunction, (cnt, line[1]))
				elif line[0] == "ERR_CODE_OPT_INVALID":
					test(len(line) == 2, err.InvalidBopErrCodeLine, (cnt, len(line)))
					try:
						self.settings.err_code_opt_invalid = int(line[1])
					except:
						raise err.InvalidBopErrCode(cnt, line[1])
				elif line[0] == "ERR_CODE_OPT_TYPE":
					test(len(line) == 2, err.InvalidBopErrCodeLine, (cnt, len(line)))
					try:
						self.settings.err_code_opt_type = int(line[1])
					except:
						raise err.InvalidBopErrCode(cnt, line[1])
				elif line[0] == "ERR_CODE_OPT_RANGE":
					test(len(line) == 2, err.InvalidBopErrCodeLine, (cnt, len(line)))
					try:
						self.settings.err_code_opt_range = int(line[1])
					except:
						raise err.InvalidBopErrCode(cnt, line[1])
				elif line[0] == "ERR_CODE_ARG_NUM":
					test(len(line) == 2, err.InvalidBopErrCodeLine, (cnt, len(line)))
					try:
						self.settings.err_code_arg_num = int(line[1])
					except:
						raise err.InvalidBopErrCode(cnt, line[1])

				else:
					raise err.UnknownSetting(cnt, line[0])
			elif current_block == "DESCRIPTION_BLOCK":
				if line[0] == "DESCRIPTION_END":
					current_block = None
					continue
				for i in range(len(line)):
					if len(line[i]) == 0:
						continue
					if insert_space_in_description and not line[i][0].isspace():
						self.description += " "
					self.description += line[i]
					if line[i][-1].isspace():
						insert_space_in_description = False
					else:
						insert_space_in_description = True
			elif current_block == "VERSION_BLOCK":
				if line[0] == "VERSION_END":
					current_block = None
					continue
				#test(len(line) == 1, err.InvalidVersLine, (cnt, len(line)))
				insert_space_in_version = False
				vline = ""
				for i in range(len(line)):
					if len(line[i]) == 0:
						continue
					if insert_space_in_version and not line[i][0].isspace():
						vline += " "
					vline += line[i]
					if line[i][-1].isspace():
						insert_space_in_version = False
					else:
						insert_space_in_version = True
				self.version.append(vline)
			elif current_block == "OPTIONS_BLOCK":
				if line[0] == "OPTIONS_END":
					current_block = None
					continue
				test(len(line) == opt.BopOption.required_args, err.InvalidOptLine, (cnt, len(line)))
				myopt = opt.BopOption(cnt, self.settings, *line)

				test(myopt.opt_name_alt == None or myopt.opt_name != myopt.opt_name_alt, err.DuplicateOpt, (cnt, myopt.opt_name))
				test(myopt.short == None or myopt.short_alt == None or myopt.short != myopt.short_alt, err.DuplicateShortOpt, (cnt, myopt.short))

				for o in self.opt_list:
					test(myopt.name != o.name, err.DuplicateOpt, (cnt, myopt.name))
					test(myopt.short == None or myopt.short != o.short, err.DuplicateShortOpt, (cnt, myopt.short))

					test(myopt.opt_name_alt == None or myopt.opt_name_alt != o.opt_name, err.DuplicateOpt, (cnt, myopt.opt_name_alt))
					test(o.opt_name_alt == None or myopt.opt_name != o.opt_name_alt, err.DuplicateOpt, (cnt, myopt.opt_name))
					test(myopt.opt_name_alt == None or o.opt_name_alt == None or myopt.opt_name_alt != o.opt_name_alt, err.DuplicateOpt, (cnt, myopt.opt_name_alt))

					test(myopt.short_alt == None or o.short == None or myopt.short_alt != o.short, err.DuplicateShortOpt, (cnt, myopt.short_alt))
					test(myopt.short == None or o.short_alt == None or myopt.short != o.short_alt, err.DuplicateShortOpt, (cnt, myopt.short))
					test(myopt.short_alt == None or o.short_alt == None or myopt.short_alt != o.short_alt, err.DuplicateShortOpt, (cnt, myopt.short_alt))
				self.opt_list.append(myopt)
			elif current_block == "ARGUMENTS_BLOCK":
				if line[0] == "ARGUMENTS_END":
					current_block = None
					continue
				if line[0].upper() == "VARARGS" or line[0] == "@":
					test(self.vararg == None, err.MultipleVararg, (cnt, ""))
					test(len(line) == arg.BopVararg.required_args, err.InvalidVarargLine, (cnt, len(line)))
					myvarg = arg.BopVararg(cnt, self.settings, *line)
					if not myvarg.mandatory:
						optional_arg_given = True
					else:
						test(not optional_arg_given, err.MandVarargAfterOptArg, (cnt, ""))
					self.vararg = myvarg
				else:
					test(self.vararg == None, err.ArgAfterVararg, (cnt, ""))
					test(len(line) == arg.BopArgument.required_args, err.InvalidArgLine, (cnt, len(line)))
					myarg = arg.BopArgument(cnt, self.settings, *line)
					if not myarg.mandatory:
						optional_arg_given = True
					else:
						test(not optional_arg_given, err.MandArgAfterOptArg, (cnt, ""))
					for a in self.arg_list:
						test(myarg.name != a.name, err.DuplicateArg, (cnt, myarg.name))
					self.arg_list.append(myarg)
			else:
				raise err.Bug(cnt, "")

		test(current_block == None, err.BlockNotClosed, (cnt, current_block))

		for o in self.opt_list:
			for a in self.arg_list:
				test(o.name != a.name, err.DuplicateOptArg, (cnt, o.name))

		if self.settings.auto_short_opts:
			for o in self.opt_list:
				if (o.short != None or o.force_noshort) and \
					(o.name_alt == None or o.short_alt != None or o.force_noshort_alt):
					continue
				if not (o.short != None or o.force_noshort):
					for c in o.name:
						if not check.optname_short(c):
							continue
						found = False
						for o1 in self.opt_list:
							if c == o1.short or c == o1.short_alt:
								found = True
								break
						if not found:
							o.short = c
							break
				if not (o.name_alt == None or o.short_alt != None or o.force_noshort_alt):
					for c in o.name_alt:
						if not check.optname_short(c):
							continue
						found = False
						for o1 in self.opt_list:
							if c == o1.short or c == o1.short_alt:
								found = True
								break
						if not found:
							o.short_alt = c
							break

		for o in self.opt_list:
			self.usage_line.append(o.gen_usage_line())
			if o.name_alt != None:
				self.usage_line.append(o.gen_usage_line_alt())


		self.usage_line.append(["--version", "output version information and exit"])
		self.usage_line.append(["--help", "print this help and exit"])

		if len(self.version) == 0:
			self.version.append("Version information unspecified")

		if not self.settings.in_function:
			self.exit_command = "exit"
		else:
			self.exit_command = "BASH_OPTPARSE_EARLY_RETURN=true; return"
コード例 #2
0
    def __init__(self, cnt, settings, name, arg_type, arg_name, arg_range,
                 default_arg, desc):
        self.settings = settings
        self.name = name
        self.short = ""
        self.force_noshort = False
        self.short_only = False
        self.arg_type = str(arg_type)
        self.is_array = False
        self.arg_name = str(arg_name)
        self.arg_range = str(arg_range)
        self.default_arg = str(default_arg)
        self.desc = str(desc)

        self.arg_range_compiled_regex = None
        self.open_boundary = [False, False]

        splitnamealt = str(name).split("|")
        test(
            len(splitnamealt) == 1 or len(splitnamealt) == 2,
            err.WrongSplitAltName, (cnt, name))

        self.name = splitnamealt[0]
        splitname = str(splitnamealt[0]).split(",")
        test(
            len(splitname) == 1 or len(splitname) == 2, err.WrongSplitName,
            (cnt, splitnamealt[0]))
        if len(splitname) > 1:
            self.name = splitname[0]
            self.short = splitname[1]
        elif len(self.name) == 1:
            self.short = self.name
            self.short_only = True

        test(check.var_name(self.name), err.InvalidName, (cnt, self.name))
        test(self.name != "help", err.ReservedOptName, (cnt, self.name))
        test(self.name != "version", err.ReservedOptName, (cnt, self.name))
        test(check.var_name_is_reserved(self.name), err.ReservedVarName,
             (cnt, self.name))

        test(
            check.optname_short(self.short)
            or check.directive_short(self.short), err.InvalidShortOpt,
            (cnt, self.short))
        self.short = check.is_empty_or_none(self.short)
        if self.short == "-":
            self.short = None
            self.force_noshort = True

        self.name_alt = None
        self.short_alt = ""
        self.force_noshort_alt = False
        self.short_only_alt = False
        if len(splitnamealt) == 2:
            self.name_alt = splitnamealt[1]
            splitname = str(splitnamealt[1]).split(",")
            test(
                len(splitname) == 1 or len(splitname) == 2, err.WrongSplitName,
                (cnt, splitnamealt[1]))
            if len(splitname) > 1:
                self.name_alt = splitname[0]
                self.short_alt = splitname[1]
            elif len(self.name_alt) == 1:
                self.short_alt = self.name_alt
                self.short_only_alt = True
            test(check.alt_opt_name(self.name_alt), err.InvalidName,
                 (cnt, self.name_alt))
            test(self.name_alt != "help", err.ReservedOptName,
                 (cnt, self.name_alt))
            test(self.name_alt != "version", err.ReservedOptName,
                 (cnt, self.name_alt))
            #test(check.var_name_is_reserved(self.name_alt), err.ReservedVarName, (cnt, self.name_alt))

            test(
                check.optname_short(self.short_alt)
                or check.directive_short(self.short_alt), err.InvalidShortOpt,
                (cnt, self.short_alt))
            if self.short_alt == "-":
                self.short_alt = None
                self.force_noshort_alt = True

        self.short_alt = check.is_empty_or_none(self.short_alt)

        self.arg_type = self.arg_type.upper()

        if self.arg_type in ["INT+", "FLOAT+", "STRING+"]:
            self.arg_type = self.arg_type[:-1]
            self.is_array = True

        if self.arg_type == "INT":
            self.parse_type_int(cnt)
        elif self.arg_type == "FLOAT":
            self.parse_type_float(cnt)
        elif self.arg_type == "STRING":
            self.parse_type_string(cnt)
        elif self.arg_type == "NONE" or self.arg_type == "":
            self.parse_type_flag(cnt)
        else:
            raise err.InvalidArgType(cnt, self.arg_type)

        self.opt_name = self.name.replace("_", "-")
        self.opt_name_alt = None
        if self.name_alt != None:
            self.opt_name_alt = self.name_alt.replace("_", "-")
コード例 #3
0
    def __init__(self, infile):
        self.description = ""
        self.opt_list = []
        self.usage_line = []
        self.arg_list = []
        self.vararg = None
        self.version = []
        self.settings = settings.BopSettings()

        current_block = None
        optional_arg_given = False

        settings_block_given = False
        description_block_given = False
        version_block_given = False
        options_block_given = False
        arguments_block_given = False

        insert_space_in_description = False
        insert_space_in_version = False

        opt_reader = preparser.LinePreParser()

        cnt = err.ParseCount()

        for l in infile.readlines():
            cnt.inc()
            cnt.line = l
            line = opt_reader.parse_line(cnt, l)
            cnt.line = line
            if len(line) == 0:
                continue

            if current_block == None:
                if len(line) > 1:
                    raise err.InvalidBlockBeginLine(cnt, line)
                line[0] = line[0].upper()
                if line[0] == "SETTINGS_BEGIN":
                    test(not settings_block_given, err.DuplicateBlock,
                         (cnt, "SETTINGS"))
                    current_block = "SETTINGS_BLOCK"
                    settings_block_given = True
                elif line[0] == "DESCRIPTION_BEGIN":
                    test(not description_block_given, err.DuplicateBlock,
                         (cnt, "DESCRIPTION"))
                    current_block = "DESCRIPTION_BLOCK"
                    description_block_given = True
                elif line[0] == "VERSION_BEGIN":
                    test(not version_block_given, err.DuplicateBlock,
                         (cnt, "VERSION"))
                    current_block = "VERSION_BLOCK"
                    version_block_given = True
                elif line[0] == "OPTIONS_BEGIN":
                    test(not options_block_given, err.DuplicateBlock,
                         (cnt, "OPTIONS"))
                    current_block = "OPTIONS_BLOCK"
                    options_block_given = True
                elif line[0] == "ARGUMENTS_BEGIN":
                    test(not arguments_block_given, err.DuplicateBlock,
                         (cnt, "ARGUMENTS"))
                    current_block = "ARGUMENTS_BLOCK"
                    arguments_block_given = True
                else:
                    raise err.UnknownDescriptor(cnt, line[0])
            elif current_block == "SETTINGS_BLOCK":
                if line[0] == "SETTINGS_END":
                    current_block = None
                    continue
                if line[0] == "REQUIRED_VERSION":
                    test(
                        len(line) == 2, err.InvalidBopMinVersLine,
                        (cnt, len(line)))
                    settings.BopRequiredVersionChecker(cnt, *(line[1:]))
                elif line[0] == "WRAP_WIDTH":
                    test(
                        len(line) == 2, err.InvalidBopWrapWidthLine,
                        (cnt, len(line)))
                    try:
                        self.settings.wrap_width = int(line[1])
                    except:
                        raise err.InvalidBopWrapWidth(cnt, line[1])
                    test(self.settings.wrap_width >= 30,
                         err.InvalidBopWrapWidth,
                         (cnt, self.settings.wrap_width))
                elif line[0] == "AUTO_SHORT_OPTS":
                    test(
                        len(line) == 2, err.InvalidBopAutoShortOptsLine,
                        (cnt, len(line)))
                    try:
                        exec("self.settings.auto_short_opts = " +
                             line[1].capitalize())
                    except (NameError, SyntaxError):
                        raise err.InvalidBopAutoShortOpts(cnt, line[1])
                    test(isinstance(self.settings.auto_short_opts, bool),
                         err.InvalidBopAutoShortOpts, (cnt, line[1]))
                elif line[0] == "ONE_DASH_LONG_OPTS":
                    test(
                        len(line) == 2, err.InvalidBopOneDashLongOptsLine,
                        (cnt, len(line)))
                    try:
                        exec("self.settings.one_dash_long_opts = " +
                             line[1].capitalize())
                    except (NameError, SyntaxError):
                        raise err.InvalidBopOneDashLongOpts(cnt, line[1])
                    test(isinstance(self.settings.one_dash_long_opts, bool),
                         err.InvalidBopOneDashLongOpts, (cnt, line[1]))
                elif line[0] == "IN_FUNCTION":
                    test(
                        len(line) == 2, err.InvalidBopInFunctionLine,
                        (cnt, len(line)))
                    try:
                        exec("self.settings.in_function = " +
                             line[1].capitalize())
                    except (NameError, SyntaxError):
                        raise err.InvalidBopInFunction(cnt, line[1])
                    test(isinstance(self.settings.in_function, bool),
                         err.InvalidBopInFunction, (cnt, line[1]))
                elif line[0] == "ERR_CODE_OPT_INVALID":
                    test(
                        len(line) == 2, err.InvalidBopErrCodeLine,
                        (cnt, len(line)))
                    try:
                        self.settings.err_code_opt_invalid = int(line[1])
                    except:
                        raise err.InvalidBopErrCode(cnt, line[1])
                elif line[0] == "ERR_CODE_OPT_TYPE":
                    test(
                        len(line) == 2, err.InvalidBopErrCodeLine,
                        (cnt, len(line)))
                    try:
                        self.settings.err_code_opt_type = int(line[1])
                    except:
                        raise err.InvalidBopErrCode(cnt, line[1])
                elif line[0] == "ERR_CODE_OPT_RANGE":
                    test(
                        len(line) == 2, err.InvalidBopErrCodeLine,
                        (cnt, len(line)))
                    try:
                        self.settings.err_code_opt_range = int(line[1])
                    except:
                        raise err.InvalidBopErrCode(cnt, line[1])
                elif line[0] == "ERR_CODE_ARG_NUM":
                    test(
                        len(line) == 2, err.InvalidBopErrCodeLine,
                        (cnt, len(line)))
                    try:
                        self.settings.err_code_arg_num = int(line[1])
                    except:
                        raise err.InvalidBopErrCode(cnt, line[1])

                else:
                    raise err.UnknownSetting(cnt, line[0])
            elif current_block == "DESCRIPTION_BLOCK":
                if line[0] == "DESCRIPTION_END":
                    current_block = None
                    continue
                for i in range(len(line)):
                    if len(line[i]) == 0:
                        continue
                    if insert_space_in_description and not line[i][0].isspace(
                    ):
                        self.description += " "
                    self.description += line[i]
                    if line[i][-1].isspace():
                        insert_space_in_description = False
                    else:
                        insert_space_in_description = True
            elif current_block == "VERSION_BLOCK":
                if line[0] == "VERSION_END":
                    current_block = None
                    continue
                #test(len(line) == 1, err.InvalidVersLine, (cnt, len(line)))
                insert_space_in_version = False
                vline = ""
                for i in range(len(line)):
                    if len(line[i]) == 0:
                        continue
                    if insert_space_in_version and not line[i][0].isspace():
                        vline += " "
                    vline += line[i]
                    if line[i][-1].isspace():
                        insert_space_in_version = False
                    else:
                        insert_space_in_version = True
                self.version.append(vline)
            elif current_block == "OPTIONS_BLOCK":
                if line[0] == "OPTIONS_END":
                    current_block = None
                    continue
                test(
                    len(line) == opt.BopOption.required_args,
                    err.InvalidOptLine, (cnt, len(line)))
                myopt = opt.BopOption(cnt, self.settings, *line)

                test(
                    myopt.opt_name_alt == None
                    or myopt.opt_name != myopt.opt_name_alt, err.DuplicateOpt,
                    (cnt, myopt.opt_name))
                test(
                    myopt.short == None or myopt.short_alt == None
                    or myopt.short != myopt.short_alt, err.DuplicateShortOpt,
                    (cnt, myopt.short))

                for o in self.opt_list:
                    test(myopt.name != o.name, err.DuplicateOpt,
                         (cnt, myopt.name))
                    test(myopt.short == None or myopt.short != o.short,
                         err.DuplicateShortOpt, (cnt, myopt.short))

                    test(
                        myopt.opt_name_alt == None
                        or myopt.opt_name_alt != o.opt_name, err.DuplicateOpt,
                        (cnt, myopt.opt_name_alt))
                    test(
                        o.opt_name_alt == None
                        or myopt.opt_name != o.opt_name_alt, err.DuplicateOpt,
                        (cnt, myopt.opt_name))
                    test(
                        myopt.opt_name_alt == None or o.opt_name_alt == None
                        or myopt.opt_name_alt != o.opt_name_alt,
                        err.DuplicateOpt, (cnt, myopt.opt_name_alt))

                    test(
                        myopt.short_alt == None or o.short == None
                        or myopt.short_alt != o.short, err.DuplicateShortOpt,
                        (cnt, myopt.short_alt))
                    test(
                        myopt.short == None or o.short_alt == None
                        or myopt.short != o.short_alt, err.DuplicateShortOpt,
                        (cnt, myopt.short))
                    test(
                        myopt.short_alt == None or o.short_alt == None
                        or myopt.short_alt != o.short_alt,
                        err.DuplicateShortOpt, (cnt, myopt.short_alt))
                self.opt_list.append(myopt)
            elif current_block == "ARGUMENTS_BLOCK":
                if line[0] == "ARGUMENTS_END":
                    current_block = None
                    continue
                if line[0].upper() == "VARARGS" or line[0] == "@":
                    test(self.vararg == None, err.MultipleVararg, (cnt, ""))
                    test(
                        len(line) == arg.BopVararg.required_args,
                        err.InvalidVarargLine, (cnt, len(line)))
                    myvarg = arg.BopVararg(cnt, self.settings, *line)
                    if not myvarg.mandatory:
                        optional_arg_given = True
                    else:
                        test(not optional_arg_given, err.MandVarargAfterOptArg,
                             (cnt, ""))
                    self.vararg = myvarg
                else:
                    test(self.vararg == None, err.ArgAfterVararg, (cnt, ""))
                    test(
                        len(line) == arg.BopArgument.required_args,
                        err.InvalidArgLine, (cnt, len(line)))
                    myarg = arg.BopArgument(cnt, self.settings, *line)
                    if not myarg.mandatory:
                        optional_arg_given = True
                    else:
                        test(not optional_arg_given, err.MandArgAfterOptArg,
                             (cnt, ""))
                    for a in self.arg_list:
                        test(myarg.name != a.name, err.DuplicateArg,
                             (cnt, myarg.name))
                    self.arg_list.append(myarg)
            else:
                raise err.Bug(cnt, "")

        test(current_block == None, err.BlockNotClosed, (cnt, current_block))

        for o in self.opt_list:
            for a in self.arg_list:
                test(o.name != a.name, err.DuplicateOptArg, (cnt, o.name))

        if self.settings.auto_short_opts:
            for o in self.opt_list:
                if (o.short != None or o.force_noshort) and \
                 (o.name_alt == None or o.short_alt != None or o.force_noshort_alt):
                    continue
                if not (o.short != None or o.force_noshort):
                    for c in o.name:
                        if not check.optname_short(c):
                            continue
                        found = False
                        for o1 in self.opt_list:
                            if c == o1.short or c == o1.short_alt:
                                found = True
                                break
                        if not found:
                            o.short = c
                            break
                if not (o.name_alt == None or o.short_alt != None
                        or o.force_noshort_alt):
                    for c in o.name_alt:
                        if not check.optname_short(c):
                            continue
                        found = False
                        for o1 in self.opt_list:
                            if c == o1.short or c == o1.short_alt:
                                found = True
                                break
                        if not found:
                            o.short_alt = c
                            break

        for o in self.opt_list:
            self.usage_line.append(o.gen_usage_line())
            if o.name_alt != None:
                self.usage_line.append(o.gen_usage_line_alt())

        self.usage_line.append(
            ["--version", "output version information and exit"])
        self.usage_line.append(["--help", "print this help and exit"])

        if len(self.version) == 0:
            self.version.append("Version information unspecified")

        if not self.settings.in_function:
            self.exit_command = "exit"
        else:
            self.exit_command = "BASH_OPTPARSE_EARLY_RETURN=true; return"
コード例 #4
0
	def __init__(self, cnt, settings, name, arg_type, arg_name, arg_range, default_arg, desc):
		self.settings = settings
		self.name = name
		self.short = ""
		self.force_noshort = False
		self.short_only = False
		self.arg_type = str(arg_type)
		self.is_array = False
		self.arg_name = str(arg_name)
		self.arg_range = str(arg_range)
		self.default_arg = str(default_arg)
		self.desc = str(desc)

		self.arg_range_compiled_regex = None
		self.open_boundary = [False, False]

		splitnamealt = str(name).split("|")
		test(len(splitnamealt) == 1 or len(splitnamealt) == 2, err.WrongSplitAltName, (cnt, name))

		self.name = splitnamealt[0]
		splitname = str(splitnamealt[0]).split(",")
		test(len(splitname) == 1 or len(splitname) == 2, err.WrongSplitName, (cnt, splitnamealt[0]))
		if len(splitname) > 1:
			self.name = splitname[0]
			self.short = splitname[1]
		elif len(self.name) == 1:
			self.short = self.name
			self.short_only = True

		test(check.var_name(self.name), err.InvalidName, (cnt, self.name))
		test(self.name != "help", err.ReservedOptName, (cnt, self.name))
		test(self.name != "version", err.ReservedOptName, (cnt, self.name))
		test(check.var_name_is_reserved(self.name), err.ReservedVarName, (cnt, self.name))

		test(check.optname_short(self.short) or check.directive_short(self.short), err.InvalidShortOpt, (cnt, self.short))
		self.short = check.is_empty_or_none(self.short)
		if self.short == "-":
			self.short = None
			self.force_noshort = True

		self.name_alt = None
		self.short_alt = ""
		self.force_noshort_alt = False
		self.short_only_alt = False
		if len(splitnamealt) == 2:
			self.name_alt = splitnamealt[1]
			splitname = str(splitnamealt[1]).split(",")
			test(len(splitname) == 1 or len(splitname) == 2, err.WrongSplitName, (cnt, splitnamealt[1]))
			if len(splitname) > 1:
				self.name_alt = splitname[0]
				self.short_alt = splitname[1]
			elif len(self.name_alt) == 1:
				self.short_alt = self.name_alt
				self.short_only_alt = True
			test(check.alt_opt_name(self.name_alt), err.InvalidName, (cnt, self.name_alt))
			test(self.name_alt != "help", err.ReservedOptName, (cnt, self.name_alt))
			test(self.name_alt != "version", err.ReservedOptName, (cnt, self.name_alt))
			#test(check.var_name_is_reserved(self.name_alt), err.ReservedVarName, (cnt, self.name_alt))

			test(check.optname_short(self.short_alt) or check.directive_short(self.short_alt), err.InvalidShortOpt, (cnt, self.short_alt))
			if self.short_alt == "-":
				self.short_alt = None
				self.force_noshort_alt = True

		self.short_alt = check.is_empty_or_none(self.short_alt)

		self.arg_type = self.arg_type.upper()

		if self.arg_type in ["INT+", "FLOAT+", "STRING+"]:
			self.arg_type = self.arg_type[:-1]
			self.is_array = True

		if self.arg_type == "INT":
			self.parse_type_int(cnt)
		elif self.arg_type == "FLOAT":
			self.parse_type_float(cnt)
		elif self.arg_type == "STRING":
			self.parse_type_string(cnt)
		elif self.arg_type == "NONE" or self.arg_type == "":
			self.parse_type_flag(cnt)
		else:
			raise err.InvalidArgType(cnt, self.arg_type)

		self.opt_name = self.name.replace("_","-")
		self.opt_name_alt = None
		if self.name_alt != None:
			self.opt_name_alt = self.name_alt.replace("_","-")