From 940e1b410343f423a9bc41ca8da0c3859e2333fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Tue, 29 Mar 2016 08:30:06 +0200 Subject: Initial implementation --- .gitignore | 29 +----------------- example.cpp | 35 ++++++++++++++++++++++ getoptpp.hpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 28 deletions(-) create mode 100644 example.cpp create mode 100644 getoptpp.hpp diff --git a/.gitignore b/.gitignore index b8bd026..bc98fd3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,28 +1 @@ -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app +getoptpp.geany diff --git a/example.cpp b/example.cpp new file mode 100644 index 0000000..c99f3e7 --- /dev/null +++ b/example.cpp @@ -0,0 +1,35 @@ +#include + +#include "getoptpp.hpp" + +int main(int argc, char* argv[]) { + int verbose_flag; + + gopt::Options opt; + opt.add("verbose", no_argument, &verbose_flag, 1, [&]() { + std::cout << "verbose: " << verbose_flag << "\n"; + }); + opt.add("brief", no_argument, &verbose_flag, 0, [&]() { + std::cout << "brief: " << verbose_flag << "\n"; + }); + opt.add("add", no_argument, 'a', [&]() { + std::cout << "add\n"; + }); + opt.add("append", no_argument, 'b', [&]() { + std::cout << "append\n"; + }); + opt.add("delete", required_argument, 'd', [&]() { + std::cout << "delete: " << optarg << "\n"; + }); + opt.add("create", required_argument, 'c', [&]() { + std::cout << "create: " << optarg << "\n"; + }); + opt.add("file", required_argument, 'f', [&]() { + std::cout << "file: " << optarg << "\n"; + }); + opt.add("help", no_argument, '?', [&]() { + std::cout << "usage stuff\n"; + }); + + opt.process(argc, argv); +} diff --git a/getoptpp.hpp b/getoptpp.hpp new file mode 100644 index 0000000..4fe3c3a --- /dev/null +++ b/getoptpp.hpp @@ -0,0 +1,96 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace gopt { + +using Handle = std::function; + +class Options { + public: + Options(); + + void add(std::string const & name, int has_arg, int val, Handle handle); + void add(std::string const & name, int has_arg, int* flag, int val, Handle handle); + + void process(int argc, char* argv[]); + + private: + std::size_t num_flags; + std::vector