16 inline Usage::Usage(std::string
const& _name){ name = _name; }
19 for(it = usage.options.begin(); it != usage.options.end(); it++){
20 unsigned char const& chr = it->first;
21 Option
const& opt = it->second;
22 if(options.find(chr) == options.end()){
28 for(
size_t i = 0; i < usage.usage_begin.size(); i++){
29 usage_begin.push_back(usage.usage_begin[i]);
31 for(
size_t i = 0; i < usage.usage_end.size(); i++){
32 usage_end.push_back(usage.usage_end[i]);
38 for(it = usage.options.begin(); it != usage.options.end(); it++){
39 unsigned char const& chr = it->first;
40 if(options.find(chr) == options.end()){
43 options[chr] = it->second;
47 inline bool Usage::addOption(
unsigned char opt, std::string
const& des){
48 if(options.find(opt) != options.end()){
51 options[opt] = Option(des);
54 inline bool Usage::addOption(
unsigned char opt, std::string
const& des,
55 std::string
const& val_type,
56 std::string
const& val_default,
58 if(options.find(opt) != options.end()){
61 options[opt] = Option(des, val_type, val_default, must);
64 inline bool Usage::addOptionValueAccept(
unsigned char opt,
65 std::string
const& val,
66 std::string
const& des){
67 if(options.find(opt) == options.end()){
70 return options[opt].addValueAccept(val, des);
73 return (options.find(opt) != options.end() &&
74 options.find(opt)->second.hasSetup());
76 inline size_t Usage::getOptionValuesCount(
unsigned char opt)
const {
77 if(options.find(opt) == options.end()){
80 return options.find(opt)->second.getValuesCount();
82 inline std::string Usage::getOptionValue(
unsigned char opt,
84 if(options.find(opt) == options.end()){
87 return options.find(opt)->second.getValue(index);
89 inline size_t Usage::getProcArgsCount()
const{
return proc_arguments.size();}
90 inline std::string Usage::getProcArg(
size_t index)
const {
91 if(index >= proc_arguments.size()){
94 return proc_arguments[index];
96 inline std::vector<std::string> Usage::getProcArgs()
const {
97 return proc_arguments;
99 inline void Usage::addUsageBegin(std::string
const& des){
102 inline void Usage::addUsageEnd(std::string
const& des){
105 inline std::string Usage::getUsage()
const {
106 std::string out =
stringPrintf(
"USAGE\n %s", name.c_str());
108 for(it = options.begin(); it != options.end(); it++){
109 out +=
" " + it->second.getUsage(it->first,
false);
111 out +=
"\n\nDESCRIPTION\n";
112 for(
size_t i = 0; i < usage_begin.size(); i++){
113 out +=
" " + usage_begin[i] +
"\n\n";
115 for(it = options.begin(); it != options.end(); it++){
116 out += it->second.getUsage(it->first,
true);
118 for(
size_t i = 0; i < usage_end.size(); i++){
119 out +=
" " + usage_end[i] +
"\n\n";
123 inline bool Usage::setArguments(
int argc,
char** argv, std::string *errmsg){
128 std::string& err = (errmsg == NULL ? zzz : *errmsg);
129 for(it = options.begin(); it != options.end(); it++){
130 s += (char)(it->first);
131 if(it->second.hasValue()){
135 for(
int opt; (opt = getopt(argc, argv, s.c_str())) != -1; ){
136 if(options.find(opt) == options.end()){
137 if(options.find(optopt) == options.end()){
146 if(options[opt].setValue(optarg == NULL ?
"" : optarg) ==
false){
148 "Option argument '%s' to '-%c' is not allowed\n"
153 for(it = options.begin(); it != options.end(); it++){
154 if(it->second.chkSetup() ==
false){
160 for(
int i = optind; i < argc; i++){
161 proc_arguments.push_back(std::string(argv[i]));
166 inline Usage::Value::Value(){ }
167 inline Usage::Value::Value(std::string
const& v){
171 inline Usage::Value::Value(std::string
const& v, std::string
const& d){
175 inline std::string Usage::Value::getUsage()
const {
176 if(description.length() > 0)
178 " ", value.c_str(), description.c_str());
182 inline std::string Usage::Value::getValue()
const {
return value; }
183 inline bool Usage::Value::operator==(Value
const& b)
const {
184 return (value == b.value);
187 inline Usage::Option::Option(){ }
188 inline Usage::Option::Option(std::string
const& des){
194 inline Usage::Option::Option(std::string
const& des,
195 std::string
const& typ,
196 std::string
const& def,
205 inline bool Usage::Option::setValue(std::string
const& str){
207 if(values_accept.size() > 0 &&
208 std::find(values_accept.begin(), values_accept.end(),
209 Value(str,
"")) == values_accept.end()){
212 values.push_back(str);
217 inline size_t Usage::Option::getValuesCount()
const{
return values.size();}
218 inline std::string Usage::Option::getValue(
size_t index)
const{
222 if(!has_setup || index >= values.size()){
223 return value_default;
225 return values[index];
227 inline bool Usage::Option::addValueAccept(std::string
const& val,
228 std::string
const& des){
232 if(std::find(values_accept.begin(), values_accept.end(), Value(val))
233 == values_accept.end()){
234 values_accept.push_back(Value(val, des));
238 inline bool Usage::Option::hasSetup()
const {
return has_setup; }
239 inline bool Usage::Option::hasValue()
const {
return has_value; }
240 inline bool Usage::Option::chkSetup()
const {
241 return !(must_setup && !has_setup);
243 inline std::string Usage::Option::getUsage(
unsigned char opt,
259 if(value_default !=
""){
260 tmp2=
stringPrintf(
"defuault='%s'",value_default.c_str());
262 std::string tmp3 = must_setup ?
"" :
"optional";
263 if(tmp2.length() + tmp3.length() > 0){
264 if(tmp2.length() > 0 && tmp3.length() > 0){
265 tmp =
"(" + tmp3 +
", " + tmp2 +
")";
267 tmp =
"(" + tmp3 + tmp2 +
")";
270 tmp = value_type +
" " + tmp;
275 for(
size_t i = 0; i < values_accept.size(); i++){
277 vs += (i + 1 < values_accept.size() ?
", " :
" or ");
279 vs +=
"'" + values_accept[i].getValue() +
"'";
281 if(vs.length() == 0){
282 vs =
"... (anything)";
285 ret +=
" " + tmp +
"\n";
286 for(
size_t i = 0; i < values_accept.size(); i++){
287 ret += values_accept[i].getUsage();
bool import(Usage const &usage)
將另一個usage的設置匯入
bool hasOptionSetup(String opt) const
回傳是否有設定此選項
std::string stringReplace(std::string str, std::string const &from, std::string const &to)
將輸入字串中的某個pattern取代成另一個pattern
bool update(Usage const &usage)
將另一個usage的選項設置加進來
std::string stringPrintf(char const *fmt,...)
類似C的printf, 不過是將格式化的字串丟到 std::string 裡回傳