Commit 49c8cef2 authored by fabianw's avatar fabianw

Merge branch 'regex' into MeshGridFix

parents 3ca5e6e0 27540998
......@@ -61,6 +61,8 @@ private:
char** vArgV;
bool bStrictMode, bVerbose;
bool _isnumber(const std::string& s) const;
protected:
std::map<std::string,Value> mapArguments;
......
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <limits>
//#include <regex> // C++11
#include "Cubism/ArgumentParser.h"
......@@ -100,6 +101,13 @@ bool CommandlineParser::check(std::string key) const
return _existKey(key,mapArguments);
}
bool CommandlineParser::_isnumber(const std::string& s) const
{
char* end = NULL;
strtod(s.c_str(), &end);
return end != s.c_str(); // only care if the number is numeric or not. This includes nan and inf
}
CommandlineParser::CommandlineParser(const int argc, char **argv)
: iArgC(argc), vArgV(argv), bStrictMode(false), bVerbose(true)
{
......@@ -117,11 +125,12 @@ CommandlineParser::CommandlineParser(const int argc, char **argv)
for (int j=i+1; j<argc; j++)
{
// if the current value is numeric and (possibly) negative,
// do not interpret it as a key
const bool leadingDash = (argv[j][0] == '-');
const char c = argv[j][1];
const bool isNumeric = ((c>='0' && c<='9') || c==0)? true:false;
// const bool isNumeric = std::regex_match(argv[j], std::regex("(\\+|-)?[0-9]*(\\.[0-9]*)?((e|E)(\\+|-)?[0-9]*)?"));
// do not interpret it as a key.
// XXX: [fabianw@mavt.ethz.ch; 2019-03-28] WARNING:
// This will treat -nan as a NUMBER and not as a KEY
std::string sval(argv[j]);
const bool leadingDash = (sval[0] == '-');
const bool isNumeric = _isnumber(sval);
if (leadingDash && !isNumeric)
break;
else
......
SHELL := /bin/bash
CC = g++
extra ?=
CFLAGS = -g -O2 -I../../include
CFLAGS+= $(extra)
CFLAGS+= -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align
CFLAGS+= -Wwrite-strings -Wmissing-declarations -Wredundant-decls
CFLAGS+= -Wshadow -Woverloaded-virtual -Wuninitialized
CFLAGS+= -Wpedantic -Wno-unused-parameter # does not make much sense with function overloading
.PHONY: all clean
all: testArgumentParser.cpp
$(CC) $(CFLAGS) -o testArgumentParser testArgumentParser.cpp ../../src/ArgumentParser.cpp
clean:
rm -f testArgumentParser *.o
#!/usr/bin/env bash
# File : run.sh
# Created : Thu Mar 28 2019 06:11:33 PM (+0100)
# Author : Fabian Wermelinger
# Description: Run testArgumentParser
# Copyright 2019 ETH Zurich. All Rights Reserved.
set -e
./testArgumentParser -conf test.conf \
-arg1 1.0e-001 \
-arg2 -1.0e-001 \
-arg3 1.0e1 \
-arg4 -1.0e+001 \
-arg5 -1.0e400 \
-arg6 +1.0e400 \
-arg7 1.0e400 \
-arg8 nan \
-arg9 -nan \
-arg10
# File : test.conf
# Created : Thu Mar 28 2019 05:48:19 PM (+0100)
# Author : Fabian Wermelinger
# Description: Test configuration file
# ./testArgumentParser -conf test.conf
# Copyright 2019 ETH Zurich. All Rights Reserved.
# strings
s1 string
+s1 concatenate more strings here
-s2 leading dashes are ok here
# floats
f1 1.0e-001
f2 -1.0e-001
f3 1.0e1
f4 -1.0e+001
f5 -1.0e400
f6 +1.0e400
f7 1.0e400
f8 nan
f9 -nan
// File : testArgumentParser.cpp
// Created : Thu Mar 28 2019 05:27:31 PM (+0100)
// Author : Fabian Wermelinger
// Description: Test Cubism ArgumentParser
// Copyright 2019 ETH Zurich. All Rights Reserved.
#include <iostream>
#include "Cubism/ArgumentParser.h"
using namespace cubism;
using namespace std;
int main(int argc, char* argv[])
{
ArgumentParser parser(argc, argv);
if (parser.exist("-conf"))
parser.readFile(parser("-conf").asString());
parser.print_args();
cout << "s1 = " << parser("s1").asString() << endl;
cout << "s2 = " << parser("s2").asString() << endl;
cout << "f1 = " << parser("f1").asDouble() << endl;
cout << "f2 = " << parser("f2").asDouble() << endl;
cout << "f3 = " << parser("f3").asDouble() << endl;
cout << "f4 = " << parser("f4").asDouble() << endl;
cout << "f5 = " << parser("f5").asDouble() << endl;
cout << "f6 = " << parser("f6").asDouble() << endl;
cout << "f7 = " << parser("f7").asDouble() << endl;
cout << "f8 = " << parser("f8").asDouble() << endl;
cout << "f9 = " << parser("f9").asDouble() << endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment