LCOV - code coverage report
Current view: top level - advgetopt - option_info.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 2 100.0 %
Date: 2020-11-13 17:54:34 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2019  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  *    Doug Barbieri  doug@m2osw.com
      25             :  */
      26             : #pragma once
      27             : 
      28             : /** \file
      29             :  * \brief Declaration of the option_info class used to record available options.
      30             :  *
      31             :  * The library offers a way to verify your command line and other options
      32             :  * with features such as validators and reading of various types of
      33             :  * configuration files.
      34             :  *
      35             :  * The class defined in this file is used to describe an option.
      36             :  */
      37             : 
      38             : // advgetopt lib
      39             : //
      40             : #include    "advgetopt/flags.h"
      41             : #include    "advgetopt/validator.h"
      42             : 
      43             : 
      44             : // C++ lib
      45             : //
      46             : #include    <map>
      47             : #include    <memory>
      48             : 
      49             : 
      50             : 
      51             : namespace advgetopt
      52             : {
      53             : 
      54             : 
      55             : typedef char32_t            short_name_t;
      56             : 
      57             : constexpr short_name_t      NO_SHORT_NAME = U'\0';
      58             : 
      59             : 
      60             : short_name_t                string_to_short_name(std::string const & name);
      61             : std::string                 short_name_to_string(short_name_t short_name);
      62             : 
      63             : 
      64             : 
      65             : // the `option_info` can be used instead or on top of the `struct option`
      66             : // it is especially used to read an external getopt declaration file
      67             : //
      68        1132 : class option_info
      69             : {
      70             : public:
      71             :     typedef std::shared_ptr<option_info>            pointer_t;
      72             :     typedef std::vector<pointer_t>                  vector_t;
      73             :     typedef std::map<std::string, pointer_t>        map_by_name_t;
      74             :     typedef std::map<short_name_t, pointer_t>       map_by_short_name_t;
      75             : 
      76             :                                 option_info(std::string const & name, short_name_t short_name = NO_SHORT_NAME);
      77             : 
      78             :     std::string const &         get_name() const;
      79             :     void                        set_short_name(short_name_t short_name);
      80             :     short_name_t                get_short_name() const;
      81             :     std::string                 get_basename() const;
      82             :     std::string                 get_section_name() const;
      83             :     string_list_t               get_section_name_list() const;
      84             :     bool                        is_default_option() const;
      85             : 
      86             :     void                        set_flags(flag_t flags);
      87             :     void                        add_flag(flag_t flag);
      88             :     void                        remove_flag(flag_t flag);
      89             :     flag_t                      get_flags() const;
      90             :     bool                        has_flag(flag_t flag) const;
      91             : 
      92             :     bool                        has_default() const;
      93             :     void                        set_default(std::string const & default_value);
      94             :     void                        set_default(char const * default_value);
      95             :     void                        remove_default();
      96             :     std::string const &         get_default() const;
      97             : 
      98             :     void                        set_help(std::string const & help);
      99             :     void                        set_help(char const * help);
     100             :     std::string const &         get_help() const;
     101             : 
     102             :     bool                        set_validator(std::string const & name_and_params);
     103             :     bool                        set_validator(validator::pointer_t validator);
     104             :     bool                        set_validator(std::nullptr_t);
     105             :     validator::pointer_t        get_validator() const;
     106             : 
     107             :     void                        set_alias_destination(option_info::pointer_t destination);
     108             :     option_info::pointer_t      get_alias_destination() const;
     109             : 
     110             :     void                        set_multiple_separators(string_list_t const & separators);
     111             :     void                        set_multiple_separators(char const * const * separators);
     112             :     string_list_t const &       get_multiple_separators() const;
     113             : 
     114             :     bool                        has_value(std::string const & value) const;
     115             :     bool                        add_value(std::string const & value);
     116             :     bool                        set_value(int idx, std::string const & value);
     117             :     bool                        set_multiple_value(std::string const & value);
     118             :     bool                        is_defined() const;
     119             :     size_t                      size() const;
     120             :     std::string const &         get_value(int idx = 0) const;
     121             :     long                        get_long(int idx = 0) const;
     122             :     void                        lock(bool always = true);
     123             :     void                        unlock();
     124             :     void                        reset();
     125             : 
     126             : private:
     127             :     bool                        validate_all_values();
     128             :     bool                        validates(int idx = 0);
     129             : 
     130             :     // definitions
     131             :     //
     132             :     std::string                 f_name = std::string();
     133             :     short_name_t                f_short_name = NO_SHORT_NAME;
     134             :     flag_t                      f_flags = GETOPT_FLAG_NONE;
     135             :     std::string                 f_default_value = std::string();
     136             :     std::string                 f_help = std::string();
     137             :     validator::pointer_t        f_validator = validator::pointer_t();
     138             :     pointer_t                   f_alias_destination = pointer_t();
     139             :     string_list_t               f_multiple_separators = string_list_t();
     140             : 
     141             :     // value read from command line, environment, .conf file
     142             :     //
     143             :     string_list_t               f_value = string_list_t();
     144             :     mutable std::vector<long>   f_integer = std::vector<long>();
     145             : };
     146             : 
     147             : 
     148         159 : class option_info_ref
     149             : {
     150             : public:
     151             :                                 option_info_ref(option_info::pointer_t opt);
     152             : 
     153             :     bool                        empty() const;
     154             :     size_t                      length() const;
     155             :     size_t                      size() const;
     156             :     long                        get_long() const;
     157             : 
     158             :                                 operator std::string () const;
     159             : 
     160             :     option_info_ref &           operator = (char value);
     161             :     option_info_ref &           operator = (char32_t value);
     162             :     option_info_ref &           operator = (char const * value);
     163             :     option_info_ref &           operator = (std::string const & value);
     164             :     option_info_ref &           operator = (option_info_ref const & value);
     165             : 
     166             :     option_info_ref &           operator += (char value);
     167             :     option_info_ref &           operator += (char32_t value);
     168             :     option_info_ref &           operator += (char const * value);
     169             :     option_info_ref &           operator += (std::string const & value);
     170             :     option_info_ref &           operator += (option_info_ref const & value);
     171             : 
     172             :     std::string                 operator + (char value) const;
     173             :     std::string                 operator + (char32_t value) const;
     174             :     std::string                 operator + (char const * value) const;
     175             :     std::string                 operator + (std::string const & value) const;
     176             :     std::string                 operator + (option_info_ref const & value) const;
     177             : 
     178             :     friend std::string          operator + (char value, option_info_ref const & rhs);
     179             :     friend std::string          operator + (char32_t value, option_info_ref const & rhs);
     180             :     friend std::string          operator + (char const * value, option_info_ref const & rhs);
     181             :     friend std::string          operator + (std::string const & value, option_info_ref const & rhs);
     182             : 
     183             :                                 operator bool () const;
     184             :     bool                        operator ! () const;
     185             : 
     186             :     bool                        operator == (char const * value) const;
     187             :     bool                        operator == (std::string const & value) const;
     188             :     bool                        operator == (option_info_ref const & value) const;
     189             : 
     190             :     bool                        operator != (char const * value) const;
     191             :     bool                        operator != (std::string const & value) const;
     192             :     bool                        operator != (option_info_ref const & value) const;
     193             : 
     194             :     bool                        operator < (char const * value) const;
     195             :     bool                        operator < (std::string const & value) const;
     196             :     bool                        operator < (option_info_ref const & value) const;
     197             : 
     198             :     bool                        operator <= (char const * value) const;
     199             :     bool                        operator <= (std::string const & value) const;
     200             :     bool                        operator <= (option_info_ref const & value) const;
     201             : 
     202             :     bool                        operator > (char const * value) const;
     203             :     bool                        operator > (std::string const & value) const;
     204             :     bool                        operator > (option_info_ref const & value) const;
     205             : 
     206             :     bool                        operator >= (char const * value) const;
     207             :     bool                        operator >= (std::string const & value) const;
     208             :     bool                        operator >= (option_info_ref const & value) const;
     209             : 
     210             :     friend bool                 operator == (char const * value, option_info_ref const & rhs);
     211             :     friend bool                 operator == (std::string const & value, option_info_ref const & rhs);
     212             : 
     213             :     friend bool                 operator != (char const * value, option_info_ref const & rhs);
     214             :     friend bool                 operator != (std::string const & value, option_info_ref const & rhs);
     215             : 
     216             :     friend bool                 operator < (char const * value, option_info_ref const & rhs);
     217             :     friend bool                 operator < (std::string const & value, option_info_ref const & rhs);
     218             : 
     219             :     friend bool                 operator <= (char const * value, option_info_ref const & rhs);
     220             :     friend bool                 operator <= (std::string const & value, option_info_ref const & rhs);
     221             : 
     222             :     friend bool                 operator > (char const * value, option_info_ref const & rhs);
     223             :     friend bool                 operator > (std::string const & value, option_info_ref const & rhs);
     224             : 
     225             :     friend bool                 operator >= (char const * value, option_info_ref const & rhs);
     226             :     friend bool                 operator >= (std::string const & value, option_info_ref const & rhs);
     227             : 
     228             : private:
     229             :     option_info::pointer_t      f_opt = option_info::pointer_t();
     230             : };
     231             : 
     232             : 
     233             : }   // namespace advgetopt
     234             : 
     235             : 
     236             : std::string operator + (char32_t value, std::string const & rhs);
     237             : std::string operator + (std::string const & lhs, char32_t value);
     238             : 
     239             : 
     240             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13