]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.h
Style updates
[libs/core.git] / source / core / getopt.h
index 94e8b1e4af35e598a6e1f99600f837aef916cfbb..8e0f49a3fb9d12c83d986289e6cf69d5fc10cee9 100644 (file)
@@ -1,9 +1,10 @@
 /* $Id$
 
 This file is part of libmspcore
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
+
 #ifndef MSP_CORE_GETOPT_H_
 #define MSP_CORE_GETOPT_H_
 
@@ -26,29 +27,31 @@ public:
        
        class OptBase
        {
-       public:
-               OptBase           &set_help(const std::string &);
-               OptBase           &set_help(const std::string &, const std::string &);
-               char              get_short() const               { return shrt; }
-               const std::string &get_long() const               { return lng; }
-               ArgType           get_arg_type() const            { return arg_type; }
-               const std::string &get_help() const               { return help; }
-               const std::string &get_metavar() const            { return metavar; }
-               unsigned          get_seen_count() const          { return seen_count; }
-               void              process();
-               void              process(const std::string &);
-               virtual ~OptBase() { }
        protected:
-               char        shrt;
+               char shrt;
                std::string lng;
-               ArgType     arg_type;
-               unsigned    seen_count;
+               ArgType arg_type;
+               unsigned seen_count;
                std::string help;
                std::string metavar;
 
                OptBase(char, const std::string &, ArgType);
-               virtual void store()=0;
-               virtual void store(const std::string &)=0;
+       public:
+               virtual ~OptBase() { }
+
+               OptBase &set_help(const std::string &);
+               OptBase &set_help(const std::string &, const std::string &);
+               char get_short() const { return shrt; }
+               const std::string &get_long() const { return lng; }
+               ArgType get_arg_type() const { return arg_type; }
+               const std::string &get_help() const { return help; }
+               const std::string &get_metavar() const { return metavar; }
+               unsigned get_seen_count() const { return seen_count; }
+               void process();
+               void process(const std::string &);
+       protected:
+               virtual void store() = 0;
+               virtual void store(const std::string &) = 0;
        };
 
 private:
@@ -68,7 +71,7 @@ private:
                        if(ss.fail())
                                throw UsageError("Invalid argument for --"+lng);
 
-                       data=tmp;
+                       data = tmp;
                }
        private:
                T &data;
@@ -106,33 +109,47 @@ public:
        const std::vector<std::string> &get_args() const { return args; }
 
        template<typename T>
-       OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG)
+       OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
        { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
        
        template<typename T>
-       OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a=REQUIRED_ARG)
+       OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
        { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
        
        template<typename T>
        OptBase &add_option(const std::string &l, T &d, ArgType a)
        { return add_option(0, l, d, a); }
 
-       std::string generate_usage(const std::string &) const;
-       std::string generate_help() const;
-       void operator()(unsigned, const char *const *);
 private:
-
        OptBase &get_option(char);
        OptBase &get_option(const std::string &);
+
+public:
+       /** Processes argc/argv style command line arguments.  The contents of argv
+       will be unchanged; use get_args to access non-option arguments. */
+       void operator()(unsigned, const char *const *);
+
+private:
+       /** Processes a long option.  Returns the number of arguments eaten. */
        unsigned process_long(const char *const *);
+
+       /** Processes short options.  Returns the number of arguments eaten. */
        unsigned process_short(const char *const *);
+
+public:
+       /** Generates a single line that describes known options. */
+       std::string generate_usage(const std::string &) const;
+
+       /** Generates help for known options in tabular format, one option per
+       line.  The returned string will have a linefeed at the end. */
+       std::string generate_help() const;
 };
 
-template<> inline void GetOpt::Option<bool>::store()     { data=true; }
+template<> inline void GetOpt::Option<bool>::store()     { data = true; }
 template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
 
 template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
-{ data=a; }
+{ data = a; }
 
 template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
 { data.push_back(a); }