[Lemon-commits] Alpar Juttner: Port arg_parser from svn -r3438

Lemon HG hg at lemon.cs.elte.hu
Mon Mar 17 12:05:31 CET 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/3453d20a82cd
changeset: 85:3453d20a82cd
user:      Alpar Juttner <alpar [at] cs.elte.hu>
date:      Tue Jan 22 15:55:49 2008 +0000
description:
	Port arg_parser from svn -r3438

diffstat:

6 files changed, 925 insertions(+), 2 deletions(-)
.hgignore               |    3 
demo/Makefile.am        |    6 
demo/arg_parser_demo.cc |   79 +++++++
lemon/Makefile.am       |    2 
lemon/arg_parser.cc     |  469 +++++++++++++++++++++++++++++++++++++++++++++++
lemon/arg_parser.h      |  368 ++++++++++++++++++++++++++++++++++++

diffs (truncated from 973 to 300 lines):

diff -r 4ab73d92f0fb -r 3453d20a82cd .hgignore
--- a/.hgignore	Tue Jan 22 10:59:14 2008 +0000
+++ b/.hgignore	Tue Jan 22 15:55:49 2008 +0000
@@ -31,4 +31,5 @@ syntax: regexp
 ^autom4te.cache/.*
 ^build-aux/.*
 ^objs.*/.*
-^test/[a-z_]*$
\ No newline at end of file
+^test/[a-z_]*$
+^demo/.*_demo$
diff -r 4ab73d92f0fb -r 3453d20a82cd demo/Makefile.am
--- a/demo/Makefile.am	Tue Jan 22 10:59:14 2008 +0000
+++ b/demo/Makefile.am	Tue Jan 22 15:55:49 2008 +0000
@@ -3,6 +3,10 @@ EXTRA_DIST += \
 
 if WANT_DEMO
 
-noinst_PROGRAMS +=
+noinst_PROGRAMS += \
+        demo/arg_parser_demo
 
 endif WANT_DEMO
+
+demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
+
diff -r 4ab73d92f0fb -r 3453d20a82cd demo/arg_parser_demo.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/arg_parser_demo.cc	Tue Jan 22 15:55:49 2008 +0000
@@ -0,0 +1,79 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2008
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+///\ingroup demos
+///\file
+///\brief Argument parser demo
+///
+/// This example shows how can the argument parser used.
+///
+/// \include arg_parser.cc
+
+#include <lemon/arg_parser.h>
+
+using namespace lemon;
+int main(int argc, const char **argv)
+{
+  ArgParser ap(argc,argv);
+  int i;
+  std::string s;
+  double d;
+  bool b,sil;
+  bool g1,g2,g3;
+  ap.refOption("n", "an integer input", i, true)
+    .refOption("val", "a double input", d)
+    .synonym("vals","val")
+    .refOption("name", "a string input", s)
+    .refOption("f", "a switch", b)
+    .refOption("nohelp", "", sil)
+    .refOption("gra","Choise A",g1)
+    .refOption("grb","Choise B",g2)
+    .refOption("grc","Choise C",g3)
+    .optionGroup("gr","gra")
+    .optionGroup("gr","grb")
+    .optionGroup("gr","grc")
+    .mandatoryGroup("gr")
+    .onlyOneGroup("gr")
+    .other("infile","The input file")
+    .other("...");
+  
+  ap.parse();
+
+  std::cout << "Parameters of '" << ap.commandName() << "':\n";
+
+  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
+  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
+  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
+  if(ap.given("f")) std::cout << "  -f is given\n";
+  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
+
+  switch(ap.files().size()) {
+  case 0:
+    std::cout << "  No file argument was given.\n";
+    break;
+  case 1:
+    std::cout << "  1 file argument was given. It is:\n";
+    break;
+  default:
+    std::cout << "  "
+	      << ap.files().size() << " file arguments were given. They are:\n";
+  }
+  for(unsigned int i=0;i<ap.files().size();++i)
+    std::cout << "    '" << ap.files()[i] << "'\n";
+  
+}
diff -r 4ab73d92f0fb -r 3453d20a82cd lemon/Makefile.am
--- a/lemon/Makefile.am	Tue Jan 22 10:59:14 2008 +0000
+++ b/lemon/Makefile.am	Tue Jan 22 15:55:49 2008 +0000
@@ -7,6 +7,7 @@ lib_LTLIBRARIES += lemon/libemon.la
 lib_LTLIBRARIES += lemon/libemon.la
 
 lemon_libemon_la_SOURCES = \
