Пример #1
0
const char *RequiredValueOpt(const char *Name)
	{
	const char *s = ValueOpt(Name);
	if (0 == s)
		CommandLineError("Required option -%s not specified\n", Name);
	return s;
	}
Пример #2
0
void ProcessArgVect(int argc, char *argv[])
	{
	for (int iArgIndex = 0; iArgIndex < argc; )
		{
		const char *Arg = argv[iArgIndex];
		if (Arg[0] != '-')
			Quit("Command-line option \"%s\" must start with '-'\n", Arg);
		const char *ArgName = Arg + 1;
		if (TestSetFlagOpt(ArgName))
			{
			++iArgIndex;
			continue;
			}
		
		char *Value = 0;
		if (iArgIndex < argc - 1)
			Value = argv[iArgIndex+1];
		if (TestSetValueOpt(ArgName, Value))
			{
			iArgIndex += 2;
			continue;
			}
		CommandLineError("Invalid command line option \"%s\"\n", ArgName);
		}
	}
Пример #3
0
static bool TestSetValueOpt(const char *Arg, const char *Value)
	{
	for (int i = 0; i < ValueOptCount; ++i)
		if (!stricmp(Arg, ValueOpts[i].m_pstrName))
			{
			if (0 == Value)
				CommandLineError("Option -%s must have value\n", Arg);
			ValueOpts[i].m_pstrValue = strsave(Value);
			return true;
			}
	return false;
	}
Пример #4
0
std::unordered_map<std::string, std::string> ArgumentProcessor::parse(int32_t argc, char *argv[])
{
    std::vector<std::string> args = toStringVector(argc, argv);

    std::unordered_map<std::string, std::string> argumentMap;

    for(size_t idx = 0; idx < argc; ++idx) {
        std::string currentArgument = args[idx];
        if(notIn(currentArgument, mArguments) &&
           notIn(currentArgument, mOptionalArguments) &&
           notIn(currentArgument, mSwitches)) {
            continue;
        } else {
            size_t nextIdx = idx + 1;
            if(nextIdx >= argc) {
                if(notIn(currentArgument, mSwitches)) {
                    continue;
                } else {
                    argumentMap[parseFlag(currentArgument)] = "true";
                }
            } else {
                std::string nextArgument = args[nextIdx];
                if(!notIn(currentArgument, mArguments) && notIn(nextArgument, {mOptionalArguments, mSwitches})) {
                    if(!mAllowedValues[currentArgument].empty()) {
                        if(!notIn(nextArgument, mAllowedValues[currentArgument])) {
                            argumentMap[parseFlag(currentArgument)] = nextArgument;
                        } else {
                            std::stringstream s;
                            s << "Encountered invalid option: " << currentArgument << " " << nextArgument << std::endl;
                            throw CommandLineError(s.str(), currentMethod, currentLine);
                        }
                    } else {
                        argumentMap[parseFlag(currentArgument)] = nextArgument;
                    }
                } else if(!notIn(currentArgument, mOptionalArguments) && notIn(nextArgument, {mArguments, mSwitches})) {
                    if(!mAllowedValues[currentArgument].empty()) {
                        if(!notIn(nextArgument, mAllowedValues[currentArgument])) {
                            argumentMap[parseFlag(currentArgument)] = nextArgument;
                        } else {
                            std::stringstream s;
                            s << "Encountered invalid option: " << currentArgument << " " << nextArgument << std::endl;
                            throw CommandLineError(s.str(), currentMethod, currentLine);
                        }
                    } else {
                        argumentMap[parseFlag(currentArgument)] = nextArgument;
                    }
                } else if(!notIn(currentArgument, mSwitches) && !notIn(nextArgument, {mArguments, mOptionalArguments})) {
                    argumentMap[parseFlag(currentArgument)] = "true";
                }
            }
        }
    }

    if(argumentMap[parseFlag("--help")].empty()) {
        for(std::string arg : mArguments) {
            if(argumentMap[parseFlag(arg)].empty()) {
                std::stringstream s;
                s << "Missing required argument: " << arg << std::endl;
                throw CommandLineError(s.str(), currentMethod, currentLine);
            }
        }
    } else {
        std::stringstream s;
        s << "Let me help you!" << std::endl;
        throw CommandLineError(s.str());
    }

    return argumentMap;
}