From b98da56a7c922e680433e5652a93f9c62e0c21af Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 15 Oct 2014 00:28:16 -0400 Subject: [PATCH] CLI - Implemented A class, to represent a single argument. Much like a tree node. --- src/CLI.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/CLI.h | 25 ++++++++++++ 2 files changed, 133 insertions(+) diff --git a/src/CLI.cpp b/src/CLI.cpp index 73c2deaaf..93f551eea 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,113 @@ static int minimumMatchLength = 3; // Alias expansion limit. Any more indicates some kind of error. static int safetyValveDefault = 10; +//////////////////////////////////////////////////////////////////////////////// +A::A (const std::string& name) +: _name (name) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +A::~A () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +A::A (const A& other) +: _name (other._name) +, _tags (other._tags) +, _attributes (other._attributes) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +A& A::operator= (const A& other) +{ + if (this != &other) + { + _name = other._name; + _tags = other._tags; + _attributes = other._attributes; + } + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +bool A::hasTag (const std::string& tag) const +{ + if (std::find (_tags.begin (), _tags.end (), tag) != _tags.end ()) + return true; + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +void A::tag (const std::string& tag) +{ + if (! hasTag (tag)) + _tags.push_back (tag); +} + +//////////////////////////////////////////////////////////////////////////////// +void A::unTag (const std::string& tag) +{ + std::vector ::iterator i; + for (i = _tags.begin (); i != _tags.end (); ++i) + { + if (*i == tag) + { + _tags.erase (i); + break; + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Accessor for attributes. +void A::attribute (const std::string& name, const std::string& value) +{ + _attributes[name] = value; +} + +//////////////////////////////////////////////////////////////////////////////// +// Accessor for attributes. +void A::attribute (const std::string& name, const int value) +{ + _attributes[name] = format (value); +} + +//////////////////////////////////////////////////////////////////////////////// +// Accessor for attributes. +void A::attribute (const std::string& name, const double value) +{ + _attributes[name] = format (value, 1, 8); +} + +//////////////////////////////////////////////////////////////////////////////// +// Accessor for attributes. +std::string A::attribute (const std::string& name) +{ + // Prevent autovivification. + std::map::iterator i = _attributes.find (name); + if (i != _attributes.end ()) + return i->second; + + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +void A::removeAttribute (const std::string& name) +{ + _attributes.erase (name); +} + +//////////////////////////////////////////////////////////////////////////////// +std::string A::dump () +{ + return "A::dump"; +} + //////////////////////////////////////////////////////////////////////////////// CLI::CLI () : _program ("") diff --git a/src/CLI.h b/src/CLI.h index 35828c7ee..e900fc189 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -30,6 +30,31 @@ #include #include +// Represents a single argument. +class A +{ +public: + A (const std::string&); + ~A (); + A (const A&); + A& operator= (const A&); + bool hasTag (const std::string&) const; + void tag (const std::string&); + void unTag (const std::string&); + void attribute (const std::string&, const std::string&); + void attribute (const std::string&, const int); + void attribute (const std::string&, const double); + std::string attribute (const std::string&); + void removeAttribute (const std::string&); + std::string dump (); + +public: + std::string _name; + std::vector _tags; + std::map _attributes; +}; + +// Represents the command line. class CLI { public: