You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by Jim Meyering <ji...@meyering.net> on 2006/12/05 22:43:11 UTC

better --help output for qpidd; new option --version

I started off writing patches to make qpidd's --help output regular
enough so that we can use help2man <http://www.gnu.org/software/help2man/>
to automatically generate a man page for it.

With the following patch, qpidd --help prints this:

--------------------
Usage: qpidd [OPTION]...
Start the Qpid broker daemon.

Options:
  -t, --trace
            Print incoming & outgoing frames to the console (default=false)
  -p, --port <value>
            Set the port to listen on (default=5672)
      --worker-threads <value>
            Set the number of worker threads to use (default=5).
      --max-connections <value>
            Set the maximum number of connections the broker can accept (default=500).
      --connection-backlog <value>
            Set the connection backlog for the servers socket (default=10)
  -s, --store <value>
            Set the message store module to use (default='' which implies no store)
      --help
            Print usage information
      --version
            Print version information

Report bugs to <qp...@incubator.apache.org>.
--------------------

I also added a new option, --version.
It prints using this familiar format:

  qpidd (qpid) version 0.1

That's "program_name" (package name) and the package
version number recorded early in configure.ac:

  AC_INIT([qpid], [0.1], [qpid-dev@incubator.apache.org])

This is similar to the --version output from programs like
GNU ls, cp, rm, mv, etc.

-------------------------------------------------------------------------
Here's the patch:
[I see I also removed trailing spaces in Configuration.cpp.
 If that's a problem, let me know and I'll resubmit without
 those hunks.  ]

2006-12-05  Jim Meyering  <me...@redhat.com>

	Improve --help output.  Add --version option.
	* lib/broker/Configuration.cpp: Include <config.h>.
	(Configuration::Configuration): Use the active voice.
	Handle --version.
	(Configuration::usage): Add Usage:... and bug-reporting address.
	Output short+long options like "-o, --option ...", so that help2man
	will format them properly.
	* lib/broker/Configuration.h:
	(class Configuration) [version, programName]: New members.
	(parse): Update prototype.
	(isVersion, setValue): New prototypes.
	* src/qpidd.cpp: Include <config.h>.
	(programName): New file-scoped global.
	(handle_signal, main): Emit diagnostics to stderr.
	(main): Pass program name to config.parse.
	(main): Handle new --version option.

Index: lib/broker/Configuration.cpp
===================================================================
--- lib/broker/Configuration.cpp	(revision 482785)
+++ lib/broker/Configuration.cpp	(working copy)
@@ -7,9 +7,9 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -20,18 +20,20 @@
  */
 #include <Configuration.h>
 #include <string.h>
+#include <config.h>

 using namespace qpid::broker;
 using namespace std;

-Configuration::Configuration() : 
+Configuration::Configuration() :
     trace('t', "trace", "Print incoming & outgoing frames to the console (default=false)", false),
-    port('p', "port", "Sets the port to listen on (default=5672)", 5672),
-    workerThreads("worker-threads", "Sets the number of worker threads to use (default=5).", 5),
-    maxConnections("max-connections", "Sets the maximum number of connections the broker can accept (default=500).", 500),
-    connectionBacklog("connection-backlog", "Sets the connection backlog for the servers socket (default=10)", 10),
-    store('s', "store", "Sets the message store module to use (default='' which implies no store)", ""),
-    help("help", "Prints usage information", false)
+    port('p', "port", "Set the port to listen on (default=5672)", 5672),
+    workerThreads("worker-threads", "Set the number of worker threads to use (default=5).", 5),
+    maxConnections("max-connections", "Set the maximum number of connections the broker can accept (default=500).", 500),
+    connectionBacklog("connection-backlog", "Set the connection backlog for the servers socket (default=10)", 10),
+    store('s', "store", "Set the message store module to use (default='' which implies no store)", ""),
+    help("help", "Print usage information", false),
+    version("version", "Print version information", false)
 {
     options.push_back(&trace);
     options.push_back(&port);
@@ -40,11 +42,13 @@
     options.push_back(&connectionBacklog);
     options.push_back(&store);
     options.push_back(&help);
+    options.push_back(&version);
 }

 Configuration::~Configuration(){}

