Poco C++ 学习笔记连载:Poco命令行解释

    在SDK中这样描述Option类:

    This class represents and stores the properties of a command line option.

    意思是:这个类表示并存储一个命令行选项的属性。

 

   An option has a full name, an optional short name, a description (used for printing a usage statement), and an optional argument name. An option can be optional or required. An option can be repeatable, which means that it can be given more than once on the command line.

  

   意思是:一个选项有一个完整的名字,一个可选的短名称,描述(用于打印出使用声明),和一个可选的参数名称。一个选项是可选的或必需的。选择可重复的,这意味着它可以不止一次在命令行。

 

    来看看Option类的构造函数:

Option();
/// Creates an empty Option.

Option(const Option& option);
/// Creates an option from another one.

Option(const std::string& fullName, const std::string& shortName);
/// Creates an option with the given properties.

Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required = false);
/// Creates an option with the given properties.

Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argRequired = false);
/// Creates an option with the given properties.

~Option();
/// Destroys the Option.

 

     成员变量:

private:
    std::string _shortName;//缩写名称
    std::string _fullName;//全称
    std::string _description;//描述
    bool        _required;//是否需要
    bool        _repeatable;//是否可重复
    std::string _argName;//参数名称
    bool        _argRequired;//是否需要参数
    std::string _group;
    std::string _binding;
    Validator*  _pValidator;
    AbstractOptionCallback* _pCallback;//回调函数
    AbstractConfiguration*  _pConfig;


      成员方法:

Option& required(bool flag);

Option& repeatable(bool flag);

Option& argument(const std::string& name, bool required = true);

Option& group(const std::string& group);

Option& binding(const std::string& propertyName);

ption& binding(const std::string& propertyName, AbstractConfiguration* pConfig);

Option& callback(const AbstractOptionCallback& cb);

Option& validator(Validator* pValidator);

......


 

OptionSet类:

A collection of Option objects.

是上面Option类的集合

 

添加Option类方法:

void addOption(
    const Option & option
);


 

下面列举一个使用例子:

void defineOptions(OptionSet& options) {
		Application::defineOptions(options);
		options.addOption(
				Option("help", "h", "Display help information").required(false).repeatable(
						false).callback(
						OptionCallback < DataLoader
								> (this, &DataLoader::handleHelp)));
		options.addOption(
				Option("latn", "l", "The user or company, not case sensitive.").required(
						true).repeatable(false).argument("latn").binding(
						"application.latn").callback(
						Poco::Util::OptionCallback < DataLoader
								> (this, &DataLoader::handleOwner)));
		Poco::Util::Validator* month = new Poco::Util::RegExpValidator(
				"[0-9]{6,8}");
		options.addOption(
				Option("month", "m", "date(yyyymm/yyyymmdd)").required(true).repeatable(
						false).argument("month").validator(month).binding(
						"application.month").callback(
						Poco::Util::OptionCallback < DataLoader
								> (this, &DataLoader::handleMonth)));


回调函数:


void handleOwner(const std::string&name, const std::string& value) {
		config().setString("application.company", value);
		_owner = value;
	}
	void handleMonth(const std::string&name, const std::string& value) {
		_month = value;
	}
	void handleDataFile(const std::string&name, const std::string& value) {
		_dataname = value;
	}


fullNamefullNamefullName

void handleOwner(const std::string&name, const std::string& value) {
		config().setString("application.company", value);
		_owner = value;
	}
	void handleMonth(const std::string&name, const std::string& value) {
		_month = value;
	}
	void handleDataFile(const std::string&name, const std::string& value) {
		_dataname = value;
	}


 

下面看看Application类:

 

void Application::processOptions()
{
	defineOptions(_options);
	OptionProcessor processor(_options);
	processor.setUnixStyle(_unixOptions);
	_argv = _unprocessedArgs;
	_unprocessedArgs.erase(_unprocessedArgs.begin());
	ArgVec::iterator it = _unprocessedArgs.begin();
	while (it != _unprocessedArgs.end() && !_stopOptionsProcessing)
	{
		std::string name;
		std::string value;
		if (processor.process(*it, name, value))
		{
			if (!name.empty()) // "--" option to end options processing or deferred argument
			{
				handleOption(name, value);
			}
			it = _unprocessedArgs.erase(it);
		}
		else ++it;
	}
	if (!_stopOptionsProcessing)
		processor.checkRequired();
}


 

 

void Application::defineOptions(OptionSet& options)
{
	for (SubsystemVec::iterator it = _subsystems.begin(); it != _subsystems.end(); ++it)
	{
		(*it)->defineOptions(options);
	}
}

 


OptionProcessorOptionProcessorOptionProcessorOptionProcessorOptionProcessor类

 

bool OptionProcessor::process(const std::string& argument, std::string& optionName, std::string& optionArg)
{
	optionName.clear();
	optionArg.clear();
	if (!_ignore)
	{
		if (!_deferredOption.empty())
			return processCommon(argument, false, optionName, optionArg);
		else if (_unixStyle)
			return processUnix(argument, optionName, optionArg);
		else
			return processDefault(argument, optionName, optionArg);
	}
	return false;
}


 

bool OptionProcessor::processCommon(const std::string& optionStr, bool isShort, std::string& optionName, std::string& optionArg)
{
	if (!_deferredOption.empty())
	{
		const Option& option = _options.getOption(_deferredOption, false);
		std::string optionWithArg(_deferredOption);
		_deferredOption.clear();
		optionWithArg += '=';
		optionWithArg += optionStr;
		option.process(optionWithArg, optionArg);
		optionName = option.fullName();
		return true;
	}
	if (optionStr.empty()) throw EmptyOptionException();
	const Option& option = _options.getOption(optionStr, isShort);
	const std::string& group = option.group();
	if (!group.empty())
	{
		if (_groups.find(group) != _groups.end())
			throw IncompatibleOptionsException(option.fullName());
		else
			_groups.insert(group);
	}
	if (_specifiedOptions.find(option.fullName()) != _specifiedOptions.end() && !option.repeatable())
		throw DuplicateOptionException(option.fullName());
	_specifiedOptions.insert(option.fullName());
	if (option.argumentRequired() && ((!isShort && optionStr.find_first_of(":=") == std::string::npos) || (isShort && optionStr.length() == option.shortName().length())))
	{
		_deferredOption = option.fullName();
		return true;
	}
	option.process(optionStr, optionArg);
	optionName = option.fullName();
	return true;
}


关于Poco命令行的处理,应该清晰了。

   

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值