]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Replace earlier options sharing the same short or long name
[libs/core.git] / source / core / getopt.cpp
index 9b8b84cdee323bc1c83f2ebb35dd69915b2f9a00..911b673d9b1d28358966a302de44aaf6428735e3 100644 (file)
@@ -1,10 +1,4 @@
-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/strings/format.h>
 #include "getopt.h"
 
 using namespace std;
@@ -23,6 +17,23 @@ GetOpt::~GetOpt()
                delete *i;
 }
 
+GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
+{
+       for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); )
+       {
+               if((*i)->get_short()==opt->get_short() || (*i)->get_long()==opt->get_long())
+               {
+                       delete *i;
+                       opts.erase(i++);
+               }
+               else
+                       ++i;
+       }
+
+       opts.push_back(opt);
+       return *opts.back();
+}
+
 GetOpt::OptBase &GetOpt::get_option(char s)
 {
        for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
@@ -137,35 +148,33 @@ unsigned GetOpt::process_short(const char *const *argp)
 
 string GetOpt::generate_usage(const string &argv0) const
 {
-       ostringstream line;
-       
-       line<<argv0;
+       string result = argv0;
        for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
        {
-               line<<" [";
+               result += " [";
                if((*i)->get_short())
                {
-                       line<<'-'<<(*i)->get_short();
+                       result += format("-%c", (*i)->get_short());
                        if(!(*i)->get_long().empty())
-                               line<<'|';
+                               result += '|';
                        else if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               line<<'['<<(*i)->get_metavar()<<']';
+                               result += format("[%s]", (*i)->get_metavar());
                        else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               line<<' '<<(*i)->get_metavar();
+                               result += format(" %s", (*i)->get_metavar());
                }
                if(!(*i)->get_long().empty())
                {
-                       line<<"--"<<(*i)->get_long();
+                       result += format("--%s", (*i)->get_long());
 
                        if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               line<<"[="<<(*i)->get_metavar()<<']';
+                               result += format("[=%s]", (*i)->get_metavar());
                        else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               line<<'='<<(*i)->get_metavar();
+                               result += format("=%s", (*i)->get_metavar());
                }
-               line<<']';
+               result += ']';
        }
 
-       return line.str();
+       return result;
 }
 
 string GetOpt::generate_help() const
@@ -178,41 +187,36 @@ string GetOpt::generate_help() const
        list<string> switches;
        for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
        {
-               ostringstream swtch;
+               string swtch;
                if((*i)->get_short())
                {
-                       swtch<<'-'<<(*i)->get_short();
+                       swtch += format("-%c", (*i)->get_short());
                        if(!(*i)->get_long().empty())
-                               swtch<<", ";
+                               swtch += ", ";
                        else if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               swtch<<'['<<(*i)->get_metavar()<<']';
+                               swtch += format("[%s]", (*i)->get_metavar());
                        else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               swtch<<' '<<(*i)->get_metavar();
+                               swtch += format(" %s", (*i)->get_metavar());
                }
                else if(any_short)
-                       swtch<<"    ";
+                       swtch += "    ";
                if(!(*i)->get_long().empty())
                {
-                       swtch<<"--"<<(*i)->get_long();
+                       swtch += format("--%s", (*i)->get_long());
 
                        if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               swtch<<"[="<<(*i)->get_metavar()<<']';
+                               swtch += format("[=%s]", (*i)->get_metavar());
                        else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               swtch<<'='<<(*i)->get_metavar();
+                               swtch += format("=%s", (*i)->get_metavar());
                }
-               switches.push_back(swtch.str());
-               maxw = max(maxw, switches.back().size());
+               switches.push_back(swtch);
+               maxw = max(maxw, swtch.size());
        }
 
        string result;
        list<string>::const_iterator j = switches.begin();
        for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
-       {
-               result += "  "+*j;
-               result += string(maxw+2-j->size(), ' ');
-               result += (*i)->get_help();
-               result += '\n';
-       }
+               result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
        
        return result;
 }
@@ -224,7 +228,10 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
        arg_type(a),
        seen_count(0),
        metavar("ARG")
-{ }
+{
+       if(lng.empty())
+               throw invalid_argument("empty long option name");
+}
 
 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
 {