-void Configuration::parse(int argc, char** argv){
+void Configuration::parse(char const *progName, int argc, char** argv){
+    programName = progName;
     int position = 1;
     while(position < argc){
         bool matched(false);
@@ -59,15 +63,25 @@
 }

 void Configuration::usage(){
+    std::cout << "Usage: " << programName << " [OPTION]..." << std::endl
+	      << "Start the Qpid broker daemon." << std::endl << std::endl
+	      << "Options:" << std::endl;
     for(op_iterator i = options.begin(); i < options.end(); i++){
         (*i)->print(std::cout);
     }
+
+    std::cout << std::endl << "Report bugs to <" << PACKAGE_BUGREPORT << ">."
+	      << std::endl;
 }

 bool Configuration::isHelp() const {
     return help.getValue();
 }

+bool Configuration::isVersion() const {
+    return version.getValue();
+}
+
 bool Configuration::isTrace() const {
     return trace.getValue();
 }
@@ -92,10 +106,10 @@
     return store.getValue();
 }

-Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) : 
+Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) :
     flag(string("-") + _flag), name("--" +_name), desc(_desc) {}

-Configuration::Option::Option(const string& _name, const string& _desc) : 
+Configuration::Option::Option(const string& _name, const string& _desc) :
     flag(""), name("--" + _name), desc(_desc) {}

 Configuration::Option::~Option(){}
@@ -121,12 +135,14 @@
 }

 void Configuration::Option::print(ostream& out) const {
-    out << "    ";
+    out << "  ";
     if(flag.length() > 0){
-        out << flag << " or ";
+        out << flag << ", ";
+    } else {
+        out << "    ";
     }
     out << name;
-    if(needsValue()) out << "<value>";
+    if(needsValue()) out << " <value>";
     out << std::endl;
     out << "            " << desc << std::endl;
 }
@@ -134,10 +150,10 @@

 // String Option:

-Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) : 
+Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) :
     Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}

-Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) : 
+Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) :
     Option(_name,_desc), defaultValue(_value), value(_value) {}

 Configuration::StringOption::~StringOption(){}
@@ -156,10 +172,10 @@

 // Int Option:

-Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) : 
+Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) :
     Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}

-Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) : 
+Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) :
     Option(_name,_desc), defaultValue(_value), value(_value) {}

 Configuration::IntOption::~IntOption(){}
@@ -178,10 +194,10 @@

 // Bool Option:

-Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) : 
+Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) :
     Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}

-Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) : 
+Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) :
     Option(_name,_desc), defaultValue(_value), value(_value) {}

 Configuration::BoolOption::~BoolOption(){}
Index: lib/broker/Configuration.h
===================================================================
--- lib/broker/Configuration.h	(revision 482785)
+++ lib/broker/Configuration.h	(working copy)
@@ -97,6 +97,8 @@
             IntOption connectionBacklog;
             StringOption store;
             BoolOption help;
+            BoolOption version;
+            char const *programName;

             typedef std::vector<Option*>::iterator op_iterator;
             std::vector<Option*> options;
@@ -111,9 +113,10 @@
             Configuration();
             ~Configuration();

-            void parse(int argc, char** argv);
+            void parse(char const*, int argc, char** argv);

             bool isHelp() const;
+            bool isVersion() const;
             bool isTrace() const;
             int getPort() const;
             int getWorkerThreads() const;
@@ -122,6 +125,7 @@
             const std::string& getStore() const;

             void setHelp(bool b) { help.setValue(b); }
+            void setVersion(bool b) { version.setValue(b); }
             void setTrace(bool b) { trace.setValue(b); }
             void setPort(int i) { port.setValue(i); }
             void setWorkerThreads(int i) { workerThreads.setValue(i); }
Index: src/qpidd.cpp
===================================================================
--- src/qpidd.cpp	(revision 482785)
+++ src/qpidd.cpp	(working copy)
@@ -7,9 +7,9 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *   http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -23,14 +23,17 @@
 #include <signal.h>
 #include <iostream>
 #include <memory>
