[Lemon-commits] Alpar Juttner: Merge

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


details:   http://lemon.cs.elte.hu/hg/lemon/rev/e6452a49192c
changeset: 89:e6452a49192c
user:      Alpar Juttner <alpar [at] cs.elte.hu>
date:      Mon Mar 17 11:03:35 2008 +0000
description:
	Merge

diffstat:

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

diffs (truncated from 973 to 300 lines):

diff -r 8161012eaa61 -r e6452a49192c .hgignore
--- a/.hgignore	Sun Mar 16 07:32:43 2008 +0000
+++ b/.hgignore	Mon Mar 17 11:03:35 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 8161012eaa61 -r e6452a49192c demo/Makefile.am
--- a/demo/Makefile.am	Sun Mar 16 07:32:43 2008 +0000
+++ b/demo/Makefile.am	Mon Mar 17 11:03:35 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 8161012eaa61 -r e6452a49192c demo/arg_parser_demo.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/arg_parser_demo.cc	Mon Mar 17 11:03:35 2008 +0000
@@ -0,0 +1,82 @@
+/* -*- 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 the argument parser can be used.
+///
+/// \include arg_parser_demo.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","Choice A",g1)
+    .refOption("grb","Choice B",g2)
+    .refOption("grc","Choice 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;
+  if(ap.given("gra")) std::cout << "  -gra is given\n";
+  if(ap.given("grb")) std::cout << "  -grb is given\n";
+  if(ap.given("grc")) std::cout << "  -grc is given\n";
+                                     
+  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 8161012eaa61 -r e6452a49192c lemon/Makefile.am
--- a/lemon/Makefile.am	Sun Mar 16 07:32:43 2008 +0000
+++ b/lemon/Makefile.am	Mon Mar 17 11:03:35 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/concept_check.h \
         lemon/dim2.h \
 	lemon/error.h \
diff -r 8161012eaa61 -r e6452a49192c lemon/arg_parser.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/arg_parser.cc	Mon Mar 17 11:03:35 2008 +0000
@@ -0,0 +1,465 @@
+/* -*- 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;
+    _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;
+    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,
+			       std::string &ref, bool obl)



More information about the Lemon-commits mailing list