115 const std::string &
name ()
const {
132 cout <<
" " << name_ <<
" " << metavar_ << std::endl;
134 if (description_ !=
"")
135 cout <<
" " << description_ << std::endl;
138 cout <<
" multiple values allowed" << std::endl;
141 cout <<
" default: " <<
default_ << std::endl;
144 cout <<
" required" << std::endl;
223 virtual void parse (std::istream & cin) = 0;
231 virtual void set (
const Json::Value & json) = 0;
241 const std::string name_;
242 std::string description_;
243 std::string metavar_;
247 template <
typename T>
248 void setValue (
const Json::Value & json, T & destination) {
249 std::istringstream iss (json.asString());
261 template <
typename T>
274 destination_ (destination)
276 std::ostringstream defaultStr;
277 defaultStr << std::boolalpha << destination;
281 virtual void parse (std::istream & cin) {
282 cin >> std::boolalpha >> destination_;
285 virtual void set (
const Json::Value & json) {
286 setValue (json, destination_);
301 template <
typename T>
315 destination_ (destination)
317 Json::Value defaults;
319 auto it = destination.begin();
320 auto end = destination.end();
321 for ( ; it != end ; ++it) {
322 defaults.append (*it);
325 default_ = Json::FastWriter().write (defaults);
332 virtual void parse (std::istream & cin) {
334 cin >> std::boolalpha >> val;
335 destination_.push_back (val);
338 virtual void set (
const Json::Value & json) {
339 destination_.resize (json.size());
340 for (
int i = 0 ; i<json.size() ; ++i) {
341 setValue (json[i], destination_[i]);
346 Vector & destination_;
369 description_ (description),
374 auto it = keys_.begin();
375 auto end = keys_.end();
376 for ( ; it != end ; ++it) {
392 void parse (std::istream & cin, std::ostream & cout,
bool echo =
false) {
397 std::getline (cin, line);
400 cout << line << std::endl;
407 std::istringstream input (line);
411 KeyMap::const_iterator it = keys_.find(key);
412 if (it != keys_.end()) {
413 it->second->parse (input);
415 cout <<
"Unknown key: `" << key <<
"'" << std::endl;
418 }
while (! cin.eof());
433 void parseJson (
const Json::Value & request, std::ostream & cout) {
436 auto it = keys_.begin();
437 auto end = keys_.end();
438 for ( ; it != end ; ++it){
439 Json::Value arg = request[it->first];
440 if (! arg.isNull()) {
441 it->second->set (arg);
464 description_ = description;
480 virtual void run (std::ostream & cout) = 0;
509 void help (std::ostream & cout)
const {
510 cout << description_ << std::endl
512 <<
"Arguments:" << std::endl;
513 auto it = keys_.begin();
514 auto end = keys_.end();
515 for ( ; it != end ; ++it) {
516 it->second->display(cout);
528 void display (std::ostream & cout) {
529 cout << std::setw(20) << std::left << name_
530 <<
" " << description_ << std::endl;
533 typedef std::map<std::string, KeyParserBase*> KeyMap;
535 std::string description_;
536 const std::string name_;
556 : description_ (description),
561 auto it = commands_.begin();
562 auto end = commands_.end();
563 for ( ; it != end ; ++it) {
604 commands_[command->
name()] = command;
615 void help (std::ostream & cout) {
616 cout << description_ << std::endl
617 <<
"Commands:" << std::endl
618 <<
" " << std::setw(20) << std::left <<
"help"
619 <<
" Display this help" << std::endl
620 <<
" " << std::setw(20) << std::left <<
"help COMMAND"
621 <<
" Display help about COMMAND" << std::endl;
623 auto it = commands_.begin();
624 auto end = commands_.end();
625 for ( ; it != end ; ++it) {
627 it->second->display(cout);
637 void parse (std::istream & cin, std::ostream & cout) {
639 cout << prompt_ << std::flush;
642 std::getline (cin, line);
645 cout << line << std::endl;
652 std::istringstream input (line);
656 bool helpRequested =
false;
657 if (command ==
"help") {
659 if (command ==
"help") {
663 helpRequested =
true;
667 CommandMap::const_iterator it = commands_.find (command);
668 if (it != commands_.end()) {
670 it->second->help (cout);
672 it->second->parse (cin, cout, echo_);
674 cout <<
"Unknown command: `" << command <<
"'" << std::endl;
677 }
while (! cin.eof());
688 void parseJson (std::istream & cin, std::ostream & cout,
bool verbose=
false) {
690 std::cerr <<
"Receiving client request:" << std::endl;
692 std::stringstream request;
695 std::getline (cin, line);
700 std::cerr << line << std::endl;
702 request << line << std::endl;
709 std::cerr <<
"Processing request... ";
710 cout <<
"Server response:" << std::endl << std::flush;
712 std::string command = json[
"command"].asString();
713 CommandMap::const_iterator it = commands_.find (command);
714 if (it != commands_.end()) {
715 it->second->parseJson (json, cout);
717 cout <<
"Unknown command: `" << command <<
"'" << std::endl;
721 std::cerr <<
"done." << std::endl << std::endl;
725 typedef std::map<std::string, CommandParser*> CommandMap;
726 CommandMap commands_;
727 std::string description_;
745 template <
typename T>
747 return new Key<T> (name, destination);