Skip to content
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Normal usage

This is an example of simple usage.

::
.. sourcecode:: cpp

// include cmdline.h
#include "cmdline.h"
Expand All @@ -35,7 +35,7 @@ This is an example of simple usage.
// 1st argument is long name
// 2nd argument is short name (no short name if '\0' specified)
// 3rd argument is description
// 4th argument is mandatory (optional. default is false)
// 4th argument is mandatory (optional. default is true)
// 5th argument is default value (optional. it used when mandatory is false)
a.add<string>("host", 'h', "host name", true, "");

Expand Down Expand Up @@ -143,16 +143,16 @@ Rest of arguments are referenced by rest() method.
It returns vector of string.
Usualy, they are used to specify filenames, and so on.

::
.. sourcecode:: cpp

for (int i = 0; i < a.rest().size(); i++)
cout << a.rest()[i] << endl\;
cout << a.rest()[i] << endl;

- footer

footer() method is add a footer text of usage.

::
.. sourcecode:: cpp

...
a.footer("filename ...");
Expand Down
29 changes: 22 additions & 7 deletions cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@
#include <typeinfo>
#include <cstring>
#include <algorithm>
#include <cxxabi.h>
#include <cstdlib>
#if defined(_MSC_VER)
#define CMDLINE_DEMANGLE_WINDOWS
#include <windows.h>
#include <dbghelp.h>
#undef max
#pragma comment(lib, "dbghelp.lib")
#elif defined(__clang__) || defined(__GNUC__)
#include <cxxabi.h>
#endif

namespace cmdline{

Expand All @@ -51,7 +59,7 @@ class lexical_cast_t{
std::stringstream ss;
if (!(ss<<arg && ss>>ret && ss.eof()))
throw std::bad_cast();

return ret;
}
};
Expand All @@ -61,7 +69,7 @@ class lexical_cast_t<Target, Source, true>{
public:
static Target cast(const Source &arg){
return arg;
}
}
};

template <typename Source>
Expand Down Expand Up @@ -104,11 +112,18 @@ Target lexical_cast(const Source &arg)

static inline std::string demangle(const std::string &name)
{
#if defined(CMDLINE_DEMANGLE_WINDOWS)
TCHAR ret[256];
std::memset(ret, 0, 256);
::UnDecorateSymbolName(name.c_str(), ret, 256, 0);
return ret;
#else
int status=0;
char *p=abi::__cxa_demangle(name.c_str(), 0, 0, &status);
std::string ret(p);
free(p);
return ret;
#endif
}

template <class T>
Expand Down Expand Up @@ -533,10 +548,10 @@ class parser{
void parse_check(const std::vector<std::string> &args){
if (!options.count("help"))
add("help", '?', "print this message");
check(args.size(), parse(args));
check(static_cast<int>(args.size()), parse(args));
}

void parse_check(int argc, char *argv[]){
void parse_check(int argc, const char *argv[]){
if (!options.count("help"))
add("help", '?', "print this message");
check(argc, parse(argc, argv));
Expand All @@ -560,7 +575,7 @@ class parser{
if (ordered[i]->must())
oss<<ordered[i]->short_description()<<" ";
}

oss<<"[options] ... "<<ftr<<std::endl;
oss<<"options:"<<std::endl;

Expand Down Expand Up @@ -721,7 +736,7 @@ class parser{
actual=read(value);
has=true;
}
catch(const std::exception &e){
catch(const std::exception &){
return false;
}
return true;
Expand Down