#ifndef USAGE_H_ #define USAGE_H_ #include #include #include namespace meow{ class Usage{ private: typedef std::string String; typedef std::vector Strings; class Value{ public: Value(); Value(String const& v); Value(String const& v, String const& d); String getUsage() const; String getValue() const; bool operator==(Value const& b) const; private: String value; String description; }; typedef std::vector Values; class Option{ public: Option(); Option(String const& des); Option(String const& des, String const& typ, String const& def, bool must); bool setValue(String const& str); String getValue(size_t index) const; size_t getValuesCount() const; bool addValueAccept(String const& val, String const& des); bool hasSetup() const; bool hasValue() const; bool chkSetup() const; String getUsage(unsigned char opt, bool detail) const; private: Strings values; Values values_accept; String value_default; String value_type; String description; bool has_value; bool has_setup; bool must_setup; }; typedef std::map Options; typedef Options::const_iterator OptionsIterator; public: Usage(); Usage(String const& _name); ////////// **# Add other options #** /////////// bool import(Usage const& usage); bool update(Usage const& usage); /////////// **# add a option/value #** ///////// bool addOption(unsigned char opt, String const& des); bool addOption(unsigned char opt, String const& des, String const& val_type, String const& val_default, bool must); bool addOptionValueAccept(unsigned char opt, String const& val, String const& des); ///////// **# access informations #** ////////// bool hasOptionSetup(unsigned char opt ) const; size_t getOptionValuesCount(unsigned char opt ) const; String getOptionValue(unsigned char opt, size_t index) const; size_t getProcArgsCount() const; String getProcArg(size_t index) const; Strings getProcArgs() const; //////// **# add a usage description #** /////// void addUsageBegin(String const& des); void addUsageEnd (String const& des); ///////////// **# access usages #** //////////// String getUsage() const; ////////// **# analysis argc,argv #** ////////// bool setArguments(int argc, char** argv, String* errmsg); private: String name; Options options; Strings usage_begin; Strings usage_end ; Strings proc_arguments; }; //# //# === meow:: *Usage* (C++ Class) //# ==== Description //# `Usage` 是用來分析argc, argv和輸出usage document的class. //# argc, argv的部份, 有以下規則 //# //# * `-c` 其中 `c` 可以代換成正常的一個字元的字符, //# 這種選像要嘛就是 *有設置* , 不然就是 *沒設置* //# * `-c ` 附加一個value, 這種選項可以是選擇性 ,即要設定與否都可以, //# 反之則一定要設定. 另外可以給定value的預設值以及哪些value是可接受的 //# * `` 其他, 一律視為process arguments //# //# ==== Methods //# * `Usage(String const& _name)` + //# 建構子, 所有說明文字中 ** 都會被代換成 `_name` //# * `Usage()` + //# 建構子, `_name` 自動取為 " *nobody* " //# * `import(Usage const& usage)` + //# 將另一個usage的設定匯入, 回傳成功與否 *(bool)* //# * `update(Usage const& usage)` + //# 將另一個usage分析argc,argv出來的資料拿來用, 回傳成功與否 *(bool)* //# * `addOption(unsigned char option, String const& description)` + //# 新增一個不接額外選項的參數, 並附上說明文字, 回傳成功與否 *(bool)* //# * `addOption(unsigned char option, String const& description, //# String const& value_type, String const& value_default, bool must)` + //# 新增一個有額外選項的參數, 並附上說明文字, 額外選項的型別跟預設值. //# 說明文字中所有的" ** "將會被取代指定的型別, 其中 `must` 代表 //# " *是否一定要設定此參數* " , 回傳表成功與否 *(bool)* //# * `addOptionValueAccept(unsigned char option, //# String const& value, String const& description)` + //# 針對某個option, 新增一個可接受的額外選項 (如果某個option從頭到尾都 //# 沒有新增可接受的選項, 則視為不限制), 回傳成功與否 *(bool)* //# * `hasOptionSetup(unsigned char option)` + //# 回傳是否有此選項 *(bool)* //# * `getOptionValuesCount(unsigned char option)` + //# 回傳此參數被設置了幾次 *(size_t)* , 只對有接額外參數的有效 //# * `getOptionValue(unsigned char option, size_t index)` + //# 回傳第`index`個額外選項 *(String)* //# * `getProcArgsCount()` + //# 回傳有多少個Process Arguments *(size_t)* //# * `getProcArg(size_t index)` + //# 取得第`index`個Process Argument *(String)* //# * `getProcArgs()` + //# 回傳一個陣列, 包含所有Process Arguments *(Strings)* //# * `addUsageBegin(String const& des)` + //# 新增一段usage document於每個選項逐條說明之前 //# * `addUsageEnd (String const& des)` + //# 新增一段usage document於每個選項逐條說明之後 //# * `String getUsage()` + //# 回傳usage document *(String)* //# * `setArguments(int argc, char** argv, String* errmsg)` + //# 輸入argv, argc, 回傳是否沒有錯誤發生 *(bool)* , 其中如果有錯誤發生, //# 且 `errmsg != NULL` 則會將錯誤訊息寫入之 //# //#[NOTE] //#================================== //# * `String` 是 `std::string` . //# * `Strings` 是 `std::vector< std::string> >`. //# * 如果沒有寫回傳什麼, 就是回傳 `void` //#================================== //# //#''' //# } #include "Usage.hpp" #endif // USAGE_H_