LCOV - code coverage report
Current view: top level - advgetopt - flags.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 6 100.0 %
Date: 2019-09-16 03:06:47 Functions: 4 4 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 Definitions of the options class a initialization functions.
      30             :  *
      31             :  * The advgetopt library offers an advanced way to manage your command
      32             :  * line tools options on the command line, in environment variables, and
      33             :  * in configuration files.
      34             :  */
      35             : 
      36             : 
      37             : // C++ lib
      38             : //
      39             : #include    <cstdint>
      40             : 
      41             : 
      42             : 
      43             : namespace advgetopt
      44             : {
      45             : 
      46             : 
      47             : 
      48             : typedef std::uint32_t       flag_t;
      49             : 
      50             : static constexpr flag_t     GETOPT_FLAG_NONE                 = static_cast<flag_t>(0x00000000);
      51             : 
      52             : static constexpr flag_t     GETOPT_FLAG_COMMAND_LINE         = static_cast<flag_t>(0x00000001);  // acceptable on the command line
      53             : static constexpr flag_t     GETOPT_FLAG_ENVIRONMENT_VARIABLE = static_cast<flag_t>(0x00000002);  // acceptable in environment variable
      54             : static constexpr flag_t     GETOPT_FLAG_CONFIGURATION_FILE   = static_cast<flag_t>(0x00000004);  // acceptable in configuration files
      55             : 
      56             : static constexpr flag_t     GETOPT_FLAG_ALIAS                = static_cast<flag_t>(0x00000010);  // alias, result in another option defined in "help" string
      57             : static constexpr flag_t     GETOPT_FLAG_FLAG                 = static_cast<flag_t>(0x00000020);  // no parameter allowed (--help)
      58             : static constexpr flag_t     GETOPT_FLAG_REQUIRED             = static_cast<flag_t>(0x00000040);  // required (--host 127.0.0.1)
      59             : static constexpr flag_t     GETOPT_FLAG_MULTIPLE             = static_cast<flag_t>(0x00000080);  // any number of parameter is allowed (--files a b c d ...)
      60             : static constexpr flag_t     GETOPT_FLAG_DEFAULT_OPTION       = static_cast<flag_t>(0x00000100);  // where entries go by default (a.k.a. after "--")
      61             : static constexpr flag_t     GETOPT_FLAG_HAS_DEFAULT          = static_cast<flag_t>(0x00000200);  // default value is defined
      62             : 
      63             : static constexpr flag_t     GETOPT_FLAG_SHOW_MOST            = static_cast<flag_t>(0x00000000);  // show in usage() when not in GROUP1 or GROUP2
      64             : static constexpr flag_t     GETOPT_FLAG_SHOW_USAGE_ON_ERROR  = static_cast<flag_t>(0x00001000);  // show in usage() when an error occurs
      65             : static constexpr flag_t     GETOPT_FLAG_SHOW_ALL             = static_cast<flag_t>(0x00002000);  // show in usage() when --long-help is used
      66             : static constexpr flag_t     GETOPT_FLAG_SHOW_GROUP1          = static_cast<flag_t>(0x00004000);  // show in usage() when --<group1>-help is used (app dependent)
      67             : static constexpr flag_t     GETOPT_FLAG_SHOW_GROUP2          = static_cast<flag_t>(0x00008000);  // show in usage() when --<group2>-help is used (app dependent)
      68             : 
      69             : static constexpr flag_t     GETOPT_FLAG_GROUP_MASK           = static_cast<flag_t>(0x00700000);
      70             : static constexpr flag_t     GETOPT_FLAG_GROUP_MINIMUM        = static_cast<flag_t>(0);
      71             : static constexpr flag_t     GETOPT_FLAG_GROUP_MAXIMUM        = static_cast<flag_t>(7);
      72             : static constexpr flag_t     GETOPT_FLAG_GROUP_SHIFT          = static_cast<flag_t>(20);
      73             : static constexpr flag_t     GETOPT_FLAG_GROUP_NONE           = static_cast<flag_t>(0x00000000);  // not in a group
      74             : static constexpr flag_t     GETOPT_FLAG_GROUP_COMMANDS       = static_cast<flag_t>(0x00100000);  // in command group (group 1)
      75             : static constexpr flag_t     GETOPT_FLAG_GROUP_OPTIONS        = static_cast<flag_t>(0x00200000);  // in option group (group 2)
      76             : static constexpr flag_t     GETOPT_FLAG_GROUP_THREE          = static_cast<flag_t>(0x00300000);  // in group 3
      77             : static constexpr flag_t     GETOPT_FLAG_GROUP_FOUR           = static_cast<flag_t>(0x00400000);  // in group 4
      78             : static constexpr flag_t     GETOPT_FLAG_GROUP_FIVE           = static_cast<flag_t>(0x00500000);  // in group 5
      79             : static constexpr flag_t     GETOPT_FLAG_GROUP_SIX            = static_cast<flag_t>(0x00600000);  // in group 6
      80             : static constexpr flag_t     GETOPT_FLAG_GROUP_SEVEN          = static_cast<flag_t>(0x00700000);  // in group 7
      81             : 
      82             : static constexpr flag_t     GETOPT_FLAG_DYNAMIC              = static_cast<flag_t>(0x20000000);  // this value was found in a configuration file and dynamic parameters are allowed (i.e. no definition for this option was found)
      83             : static constexpr flag_t     GETOPT_FLAG_LOCK                 = static_cast<flag_t>(0x40000000);  // this value is currently locked (can't be modified)
      84             : 
      85             : static constexpr flag_t     GETOPT_FLAG_END                  = static_cast<flag_t>(0x80000000);  // mark the end of the list
      86             : 
      87             : 
      88             : 
      89             : template<class none = void>
      90             : constexpr flag_t option_flags_merge()
      91             : {
      92             :     return GETOPT_FLAG_NONE;
      93             : }
      94             : 
      95             : 
      96             : template<flag_t flag, flag_t ...args>
      97             : constexpr flag_t option_flags_merge()
      98             : {
      99             :     static_assert((flag & GETOPT_FLAG_GROUP_MASK) == 0
     100             :                || (option_flags_merge<args...>() & GETOPT_FLAG_GROUP_MASK) == 0
     101             :                , "more than one GETOPT_FLAG_GROUP_... is not allowed within one set of flags.");
     102             : 
     103             :     return flag | option_flags_merge<args...>();
     104             : }
     105             : 
     106             : 
     107             : template<flag_t flag, flag_t ...args>
     108             : constexpr flag_t combine_option_flags()
     109             : {
     110             :     constexpr flag_t result(option_flags_merge<flag, args...>());
     111             : 
     112             :     static_assert(static_cast<int>((result & GETOPT_FLAG_FLAG) != 0)
     113             :                 + static_cast<int>((result & (GETOPT_FLAG_REQUIRED | GETOPT_FLAG_MULTIPLE | GETOPT_FLAG_DEFAULT_OPTION)) != 0)
     114             :                 + static_cast<int>((result & GETOPT_FLAG_END) != 0)
     115             :                         <= 1
     116             :                 , "flag GETOPT_FLAG_FLAG is not compatible with any of GETOPT_FLAG_REQUIRED | GETOPT_FLAG_MULTIPLE | GETOPT_FLAG_DEFAULT_OPTION or none of these flags were specified.");
     117             : 
     118             :     static_assert(((result & (GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE)) != 0)
     119             :                 ^ ((result & GETOPT_FLAG_END) != 0)
     120             :                 , "flags must include at least one of GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE or be set to GETOPT_FLAG_END");
     121             : 
     122             :     return result;
     123             : }
     124             : 
     125             : 
     126             : constexpr flag_t end_flags()
     127             : {
     128             :     return combine_option_flags<GETOPT_FLAG_END>();
     129             : }
     130             : 
     131             : 
     132             : template<flag_t ...args>
     133             : constexpr flag_t any_flags()
     134             : {
     135             :     constexpr flag_t result(combine_option_flags<args...>());
     136             : 
     137             :     static_assert((result & GETOPT_FLAG_END) == 0
     138             :                 , "an option_flag() cannot include GETOPT_FLAG_END");
     139             : 
     140             :     return result;
     141             : }
     142             : 
     143             : 
     144             : template<flag_t ...args>
     145             : constexpr flag_t option_flags()
     146             : {
     147             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_FLAG, args...>());
     148             : 
     149             :     //static_assert((result & (GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE)) == 0
     150             :     //            , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
     151             : 
     152             :     return result;
     153             : }
     154             : 
     155             : 
     156             : template<flag_t ...args>
     157           5 : constexpr flag_t all_flags()
     158             : {
     159             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_COMMAND_LINE
     160             :                                                , GETOPT_FLAG_ENVIRONMENT_VARIABLE
     161             :                                                , GETOPT_FLAG_CONFIGURATION_FILE
     162           5 :                                                , args...>());
     163             : 
     164           5 :     return result;
     165             : }
     166             : 
     167             : 
     168             : template<flag_t ...args>
     169             : constexpr flag_t standalone_all_flags()
     170             : {
     171             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_COMMAND_LINE
     172             :                                                , GETOPT_FLAG_ENVIRONMENT_VARIABLE
     173             :                                                , GETOPT_FLAG_CONFIGURATION_FILE
     174             :                                                , GETOPT_FLAG_FLAG
     175             :                                                , args...>());
     176             : 
     177             :     return result;
     178             : }
     179             : 
     180             : 
     181             : template<flag_t ...args>
     182             : constexpr flag_t standalone_command_flags()
     183             : {
     184             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_COMMAND_LINE, GETOPT_FLAG_FLAG, args...>());
     185             : 
     186             :     static_assert((result & (GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE)) == 0
     187             :                 , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
     188             : 
     189             :     return result;
     190             : }
     191             : 
     192             : 
     193             : template<flag_t ...args>
     194          13 : constexpr flag_t command_flags()
     195             : {
     196          13 :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_COMMAND_LINE, args...>());
     197             : 
     198             :     static_assert((result & (GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE)) == 0
     199             :                 , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
     200             : 
     201          13 :     return result;
     202             : }
     203             : 
     204             : 
     205             : template<flag_t ...args>
     206             : constexpr flag_t var_flags()
     207             : {
     208             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_ENVIRONMENT_VARIABLE, args...>());
     209             : 
     210             :     static_assert((result & (GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_CONFIGURATION_FILE)) == 0
     211             :                 , "a config_flag() cannot include GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_CONFIGURATION_FILE");
     212             : 
     213             :     return result;
     214             : }
     215             : 
     216             : 
     217             : template<flag_t ...args>
     218             : constexpr flag_t config_flags()
     219             : {
     220             :     constexpr flag_t result(combine_option_flags<GETOPT_FLAG_CONFIGURATION_FILE, GETOPT_FLAG_FLAG, args...>());
     221             : 
     222             :     static_assert((result & (GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE)) == 0
     223             :                 , "a config_flag() cannot include GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE");
     224             : 
     225             :     return result;
     226             : }
     227             : 
     228             : 
     229             : 
     230             : 
     231             : 
     232             : 
     233             : }   // namespace advgetopt
     234             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12