virtual void print_help(std::ostream* s, const int depth, const bool recurse = false) { if (!s) return; std::string indent(indent_width * depth, ' '); std::string subindent(indent_width, ' '); *s << indent << _name << "=<" << _value_type << ">" << std::endl; *s << indent << subindent << _description << std::endl; *s << indent << subindent << "Valid values:" << print_valid() << std::endl; *s << indent << subindent << "Defaults to " << _default << std::endl; *s << std::endl; }
bool parse_args(std::vector<std::string>& args, std::ostream* out, std::ostream* err, bool& help_flag) { if (args.size() == 0) return true; std::string name; std::string value; split_arg(args.back(), name, value); if (_name == "help") { print_help(out, 0, false); help_flag |= true; args.clear(); return false; } else if (_name == "help-all") { print_help(out, 0, true); help_flag |= true; args.clear(); return false; } else if (_name == name) { args.pop_back(); bool good_arg = false; bool valid_arg = true; for (size_t i = 0; i < _values.size(); ++i) { if ( _values.at(i)->name() != value) continue; _cursor = i; valid_arg &= _values.at(_cursor)->parse_args(args, out, err, help_flag); good_arg = true; break; } if (!good_arg) { if (err) { *err << value << " is not a valid value for \"" << _name << "\"" << std::endl; *err << std::string(indent_width, ' ') << "Valid values:" << print_valid() << std::endl; } args.clear(); } return valid_arg && good_arg; } return true; }
bool parse_args(std::vector<std::string>& args, interface_callbacks::writer::base_writer& info, interface_callbacks::writer::base_writer& err, bool& help_flag) { if (args.size() == 0) return true; if ( (args.back() == "help") || (args.back() == "help-all") ) { print_help(info, 0); help_flag |= true; args.clear(); return true; } std::string name; std::string value; split_arg(args.back(), name, value); if (_name == name) { args.pop_back(); T proposed_value = boost::lexical_cast<T>(value); if (!set_value(proposed_value)) { std::stringstream message; message << proposed_value << " is not a valid value for " << "\"" << _name << "\""; err(message.str()); err(std::string(indent_width, ' ') + "Valid values:" + print_valid()); args.clear(); return false; } } return true; }
bool parse_args(std::vector<std::string>& args, std::ostream* out, std::ostream* err, bool& help_flag) { if (args.size() == 0) return true; if ( (args.back() == "help") || (args.back() == "help-all") ) { print_help(out, 0); help_flag |= true; args.clear(); return true; } std::string name; std::string value; split_arg(args.back(), name, value); if (_name == name) { args.pop_back(); T proposed_value = boost::lexical_cast<T>(value); if (!set_value(proposed_value)) { if (err) { *err << proposed_value << " is not a valid value for " << "\"" << _name << "\"" << std::endl; *err << std::string(indent_width, ' ') << "Valid values:" << print_valid() << std::endl; } args.clear(); return false; } } return true; }
int main(int argc, char *argv[]) { Tree<int, int> tree; unsigned int num, key, key2, root; bool error; srand(time(NULL)); build_tree(&tree, 0, 20); key = rand_int(0, 20); key2 = rand_int(0, 20); num = tree.SearchByKey(key2, &error); print_valid(&tree); cout << endl << "[Remove by key " << key << "]" << endl; if (tree.RemoveByKey(key)) { cout << "ERROR removing by key " << key << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Remove ( key " << key2 << ") by data " << num << "]" << endl; if (tree.RemoveByData(num)) { cout << "ERROR removing by data " << num << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// root = tree.Root(); cout << endl << "[Remove root " << root << " by key " << root << "]" << endl; if (tree.RemoveByKey(root)) { cout << "ERROR removing by key " << root << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Insert]" << endl; build_tree(&tree, 20, 23); print_valid(&tree); //////////////////////////////////////////////////////////////// root = tree.Root(); cout << endl << "[Remove root " << root << " by key " << root << "]" << endl; if (tree.RemoveByKey(root)) { cout << "ERROR removing by key " << root << endl; } print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Clear tree]" << endl; tree.Clear(); print_valid(&tree); //////////////////////////////////////////////////////////////// cout << endl << "[Build new tree]" << endl; build_tree(&tree, 0, 5); print_valid(&tree); return 0; }