Exemplo n.º 1
0
int OOBase::CmdArgs::parse_short_options(options_t& options, const char** argv, int& arg, int argc) const
{
	String strKey,strVal;
	for (const char* c = argv[arg]+1; *c!='\0'; ++c)
	{
		Table<String,Option>::const_iterator i = m_map_opts.begin();
		for (;i; ++i)
		{
			if (i->second.m_short_opt == *c)
			{
				if (i->second.m_has_value)
				{
					const char* value;
					if (c[1] == '\0')
					{
						// Next arg is the value
						if (arg >= argc-1)
						{
							int err = strVal.printf("-%c",*c);
							if (err)
								return err;

							return error(options,EINVAL,"missing",strVal.c_str());
						}
						
						value = argv[++arg];
					}
					else
						value = &c[1];

					if (!strVal.assign(value) || !options.insert(i->first,strVal))
						return system_error();

					// No more for this arg...
					return 0;
				}
				else
				{
					if (!strVal.assign("true") || !options.insert(i->first,strVal))
						return system_error();

					break;
				}
			}
		}

		if (!i)
		{
			int err = strVal.printf("-%c",*c);
			if (err)
				return err;
				
			return error(options,ENOENT,"unknown",strVal.c_str());
		}
	}

	return 0;
}
Exemplo n.º 2
0
int OOBase::CmdArgs::error(options_t& options, int err, const char* key, const char* value) const
{
	String strErr,strVal;
	if (!strErr.assign(key) || !strVal.assign(value))
		return system_error();
	
	options.clear();
	if (!options.insert(strErr,strVal))
		return system_error();

	return err;
}
Exemplo n.º 3
0
int OOBase::CmdArgs::parse_long_option(options_t& options, const char** argv, int& arg, int argc) const
{
	String strKey,strVal;
	for (Table<String,Option>::const_iterator i = m_map_opts.begin();i; ++i)
	{
		const char* value = "true";
		if (i->second.m_long_opt == argv[arg]+2)
		{
			if (i->second.m_has_value)
			{
				if (arg >= argc-1)
					return error(options,EINVAL,"missing",argv[arg]);
					
				value = argv[++arg];
			}

			if (!strVal.assign(value) || !options.insert(i->first,strVal))
				return system_error();

			return 0;
		}

		if (strncmp(i->second.m_long_opt.c_str(),argv[arg]+2,i->second.m_long_opt.length())==0 && argv[arg][i->second.m_long_opt.length()+2]=='=')
		{
			if (i->second.m_has_value)
				value = &argv[arg][i->second.m_long_opt.length()+3];

			if (!strVal.assign(value) || !options.insert(i->first,strVal))
				return system_error();

			return 0;
		}
	}

	return error(options,ENOENT,"unknown",argv[arg]);
}