[contrib/cli] introduce usage method (#425)
author박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 3 Jul 2018 00:49:52 +0000 (09:49 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 3 Jul 2018 00:49:52 +0000 (09:49 +0900)
* [contrib/cli] introduce show_usage method

This will introduce show_usage() method in cli App class
- purpose is to show provided commands when no command is given

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
* rename to usage()

contrib/cli/include/cli/App.h
contrib/cli/src/App.cpp

index 39ee86d..7ee99fc 100644 (file)
@@ -22,6 +22,9 @@ public:
   int run(int argc, const char * const *argv) const;
 
 private:
+  void usage(std::ostream& os) const;
+
+private:
   const std::string _name;
   std::map<std::string, std::unique_ptr<Command>> _commands;
 };
index 56119f8..39971b0 100644 (file)
@@ -25,8 +25,7 @@ int App::run(int argc, const char * const *argv) const
   if (argc < 1)
   {
     std::cerr << "ERROR: COMMAND is not provided" << std::endl;
-    std::cerr << std::endl;
-    std::cerr << "USAGE: " << _name << " [COMMAND] ..." << std::endl;
+    usage(std::cerr);
     return 255;
   }
 
@@ -37,18 +36,23 @@ int App::run(int argc, const char * const *argv) const
   if (it == _commands.end())
   {
     std::cerr << "ERROR: '" << command << "' is not a valid command" << std::endl;
-    std::cerr << std::endl;
-    std::cerr << "USAGE: " << _name << " [COMMAND] ..." << std::endl;
-    std::cerr << std::endl;
-    std::cerr << "SUPPORTED COMMANDS:" << std::endl;
-    for (auto it = _commands.begin(); it != _commands.end(); ++it)
-    {
-      std::cerr << "  " << it->first << std::endl;
-    }
+    usage(std::cerr);
     return 255;
   }
 
   return it->second->run(argc - 1, argv + 1);
 }
 
+void App::usage(std::ostream& os) const
+{
+    os << std::endl;
+    os << "USAGE: " << _name << " [COMMAND] ..." << std::endl;
+    os << std::endl;
+    os << "SUPPORTED COMMANDS:" << std::endl;
+    for (auto it = _commands.begin(); it != _commands.end(); ++it)
+    {
+      os << "  " << it->first << std::endl;
+    }
+}
+
 } // namespace cli