+#include <config.h>

+static char const* programName = "qpidd";
+
 using namespace qpid::broker;
 using namespace qpid::sys;

 Broker::shared_ptr broker;

 void handle_signal(int /*signal*/){
-    std::cout << "Shutting down..." << std::endl;
+    std::cerr << "Shutting down..." << std::endl;
     broker->shutdown();
 }

@@ -38,9 +41,12 @@
 {
     Configuration config;
     try {
-        config.parse(argc, argv);
+        config.parse(programName, argc, argv);
         if(config.isHelp()){
             config.usage();
+        }else if(config.isVersion()){
+            std::cout << programName << " (" << PACKAGE_NAME << ") version "
+                      << PACKAGE_VERSION << std::endl;
         }else{
             broker = Broker::create(config);
             signal(SIGINT, handle_signal);
@@ -48,7 +54,7 @@
         }
         return 0;
     } catch(const std::exception& e) {
-        std::cout << e.what() << std::endl;
+        std::cerr << e.what() << std::endl;
     }
     return 1;
 }

Re: better --help output for qpidd; new option --version

Posted by Gordon Sim <gs...@redhat.com>.
Jim Meyering wrote:
> Here's one more piece, without which "make check" won't compile:
> 
> 2006-12-06  Jim Meyering  <ji...@meyering.net>
> 
> 	* tests/ConfigurationTest.cpp: Update conf.parse callers.

Sorry Jim, only got your mail after having committed a change for this 
along with your previous patch.

Re: better --help output for qpidd; new option --version

Posted by Jim Meyering <ji...@meyering.net>.
Carl Trieloff <cc...@redhat.com> wrote:
> Doing this patch

Thank you.

> but having build issues. will figure them out tomorrow.

Details?  Without those, it's much harder to help.

Here's one more piece, without which "make check" won't compile:

2006-12-06  Jim Meyering  <ji...@meyering.net>

	* tests/ConfigurationTest.cpp: Update conf.parse callers.

Index: tests/ConfigurationTest.cpp
===================================================================
--- tests/ConfigurationTest.cpp	(revision 482944)
+++ tests/ConfigurationTest.cpp	(working copy)
@@ -41,7 +41,7 @@
     {
         Configuration conf;
         char* argv[] = {"ignore", "--help"};
-        conf.parse(2, argv);
+        conf.parse("t1", 2, argv);
         CPPUNIT_ASSERT(conf.isHelp());
     }
 
@@ -49,7 +49,7 @@
     {
         Configuration conf;
         char* argv[] = {"ignore", "--port", "6789"};
-        conf.parse(3, argv);
+        conf.parse("t1", 3, argv);
         CPPUNIT_ASSERT_EQUAL(6789, conf.getPort());
     }
 
@@ -57,7 +57,7 @@
     {
         Configuration conf;
         char* argv[] = {"ignore", "-p", "6789"};
-        conf.parse(3, argv);
+        conf.parse("t1", 3, argv);
         CPPUNIT_ASSERT_EQUAL(6789, conf.getPort());
     }
 
@@ -65,7 +65,7 @@
     {
         Configuration conf;
         char* argv[] = {"ignore", "--store", "my-store-module.so"};
-        conf.parse(3, argv);
+        conf.parse("t1", 3, argv);
         std::string expected("my-store-module.so");
         CPPUNIT_ASSERT_EQUAL(expected, conf.getStore());
     }
