From 5f0849ef45459647661fabd402c2642146eb4d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Mon, 8 May 2017 12:00:27 +0200 Subject: Parse arguments without options --- .clang_complete | 1 + .gitignore | 1 + example.cpp | 8 ++++++-- getoptpp.hpp | 47 ++++++++++++++++++++++++++--------------------- 4 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 .clang_complete diff --git a/.clang_complete b/.clang_complete new file mode 100644 index 0000000..c24e3b5 --- /dev/null +++ b/.clang_complete @@ -0,0 +1 @@ +-std=c++11 diff --git a/.gitignore b/.gitignore index bc98fd3..93ed025 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ getoptpp.geany +example diff --git a/example.cpp b/example.cpp index f4ca943..73988aa 100644 --- a/example.cpp +++ b/example.cpp @@ -4,7 +4,7 @@ int main(int argc, char* argv[]) { int verbose_flag; - + dg::Options opt; opt.add("verbose", no_argument, &verbose_flag, 1, [&]() { std::cout << "verbose: " << verbose_flag << "\n"; @@ -30,6 +30,10 @@ int main(int argc, char* argv[]) { opt.add("help", no_argument, '?', [&]() { std::cout << "usage stuff\n"; }); - + opt.process(argc, argv); + for(auto const &arg : opt.arguments()) + { + std::cout << arg << std::endl; + } } diff --git a/getoptpp.hpp b/getoptpp.hpp index ccefe9a..0eafe3f 100644 --- a/getoptpp.hpp +++ b/getoptpp.hpp @@ -1,8 +1,10 @@ #pragma once +#include #include #include #include #include +#include #include namespace dg { @@ -12,25 +14,27 @@ using Handle = std::function; class Options { public: Options(); - + /// @param name name of the option /// @param has_arg kind of arguments that are used (no_argument, required_argument, optional_argument) /// @param val identifies the option (see getopt documentation) /// @param handle lambda that is invoked when the option occures void add(std::string const & name, int has_arg, int val, Handle handle); - + /// @param name name of the option /// @param has_arg kind of arguments that are used (no_argument, required_argument, optional_argument) /// @param flag pointer that is set after the option occured /// @param val value for the flag to be set /// @param handle lambda that is invoked when the option occures void add(std::string const & name, int has_arg, int* flag, int val, Handle handle); - - void process(int argc, char* argv[]); - + + bool process(int argc, char* argv[]); + const std::vector arguments() const { return args; } + private: std::size_t num_flags; std::vector