From f472ae0e9c75244762523ab1ff56a4a6223c4cf5 Mon Sep 17 00:00:00 2001 From: Jon TURNEY Date: Thu, 23 Feb 2012 14:31:23 +0000 Subject: [PATCH] Add infrastructure for handling string options which are repeated 2011-05-30 SZAVAI Gyula * libgetopt++/src/StringArrayOption.cc: New file. * libgetopt++/include/getopt++/StringArrayOption.h: New file. * libgetopt++/Makefile.am: Add new files. --- libgetopt++/ChangeLog | 5 ++ libgetopt++/Makefile.am | 3 +- .../include/getopt++/StringArrayOption.h | 44 ++++++++++++ libgetopt++/src/StringArrayOption.cc | 68 +++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 libgetopt++/include/getopt++/StringArrayOption.h create mode 100644 libgetopt++/src/StringArrayOption.cc diff --git a/libgetopt++/ChangeLog b/libgetopt++/ChangeLog index 14e4554c..6c872c47 100644 --- a/libgetopt++/ChangeLog +++ b/libgetopt++/ChangeLog @@ -1,3 +1,8 @@ +2011-05-30 SZAVAI Gyula + * libgetopt++/src/StringArrayOption.cc: New file. + * libgetopt++/include/getopt++/StringArrayOption.h: New file. + * libgetopt++/Makefile.am: Add new files. + 2008-06-16 Dave Korn * libgetopt++/src/OptionSet.cc (OptionSet::doOption): Move value diff --git a/libgetopt++/Makefile.am b/libgetopt++/Makefile.am index 1ce48eb1..3da877d8 100644 --- a/libgetopt++/Makefile.am +++ b/libgetopt++/Makefile.am @@ -26,7 +26,7 @@ TESTS = tests/OptionSet tests/optioniterator tests/BoolOptionTest libgetopt___la_SOURCES = src/GetOption.cc src/Option.cc src/BoolOption.cc \ src/OptionSet.cc \ - src/StringOption.cc + src/StringArrayOption.cc src/StringOption.cc libgetopt___la_LDFLAGS = -version-info 1:1:0 @@ -35,6 +35,7 @@ getoptinclude_HEADERS = include/getopt++/Option.h \ include/getopt++/DefaultFormatter.h \ include/getopt++/GetOption.h \ include/getopt++/OptionSet.h \ + include/getopt++/StringArrayOption.h \ include/getopt++/StringOption.h tests_testoption_SOURCES = tests/testoption.cc diff --git a/libgetopt++/include/getopt++/StringArrayOption.h b/libgetopt++/include/getopt++/StringArrayOption.h new file mode 100644 index 00000000..d3f87c0d --- /dev/null +++ b/libgetopt++/include/getopt++/StringArrayOption.h @@ -0,0 +1,44 @@ +/* + * Modified from StringOption.h by Szavai Gyula in 2011 + * + * Copyright (c) 2002 Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + */ + +#ifndef _STRINGARRAYOPTION_H_ +#define _STRINGARRAYOPTION_H_ + +#include +#include + +// Each registered option must implement this class. +class StringArrayOption : public Option +{ +public: + StringArrayOption(char shortopt, char const *longopt = 0, + std::string const &shorthelp = std::string(), + OptionSet &owner=GetOption::GetInstance()); + virtual ~ StringArrayOption (); + virtual std::string const shortOption () const; + virtual std::string const longOption () const; + virtual std::string const shortHelp () const; + virtual Result Process (char const *); + virtual Argument argument () const; + operator std::vector () const; + +private: + Argument _optional; + std::vector _value; + char _shortopt; + char const *_longopt; + std::string _shorthelp; +}; + +#endif // _STRINGARRAYOPTION_H_ diff --git a/libgetopt++/src/StringArrayOption.cc b/libgetopt++/src/StringArrayOption.cc new file mode 100644 index 00000000..79ba88c1 --- /dev/null +++ b/libgetopt++/src/StringArrayOption.cc @@ -0,0 +1,68 @@ +/* + * Modified from StringOption.cc by Szavai Gyula in 2011 + * + * Copyright (c) 2002 Robert Collins. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * A copy of the GNU General Public License can be found at + * http://www.gnu.org/ + */ + +#include + +using namespace std; + +StringArrayOption::StringArrayOption(char shortopt, + char const *longopt, string const &shorthelp, + OptionSet &owner) : + _optional(Required), _shortopt(shortopt), + _longopt (longopt), _shorthelp (shorthelp) +{ + owner.Register (this); +}; + +StringArrayOption::~ StringArrayOption () {}; + +string const +StringArrayOption::shortOption () const +{ + return string() + _shortopt + ":"; +} + +string const +StringArrayOption::longOption () const +{ + return _longopt; +} + +string const +StringArrayOption::shortHelp () const +{ + return _shorthelp; +} + +Option::Result +StringArrayOption::Process (char const *optarg) +{ + if (optarg) + { + _value.push_back(optarg); + return Ok; + } + return Failed; +} + +StringArrayOption::operator vector () const +{ + return _value; +} + +Option::Argument +StringArrayOption::argument () const +{ + return _optional; +} -- 2.43.5