@@ -74,7 +74,7 @@
     {        
         Configuration conf;
         char* argv[] = {"ignore", "-t", "--worker-threads", "10"};
-        conf.parse(4, argv);
+        conf.parse("t1", 4, argv);
         CPPUNIT_ASSERT_EQUAL(5672, conf.getPort());//default
         CPPUNIT_ASSERT_EQUAL(10, conf.getWorkerThreads());
         CPPUNIT_ASSERT(conf.isTrace());

Re: better --help output for qpidd; new option --version

Posted by Carl Trieloff <cc...@redhat.com>.
Doing this patch - but having build issues. will figure them out tomorrow.
Carl.

Jim Meyering wrote:
> I started off writing patches to make qpidd's --help output regular
> enough so that we can use help2man <http://www.gnu.org/software/help2man/>
> to automatically generate a man page for it.
>
> With the following patch, qpidd --help prints this:
>
> --------------------
> Usage: qpidd [OPTION]...
> Start the Qpid broker daemon.
>
> Options:
>   -t, --trace
>             Print incoming & outgoing frames to the console (default=false)
>   -p, --port <value>
>             Set the port to listen on (default=5672)
>       --worker-threads <value>
>             Set the number of worker threads to use (default=5).
>       --max-connections <value>
>             Set the maximum number of connections the broker can accept (default=500).
>       --connection-backlog <value>
>             Set the connection backlog for the servers socket (default=10)
>   -s, --store <value>
>             Set the message store module to use (default='' which implies no store)
>       --help
>             Print usage information
>       --version
>             Print version information
>
> Report bugs to <qp...@incubator.apache.org>.
> --------------------
>
> I also added a new option, --version.
> It prints using this familiar format:
>
>   qpidd (qpid) version 0.1
>
> That's "program_name" (package name) and the package
> version number recorded early in configure.ac:
>
>   AC_INIT([qpid], [0.1], [qpid-dev@incubator.apache.org])
>
> This is similar to the --version output from programs like
> GNU ls, cp, rm, mv, etc.
>
> -------------------------------------------------------------------------
> Here's the patch:
> [I see I also removed trailing spaces in Configuration.cpp.
>  If that's a problem, let me know and I'll resubmit without
>  those hunks.  ]
>
> 2006-12-05  Jim Meyering  <me...@redhat.com>
>
> 	Improve --help output.  Add --version option.
> 	* lib/broker/Configuration.cpp: Include <config.h>.
> 	(Configuration::Configuration): Use the active voice.
> 	Handle --version.
> 	(Configuration::usage): Add Usage:... and bug-reporting address.
> 	Output short+long options like "-o, --option ...", so that help2man
> 	will format them properly.
> 	* lib/broker/Configuration.h:
> 	(class Configuration) [version, programName]: New members.
> 	(parse): Update prototype.
> 	(isVersion, setValue): New prototypes.
> 	* src/qpidd.cpp: Include <config.h>.
> 	(programName): New file-scoped global.
> 	(handle_signal, main): Emit diagnostics to stderr.
> 	(main): Pass program name to config.parse.
> 	(main): Handle new --version option.
>
> Index: lib/broker/Configuration.cpp
> ===================================================================
> --- lib/broker/Configuration.cpp	(revision 482785)
> +++ lib/broker/Configuration.cpp	(working copy)
> @@ -7,9 +7,9 @@
>   * to you under the Apache License, Version 2.0 (the
>   * "License"); you may not use this file except in compliance
>   * with the License.  You may obtain a copy of the License at
> - * 
> + *
>   *   http://www.apache.org/licenses/LICENSE-2.0
> - * 
> + *
>   * Unless required by applicable law or agreed to in writing,
>   * software distributed under the License is distributed on an
>   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> @@ -20,18 +20,20 @@
>   */
>  #include <Configuration.h>
>  #include <string.h>
> +#include <config.h>
>
>  using namespace qpid::broker;
>  using namespace std;
>
> -Configuration::Configuration() : 
> +Configuration::Configuration() :
>      trace('t', "trace", "Print incoming & outgoing frames to the console (default=false)", false),
> -    port('p', "port", "Sets the port to listen on (default=5672)", 5672),
> -    workerThreads("worker-threads", "Sets the number of worker threads to use (default=5).", 5),
> -    maxConnections("max-connections", "Sets the maximum number of connections the broker can accept (default=500).", 500),
> -    connectionBacklog("connection-backlog", "Sets the connection backlog for the servers socket (default=10)", 10),
> -    store('s', "store", "Sets the message store module to use (default='' which implies no store)", ""),
> -    help("help", "Prints usage information", false)
> +    port('p', "port", "Set the port to listen on (default=5672)", 5672),
> +    workerThreads("worker-threads", "Set the number of worker threads to use (default=5).", 5),
> +    maxConnections("max-connections", "Set the maximum number of connections the broker can accept (default=500).", 500),
> +    connectionBacklog("connection-backlog", "Set the connection backlog for the servers socket (default=10)", 10),
> +    store('s', "store", "Set the message store module to use (default='' which implies no store)", ""),
> +    help("help", "Print usage information", false),
> +    version("version", "Print version information", false)
>  {
>      options.push_back(&trace);
>      options.push_back(&port);
> @@ -40,11 +42,13 @@
>      options.push_back(&connectionBacklog);
>      options.push_back(&store);
>      options.push_back(&help);
> +    options.push_back(&version);
>  }
>
>  Configuration::~Configuration(){}
>
> -void Configuration::parse(int argc, char** argv){
> +void Configuration::parse(char const *progName, int argc, char** argv){
> +    programName = progName;
>      int position = 1;
>      while(position < argc){
>          bool matched(false);
> @@ -59,15 +63,25 @@
>  }
>
>  void Configuration::usage(){
> +    std::cout << "Usage: " << programName << " [OPTION]..." << std::endl
> +	      << "Start the Qpid broker daemon." << std::endl << std::endl
> +	      << "Options:" << std::endl;
>      for(op_iterator i = options.begin(); i < options.end(); i++){
>          (*i)->print(std::cout);
>      }
> +
> +    std::cout << std::endl << "Report bugs to <" << PACKAGE_BUGREPORT << ">."
> +	      << std::endl;
>  }
>
>  bool Configuration::isHelp() const {
>      return help.getValue();
>  }
>
> +bool Configuration::isVersion() const {
> +    return version.getValue();
> +}
> +
>  bool Configuration::isTrace() const {
>      return trace.getValue();
>  }
> @@ -92,10 +106,10 @@
>      return store.getValue();
>  }
>
> -Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) : 
> +Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) :
>      flag(string("-") + _flag), name("--" +_name), desc(_desc) {}
>
> -Configuration::Option::Option(const string& _name, const string& _desc) : 
> +Configuration::Option::Option(const string& _name, const string& _desc) :
>      flag(""), name("--" + _name), desc(_desc) {}
>
>  Configuration::Option::~Option(){}
> @@ -121,12 +135,14 @@
>  }
>
>  void Configuration::Option::print(ostream& out) const {
> -    out << "    ";
> +    out << "  ";
>      if(flag.length() > 0){
> -        out << flag << " or ";
> +        out << flag << ", ";
> +    } else {
> +        out << "    ";
>      }
>      out << name;
> -    if(needsValue()) out << "<value>";
> +    if(needsValue()) out << " <value>";
>      out << std::endl;
>      out << "            " << desc << std::endl;
>  }
> @@ -134,10 +150,10 @@
>
>  // String Option:
>
> -Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) : 
> +Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
>
> -Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) : 
> +Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
>
>  Configuration::StringOption::~StringOption(){}
> @@ -156,10 +172,10 @@
>
>  // Int Option:
>
> -Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) : 
> +Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
>
> -Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) : 
> +Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
>
>  Configuration::IntOption::~IntOption(){}
> @@ -178,10 +194,10 @@
>
>  // Bool Option:
>
> -Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) : 
> +Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
>
> -Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) : 
> +Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
>
>  Configuration::BoolOption::~BoolOption(){}
> Index: lib/broker/Configuration.h
> ===================================================================
> --- lib/broker/Configuration.h	(revision 482785)
> +++ lib/broker/Configuration.h	(working copy)
> @@ -97,6 +97,8 @@
>              IntOption connectionBacklog;
>              StringOption store;
>              BoolOption help;
> +            BoolOption version;
> +            char const *programName;
>
>              typedef std::vector<Option*>::iterator op_iterator;
>              std::vector<Option*> options;
> @@ -111,9 +113,10 @@
>              Configuration();
>              ~Configuration();
>
> -            void parse(int argc, char** argv);
> +            void parse(char const*, int argc, char** argv);
>
>              bool isHelp() const;
> +            bool isVersion() const;
>              bool isTrace() const;
>              int getPort() const;
>              int getWorkerThreads() const;
> @@ -122,6 +125,7 @@
>              const std::string& getStore() const;
>
>              void setHelp(bool b) { help.setValue(b); }
> +            void setVersion(bool b) { version.setValue(b); }
>              void setTrace(bool b) { trace.setValue(b); }
>              void setPort(int i) { port.setValue(i); }
>              void setWorkerThreads(int i) { workerThreads.setValue(i); }
> Index: src/qpidd.cpp
> ===================================================================
> --- src/qpidd.cpp	(revision 482785)
> +++ src/qpidd.cpp	(working copy)
> @@ -7,9 +7,9 @@
>   * to you under the Apache License, Version 2.0 (the
>   * "License"); you may not use this file except in compliance
>   * with the License.  You may obtain a copy of the License at
> - * 
> + *
>   *   http://www.apache.org/licenses/LICENSE-2.0
> - * 
> + *
>   * Unless required by applicable law or agreed to in writing,
>   * software distributed under the License is distributed on an
>   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> @@ -23,14 +23,17 @@
>  #include <signal.h>
>  #include <iostream>
>  #include <memory>
> +#include <config.h>
>
> +static char const* programName = "qpidd";
> +
>  using namespace qpid::broker;
>  using namespace qpid::sys;
>
>  Broker::shared_ptr broker;
>
>  void handle_signal(int /*signal*/){
> -    std::cout << "Shutting down..." << std::endl;
> +    std::cerr << "Shutting down..." << std::endl;
>      broker->shutdown();
>  }
>
> @@ -38,9 +41,12 @@
>  {
>      Configuration config;
>      try {
> -        config.parse(argc, argv);
> +        config.parse(programName, argc, argv);
>          if(config.isHelp()){
>              config.usage();
> +        }else if(config.isVersion()){
> +            std::cout << programName << " (" << PACKAGE_NAME << ") version "
> +                      << PACKAGE_VERSION << std::endl;
>          }else{
>              broker = Broker::create(config);
>              signal(SIGINT, handle_signal);
> @@ -48,7 +54,7 @@
>          }
>          return 0;
>      } catch(const std::exception& e) {
> -        std::cout << e.what() << std::endl;
> +        std::cerr << e.what() << std::endl;
>      }
>      return 1;
>  }
>   


