示例#1
0
	bool Context::operator () (int argc, char* argv[])
	{
		while (pTokenIndex < argc)
		{
			arg = argv[pTokenIndex];

			switch (GetTokenType(arg))
			{
				// The current argument seems to be a short name of an option
				case ttShortOption:
					{
						while ('\0' != *(++arg))
						{
							OptionsOrderedByShortName::iterator i = pParser.pShortNames.find(*arg);
							if (i != pParser.pShortNames.end())
							{
								if (not findNextParameter(i->second, argc, argv))
									std::cerr << "Error: The parameter is missing for `" << arg << "`" << std::endl;
							}
							else
							{
								if (*arg == 'h' or *arg == '?')
								{
									pParser.helpUsage(argv[0]);
									return false;
								}
								optionIsUnknown(arg);
							}
						}
						break;
					}

				// The current argument seems to be a long name of an option
				case ttLongOption:
					{
						++arg;
						++arg;
						if ('\0' == *arg) // End of options
							return (0 == pParser.pErrors);

						if ((sub = strchr(arg, '=')))
						{
							const uint size = static_cast<uint>(sub - arg);

							if (size < YUNI_GETOPT_LONGNAME_MAX_LENGTH)
							{
								buffer.assign(arg, size);
								arg += size;
								++arg;

								OptionsOrderedByLongName::iterator i = pParser.pLongNames.find(buffer.c_str());
								if (i != pParser.pLongNames.end())
								{
									i->second->addValue(arg, static_cast<String::size_type>(::strlen(arg)));
								}
								else
								{
									if (0 == ::strcmp(buffer.c_str(), "help"))
									{
										pParser.helpUsage(argv[0]);
										return false;
									}
									optionIsUnknown(buffer.c_str());
								}
							}
							else
							{
								STD_CERR << "Error: name too long" << std::endl;
								++pParser.pErrors;
							}
						}
						else
						{
							OptionsOrderedByLongName::iterator i = pParser.pLongNames.find(arg);
							if (i != pParser.pLongNames.end())
							{
								if (not findNextParameter(i->second, argc, argv))
									parameterIsMissing(arg);
							}
							else
							{
								if (0 == ::strcmp(arg, "help"))
								{
									pParser.helpUsage(argv[0]);
									return false;
								}
								optionIsUnknown(arg);
							}
						}
						break;
					}

					// The current argument is a parameter actually (not attached to any option)
				case ttParameter:
					{
						if (pTokenIndex >= pParameterIndex)
						{
							pParameterIndex = pTokenIndex;
							if (pParser.pRemains)
								pParser.pRemains->addValue(arg, static_cast<String::size_type>(::strlen(arg)));
						}
						break;
					}
			}
			++pTokenIndex;
		}

		return (0 == pParser.pErrors);
	}
示例#2
0
	bool Context::operator () (int argc, char* argv[])
	{
		while (pTokenIndex < argc)
		{
			arg = argv[pTokenIndex];
			switch (GetTokenType(arg))
			{
				// The current argument seems to be a short name of an option
				case ttShortOption:
					{
						while ('\0' != *(++arg))
						{
							OptionsOrderedByShortName::iterator i = pParser.pShortNames.find(*arg);
							if (i != pParser.pShortNames.end())
							{
								if (!findNextParameter(i->second, argc, argv))
									std::cerr << "Error: The parameter is missing for `" << arg << "`" << std::endl;
							}
							else
							{
								if (*arg == 'h' || *arg == '?')
								{
									pParser.helpUsage(argv[0]);
									return false;
								}
								optionIsUnknown(arg);
							}
						}
						break;
					}

				// The current argument seems to be a long name of an option
				case ttLongOption:
					{
						++arg;
						++arg;
						if ('\0' == *arg) // End of options
							return (!pParser.pErrors);
						if ((sub = strchr(arg, '=')))
						{
							const size_t size = static_cast<size_t>(sub - arg);
							if (size < sizeof(buffer))
							{
								# ifdef YUNI_OS_MSVC
								strncpy_s(buffer, sizeof(buffer), arg, size);
								# else
								strncpy(buffer, arg, size);
								# endif
								buffer[size] = '\0';
								arg += size;
								++arg;

								OptionsOrderedByLongName::iterator i = pParser.pLongNames.find(buffer);
								if (i != pParser.pLongNames.end())
									i->second->addValue(arg, static_cast<String::size_type>(::strlen(arg)));
								else
								{
									if (!::strcmp(buffer, "help"))
									{
										pParser.helpUsage(argv[0]);
										return false;
									}
									optionIsUnknown(buffer);
								}
							}
							else
							{
								std::cerr << "Error: name too long" << std::endl;
								++pParser.pErrors;
							}
						}
						else
						{
							OptionsOrderedByLongName::iterator i = pParser.pLongNames.find(arg);
							if (i != pParser.pLongNames.end())
							{
								if (!findNextParameter(i->second, argc, argv))
									parameterIsMissing(arg);
							}
							else
							{
								if (!::strcmp(arg, "help"))
								{
									pParser.helpUsage(argv[0]);
									return false;
								}
								optionIsUnknown(arg);
							}
						}
						break;
					}

					// The current argument is a parameter actually (not attached to any option)
				case ttParameter:
					{
						if (pTokenIndex >= pParameterIndex)
						{
							pParameterIndex = pTokenIndex;
							if (pParser.pRemains)
								pParser.pRemains->addValue(arg, static_cast<String::size_type>(::strlen(arg)));
						}
						break;
					}
			}
			++pTokenIndex;
		}
		return (!pParser.pErrors);
	}