From 74ddd0381aa1b1a90eb0d5300fa576cb2348eeac Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 17 Oct 2021 10:10:18 -0700 Subject: basically functional, but still a work in progress --- arg_parser.h | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 arg_parser.h (limited to 'arg_parser.h') diff --git a/arg_parser.h b/arg_parser.h new file mode 100644 index 0000000..2ee69c6 --- /dev/null +++ b/arg_parser.h @@ -0,0 +1,95 @@ +#ifndef ARG_PARSER_H +#define ARG_PARSER_H + +#include +#include +#include +#include +#include +#include +#include // for basic parsing + // and for formatting error msgs +#include // for ::infinity() + +// TODO: use "some_sstream >> some_int" instead of Atoi +// since that allows more error checking + +#ifndef HAVE_INF +double const Inf = std::numeric_limits::infinity(); +#define HAVE_INF +#endif + +std::string tolower(const std::string& arg); + +// This is the interfact that ordinary users use; +// implementation in arg_parser.c is slightly tricky, +// but users never see that. +template T Atox(std::string const &foo); + +// The base class: +class baseGetter{ +public: + virtual std::string get()=0; + virtual int fail()=0; + virtual ~baseGetter(){ +//---- cerr << "dtor: baseGetter" << std::endl; + }; + baseGetter(){ +//---- cerr << "basic ctor: baseGetter" << std::endl; + } +}; + +// A Getter that reads words from the command line: +class cmdGetter : public baseGetter{ +public: + int argc; + char const * const * argv; + int wasgood; + +//ordinary constructor: + cmdGetter(int const _argc, char const * const * _argv); + virtual std::string get(); + virtual int fail(); +}; + +// A Getter that reads words from a file: +class fileGetter : public baseGetter{ +public: + std::fstream file; + +//ordinary constructor: + fileGetter(const std::string fname); + virtual std::string get(); + virtual int fail(); + void dummy() { + if (Inf); + } +}; + +class arg_parser{ +public: + std::list > getsome; + std::string cur; + int fold_case; + std::string keyword; + int narg; // number of args picked up using >> after this keyword + +// Constructor, for reading words from command line: + arg_parser(int const _argc, char const * const * _argv); +// Constructor, for reading words from file: + arg_parser(const std::string fname); + void push(const std::string fname); + int fail(); + template T nextArg(); + template T nextRaw(); + +// see if cur arg is a prefix of patt */ + int prefix(const std::string& patt); + template arg_parser& operator>>(T& val); + std::string xcase(const std::string& arg); +}; + +template struct myTraits {static const char* name;}; + +#define arg_parser_h +#endif -- cgit v1.2.3