Poco C++ 学习笔记连载:Poco命令行解释
来源:互联网 发布:日本tv直播软件 编辑:程序博客网 时间:2024/05/01 08:16
在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);elsereturn 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命令行的处理,应该清晰了。
- Poco C++ 学习笔记连载:Poco命令行解释
- poco库学习笔记(5) Poco::Thread
- poco库学习笔记(6) Poco::Timestamp
- poco库学习笔记(7) Poco::Any
- poco库学习笔记(8) Poco::HashMap
- poco库学习笔记(9) Poco::AutoPtr
- poco库学习笔记(10) Poco::NotificationCenter
- Poco学习笔记
- POCO
- poco库学习笔记(1) poco库的安装
- poco库学习笔记(1) Poco::Net::TCPServerConnection
- poco库学习笔记(2) Poco::Net::TCPServerConnectionFactory
- poco库学习笔记(4) Poco::Util::Option
- poco库学习笔记(3) 加载配置文件
- C++跨平台开源库POCO学习笔记
- Poco c ++ 库安装
- c++Poco库
- Poco Application 框架学习(2)增加命令行参数
- unity3d游戏开发之讲解如何使用NGUI制作表单
- 应该注意的搜索引擎营销的误区
- io复用select pselect
- c 字符操作函数
- Django介绍
- Poco C++ 学习笔记连载:Poco命令行解释
- 加载图片三种方式
- 关键词的密度是多少比较好?
- 颜色迁移(色彩空间的相互转化)
- day6:通过表单传递数据的两种方法
- 2015年物联网专业师资培训班邀请函
- nexus在ubuntu下配置开机自动启动
- java网络编程之缓存(二)
- windows 安装mysql