X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/df2bb45e41303b5132feb6b264caaa01c31b8bb5..16e9905f82947dd471c09382122d8150ba6fda1a:/projects/sgconf/options.h?ds=sidebyside

diff --git a/projects/sgconf/options.h b/projects/sgconf/options.h
index 696255ff..f2f0a0eb 100644
--- a/projects/sgconf/options.h
+++ b/projects/sgconf/options.h
@@ -18,12 +18,13 @@
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
-#ifndef __STG_SGCONF_OPTIONS_H__
-#define __STG_SGCONF_OPTIONS_H__
+#pragma once
 
 #include <string>
 #include <vector>
+#include <utility>
 #include <stdexcept>
+#include <memory>
 #include <cstddef> // size_t
 
 namespace SGCONF
@@ -35,55 +36,83 @@ struct PARSER_STATE;
 class OPTION
 {
     public:
-        OPTION(const std::string & shortName,
-               const std::string & longName,
-               ACTION * action,
-               const std::string & description);
-        OPTION(const std::string & longName,
-               ACTION * action,
-               const std::string & description);
-        OPTION(const OPTION & rhs);
-        ~OPTION();
-
-        OPTION & operator=(const OPTION & rhs);
+        OPTION(const std::string& shortName,
+               const std::string& longName,
+               std::unique_ptr<ACTION> action,
+               const std::string& description);
+        OPTION(const std::string& longName,
+               std::unique_ptr<ACTION> action,
+               const std::string& description);
+
+        OPTION(OPTION&& rhs) = default;
+        OPTION& operator=(OPTION&& rhs) = default;
 
         void Help(size_t level = 0) const;
-        PARSER_STATE Parse(int argc, char ** argv);
-        bool Check(const char * arg) const;
+        PARSER_STATE Parse(int argc, char** argv, void* data);
+        void ParseValue(const std::string & value);
+        bool Check(const char* arg) const;
+        const std::string& Name() const { return m_longName; }
 
-        class ERROR : public std::runtime_error
+        struct ERROR : std::runtime_error
         {
-            public:
-                ERROR(const std::string & message)
-                    : std::runtime_error(message.c_str()) {}
+            explicit ERROR(const std::string & message) noexcept
+                : std::runtime_error(message.c_str()) {}
         };
 
     private:
         std::string m_shortName;
         std::string m_longName;
-        ACTION * m_action;
+        std::unique_ptr<ACTION> m_action;
         std::string m_description;
 };
 
 class OPTION_BLOCK
 {
     public:
-        void Add(const std::string & shortName,
-                 const std::string & longName,
-                 ACTION * action,
-                 const std::string & description);
-        void Add(const std::string & longName,
-                 ACTION * action,
-                 const std::string & description);
+        OPTION_BLOCK() {}
+        explicit OPTION_BLOCK(const std::string& description)
+            : m_description(description) {}
+
+        OPTION_BLOCK(OPTION_BLOCK&&) = default;
+        OPTION_BLOCK& operator=(OPTION_BLOCK&&) = default;
+
+        OPTION_BLOCK& Add(const std::string& shortName,
+                          const std::string& longName,
+                          std::unique_ptr<ACTION> action,
+                          const std::string& description);
+        OPTION_BLOCK& Add(const std::string& longName,
+                          std::unique_ptr<ACTION> action,
+                          const std::string& description);
 
         void Help(size_t level) const;
 
-        PARSER_STATE Parse(int argc, char ** argv);
+        PARSER_STATE Parse(int argc, char** argv, void* data = NULL);
+        void ParseFile(const std::string& filePath);
+
+        struct ERROR : std::runtime_error
+        {
+            explicit ERROR(const std::string & message) noexcept
+                : std::runtime_error(message.c_str()) {}
+        };
 
     private:
         std::vector<OPTION> m_options;
+        std::string m_description;
+
+        void OptionCallback(const std::string& key, const std::string& value);
 };
 
-} // namespace SGCONF
+class OPTION_BLOCKS
+{
+    public:
+        OPTION_BLOCK& Add(const std::string& description)
+        { m_blocks.push_back(OPTION_BLOCK(description)); return m_blocks.back(); }
+        void Add(OPTION_BLOCK&& block) { m_blocks.push_back(std::move(block)); }
+        void Help(size_t level) const;
+        PARSER_STATE Parse(int argc, char** argv);
 
-#endif
+    private:
+        std::vector<OPTION_BLOCK> m_blocks;
+};
+
+} // namespace SGCONF