+        lemon/arg_parser.cc \
         lemon/base.cc \
         lemon/random.cc
 
@@ -15,6 +16,7 @@ lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) 
 lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS)
 
 lemon_HEADERS += \
+        lemon/arg_parser.h \
         lemon/dim2.h \
 	lemon/maps.h \
         lemon/random.h \
diff -r 4ab73d92f0fb -r 3453d20a82cd lemon/arg_parser.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/arg_parser.cc	Tue Jan 22 15:55:49 2008 +0000
@@ -0,0 +1,469 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2008
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#include <lemon/arg_parser.h>
+
+namespace lemon {
+
+  void ArgParser::_showHelp(void *p)
+  {
+    (static_cast<ArgParser*>(p))->showHelp();
+    exit(1);
+  }
+
+  ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
+                                                     _command_name(argv[0]) {
+    funcOption("-help","Print a short help message",_showHelp,this);
+    synonym("help","-help");
+    synonym("h","-help");
+
+  }
+
+  ArgParser::~ArgParser()
+  {
+    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
+      if(i->second.self_delete)
+	switch(i->second.type) {
+	case BOOL:
+	  delete i->second.bool_p;
+	  break;
+	case STRING:
+	  delete i->second.string_p;
+	  break;
+	case DOUBLE:
+	  delete i->second.double_p;
+	  break;
+	case INTEGER:
+	  delete i->second.int_p;
+	  break;
+	case UNKNOWN:
+	  break;
+	case FUNC:
+	  break;
+	}
+  }
+  
+
+  ArgParser &ArgParser::intOption(const std::string &name,
+			       const std::string &help,
+			       int value, bool obl)
+  {
+    ParData p;
+    p.int_p=new int(value);
+    p.self_delete=true;
+    p.help=help;
+    p.type=INTEGER;
+    p.mandatory=obl;
+    p.self_delete=true;
+    _opts[name]=p;
+    return *this;
+  }
+
+  ArgParser &ArgParser::doubleOption(const std::string &name,
+			       const std::string &help,
+			       double value, bool obl)
+  {
+    ParData p;
+    p.double_p=new double(value);
+    p.self_delete=true;
+    p.help=help;
+    p.type=DOUBLE;
+    p.mandatory=obl;
+    _opts[name]=p;
+    return *this;
+  }
+
+  ArgParser &ArgParser::boolOption(const std::string &name,
+			       const std::string &help,
+			       bool value, bool obl)
+  {
+    ParData p;
+    p.bool_p=new bool(value);
+    p.self_delete=true;
+    p.help=help;
+    p.type=BOOL;
+    p.mandatory=obl;
+    _opts[name]=p;
+
+    value = false;
+
+    return *this;
+  }
+
+  ArgParser &ArgParser::stringOption(const std::string &name,
+			       const std::string &help,
+			       std::string value, bool obl)
+  {
+    ParData p;
+    p.string_p=new std::string(value);
+    p.self_delete=true;
+    p.help=help;
+    p.type=STRING;
+    p.mandatory=obl;
+    _opts[name]=p;
+    return *this;
+  }
+
+  ArgParser &ArgParser::refOption(const std::string &name,
+			       const std::string &help,
+			       int &ref, bool obl)
+  {
+    ParData p;
+    p.int_p=&ref;
+    p.self_delete=false;
+    p.help=help;
+    p.type=INTEGER;
+    p.mandatory=obl;
+    _opts[name]=p;
+    return *this;
+  }
+
+  ArgParser &ArgParser::refOption(const std::string &name,
+                                  const std::string &help,
+                                  double &ref, bool obl)
+  {
+    ParData p;
+    p.double_p=&ref;
+    p.self_delete=false;
+    p.help=help;
+    p.type=DOUBLE;
+    p.mandatory=obl;
+    _opts[name]=p;
+    return *this;
+  }
+
+  ArgParser &ArgParser::refOption(const std::string &name,
+                                  const std::string &help,
+                                  bool &ref, bool obl)
+  {
+    ParData p;
+    p.bool_p=&ref;
+    p.self_delete=false;
+    p.help=help;
+    p.type=BOOL;
+    p.mandatory=obl;
+    _opts[name]=p;
+
+    ref = false;
+
+    return *this;
+  }
+
+  ArgParser &ArgParser::refOption(const std::string &name,
+			       const std::string &help,



More information about the Lemon-commits mailing list