Re: better --help output for qpidd; new option --version

Posted by Gordon Sim <gs...@redhat.com>.
Jim Meyering wrote:
> I started off writing patches to make qpidd's --help output regular
> enough so that we can use help2man <http://www.gnu.org/software/help2man/>
> to automatically generate a man page for it.
[...]
> -------------------------------------------------------------------------
> Here's the patch:
> [I see I also removed trailing spaces in Configuration.cpp.
>  If that's a problem, let me know and I'll resubmit without
>  those hunks.  ]

Thanks, Jim! Applied as rev. 482958 (which includes trivial fixes to 
tests/ConfigurationTest.cpp as well).

> 2006-12-05  Jim Meyering  <me...@redhat.com>
> 
> 	Improve --help output.  Add --version option.
> 	* lib/broker/Configuration.cpp: Include <config.h>.
> 	(Configuration::Configuration): Use the active voice.
> 	Handle --version.
> 	(Configuration::usage): Add Usage:... and bug-reporting address.
> 	Output short+long options like "-o, --option ...", so that help2man
> 	will format them properly.
> 	* lib/broker/Configuration.h:
> 	(class Configuration) [version, programName]: New members.
> 	(parse): Update prototype.
> 	(isVersion, setValue): New prototypes.
> 	* src/qpidd.cpp: Include <config.h>.
> 	(programName): New file-scoped global.
> 	(handle_signal, main): Emit diagnostics to stderr.
> 	(main): Pass program name to config.parse.
> 	(main): Handle new --version option.
> 
> Index: lib/broker/Configuration.cpp
> ===================================================================
> --- lib/broker/Configuration.cpp	(revision 482785)
> +++ lib/broker/Configuration.cpp	(working copy)
> @@ -7,9 +7,9 @@
>   * to you under the Apache License, Version 2.0 (the
>   * "License"); you may not use this file except in compliance
>   * with the License.  You may obtain a copy of the License at
> - * 
> + *
>   *   http://www.apache.org/licenses/LICENSE-2.0
> - * 
> + *
>   * Unless required by applicable law or agreed to in writing,
>   * software distributed under the License is distributed on an
>   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> @@ -20,18 +20,20 @@
>   */
>  #include <Configuration.h>
>  #include <string.h>
> +#include <config.h>
> 
>  using namespace qpid::broker;
>  using namespace std;
> 
> -Configuration::Configuration() : 
> +Configuration::Configuration() :
>      trace('t', "trace", "Print incoming & outgoing frames to the console (default=false)", false),
> -    port('p', "port", "Sets the port to listen on (default=5672)", 5672),
> -    workerThreads("worker-threads", "Sets the number of worker threads to use (default=5).", 5),
> -    maxConnections("max-connections", "Sets the maximum number of connections the broker can accept (default=500).", 500),
> -    connectionBacklog("connection-backlog", "Sets the connection backlog for the servers socket (default=10)", 10),
> -    store('s', "store", "Sets the message store module to use (default='' which implies no store)", ""),
> -    help("help", "Prints usage information", false)
> +    port('p', "port", "Set the port to listen on (default=5672)", 5672),
> +    workerThreads("worker-threads", "Set the number of worker threads to use (default=5).", 5),
> +    maxConnections("max-connections", "Set the maximum number of connections the broker can accept (default=500).", 500),
> +    connectionBacklog("connection-backlog", "Set the connection backlog for the servers socket (default=10)", 10),
> +    store('s', "store", "Set the message store module to use (default='' which implies no store)", ""),
> +    help("help", "Print usage information", false),
> +    version("version", "Print version information", false)
>  {
>      options.push_back(&trace);
>      options.push_back(&port);
> @@ -40,11 +42,13 @@
>      options.push_back(&connectionBacklog);
>      options.push_back(&store);
>      options.push_back(&help);
> +    options.push_back(&version);
>  }
> 
>  Configuration::~Configuration(){}
> 
> -void Configuration::parse(int argc, char** argv){
> +void Configuration::parse(char const *progName, int argc, char** argv){
> +    programName = progName;
>      int position = 1;
>      while(position < argc){
>          bool matched(false);
> @@ -59,15 +63,25 @@
>  }
> 
>  void Configuration::usage(){
> +    std::cout << "Usage: " << programName << " [OPTION]..." << std::endl
> +	      << "Start the Qpid broker daemon." << std::endl << std::endl
> +	      << "Options:" << std::endl;
>      for(op_iterator i = options.begin(); i < options.end(); i++){
>          (*i)->print(std::cout);
>      }
> +
> +    std::cout << std::endl << "Report bugs to <" << PACKAGE_BUGREPORT << ">."
> +	      << std::endl;
>  }
> 
>  bool Configuration::isHelp() const {
>      return help.getValue();
>  }
> 
> +bool Configuration::isVersion() const {
> +    return version.getValue();
> +}
> +
>  bool Configuration::isTrace() const {
>      return trace.getValue();
>  }
> @@ -92,10 +106,10 @@
>      return store.getValue();
>  }
> 
> -Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) : 
> +Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) :
>      flag(string("-") + _flag), name("--" +_name), desc(_desc) {}
> 
> -Configuration::Option::Option(const string& _name, const string& _desc) : 
> +Configuration::Option::Option(const string& _name, const string& _desc) :
>      flag(""), name("--" + _name), desc(_desc) {}
> 
>  Configuration::Option::~Option(){}
> @@ -121,12 +135,14 @@
>  }
> 
>  void Configuration::Option::print(ostream& out) const {
> -    out << "    ";
> +    out << "  ";
>      if(flag.length() > 0){
> -        out << flag << " or ";
> +        out << flag << ", ";
> +    } else {
> +        out << "    ";
>      }
>      out << name;
> -    if(needsValue()) out << "<value>";
> +    if(needsValue()) out << " <value>";
>      out << std::endl;
>      out << "            " << desc << std::endl;
>  }
> @@ -134,10 +150,10 @@
> 
>  // String Option:
> 
> -Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) : 
> +Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
> 
> -Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) : 
> +Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
> 
>  Configuration::StringOption::~StringOption(){}
> @@ -156,10 +172,10 @@
> 
>  // Int Option:
> 
> -Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) : 
> +Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
> 
> -Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) : 
> +Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
> 
>  Configuration::IntOption::~IntOption(){}
> @@ -178,10 +194,10 @@
> 
>  // Bool Option:
> 
> -Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) : 
> +Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) :
>      Option(_flag,_name,_desc), defaultValue(_value), value(_value) {}
> 
> -Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) : 
> +Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) :
>      Option(_name,_desc), defaultValue(_value), value(_value) {}
> 
>  Configuration::BoolOption::~BoolOption(){}
> Index: lib/broker/Configuration.h
> ===================================================================
> --- lib/broker/Configuration.h	(revision 482785)
> +++ lib/broker/Configuration.h	(working copy)
> @@ -97,6 +97,8 @@
>              IntOption connectionBacklog;
>              StringOption store;
>              BoolOption help;
> +            BoolOption version;
> +            char const *programName;
> 
>              typedef std::vector<Option*>::iterator op_iterator;
>              std::vector<Option*> options;
> @@ -111,9 +113,10 @@
>              Configuration();
>              ~Configuration();
> 
> -            void parse(int argc, char** argv);
> +            void parse(char const*, int argc, char** argv);
> 
>              bool isHelp() const;
> +            bool isVersion() const;
>              bool isTrace() const;
>              int getPort() const;
>              int getWorkerThreads() const;
> @@ -122,6 +125,7 @@
>              const std::string& getStore() const;
> 
>              void setHelp(bool b) { help.setValue(b); }
> +            void setVersion(bool b) { version.setValue(b); }
>              void setTrace(bool b) { trace.setValue(b); }
>              void setPort(int i) { port.setValue(i); }
>              void setWorkerThreads(int i) { workerThreads.setValue(i); }
> Index: src/qpidd.cpp
> ===================================================================
> --- src/qpidd.cpp	(revision 482785)
> +++ src/qpidd.cpp	(working copy)
> @@ -7,9 +7,9 @@
>   * to you under the Apache License, Version 2.0 (the
>   * "License"); you may not use this file except in compliance
>   * with the License.  You may obtain a copy of the License at
> - * 
> + *
>   *   http://www.apache.org/licenses/LICENSE-2.0
> - * 
> + *
>   * Unless required by applicable law or agreed to in writing,
>   * software distributed under the License is distributed on an
>   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> @@ -23,14 +23,17 @@
>  #include <signal.h>
>  #include <iostream>
>  #include <memory>
> +#include <config.h>
> 
> +static char const* programName = "qpidd";
> +
>  using namespace qpid::broker;
>  using namespace qpid::sys;
> 
>  Broker::shared_ptr broker;
> 
>  void handle_signal(int /*signal*/){
> -    std::cout << "Shutting down..." << std::endl;
> +    std::cerr << "Shutting down..." << std::endl;
>      broker->shutdown();
>  }
> 
> @@ -38,9 +41,12 @@
>  {
>      Configuration config;
>      try {
> -        config.parse(argc, argv);
> +        config.parse(programName, argc, argv);
>          if(config.isHelp()){
>              config.usage();
> +        }else if(config.isVersion()){
> +            std::cout << programName << " (" << PACKAGE_NAME << ") version "
> +                      << PACKAGE_VERSION << std::endl;
>          }else{
>              broker = Broker::create(config);
>              signal(SIGINT, handle_signal);
> @@ -48,7 +54,7 @@
>          }
>          return 0;
>      } catch(const std::exception& e) {
> -        std::cout << e.what() << std::endl;
> +        std::cerr << e.what() << std::endl;
>      }
>      return 1;
>  }