LCOV - code coverage report
Current view: top level - advgetopt - advgetopt.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 1 100.0 %
Date: 2022-03-01 20:39:45 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/advgetopt
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : #pragma once
      20             : 
      21             : /** \file
      22             :  * \brief Definitions of the advanced getopt class.
      23             :  *
      24             :  * The advgetopt library offers an advanced way to manage your command
      25             :  * line tools options on the command line, in environment variables, and
      26             :  * in configuration files.
      27             :  */
      28             : 
      29             : // advgetopt lib
      30             : //
      31             : #include    "advgetopt/option_info.h"
      32             : #include    "advgetopt/options.h"
      33             : #include    "advgetopt/validator.h"
      34             : #include    "advgetopt/variables.h"
      35             : 
      36             : 
      37             : // C++ lib
      38             : //
      39             : #include    <limits>
      40             : #include    <map>
      41             : #include    <memory>
      42             : #include    <ostream>
      43             : #include    <vector>
      44             : 
      45             : 
      46             : 
      47             : namespace advgetopt
      48             : {
      49             : 
      50             : 
      51             : constexpr char const        CONFIGURATION_SECTIONS[] = "configuration_sections";
      52             : 
      53             : 
      54             : constexpr flag_t            SYSTEM_OPTION_NONE                          = 0x0000;
      55             : 
      56             : // system commands
      57             : constexpr flag_t            SYSTEM_OPTION_HELP                          = 0x0001;
      58             : constexpr flag_t            SYSTEM_OPTION_VERSION                       = 0x0002;
      59             : constexpr flag_t            SYSTEM_OPTION_COPYRIGHT                     = 0x0004;
      60             : constexpr flag_t            SYSTEM_OPTION_LICENSE                       = 0x0008;
      61             : constexpr flag_t            SYSTEM_OPTION_BUILD_DATE                    = 0x0010;
      62             : constexpr flag_t            SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME     = 0x0020;
      63             : constexpr flag_t            SYSTEM_OPTION_CONFIGURATION_FILENAMES       = 0x0040;
      64             : constexpr flag_t            SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS    = 0x0080;
      65             : constexpr flag_t            SYSTEM_OPTION_SHOW_OPTION_SOURCES           = 0x0100;
      66             : 
      67             : // system options
      68             : constexpr flag_t            SYSTEM_OPTION_CONFIG_DIR                    = 0x1000;
      69             : 
      70             : constexpr flag_t            SYSTEM_OPTION_COMMANDS_MASK                 = 0x0FFF;
      71             : constexpr flag_t            SYSTEM_OPTION_OPTIONS_MASK                  = 0xF000;
      72             : 
      73             : 
      74             : 
      75         319 : class getopt
      76             : {
      77             : public:
      78             :     typedef std::shared_ptr<getopt>     pointer_t;
      79             : 
      80             :                             getopt(options_environment const & opts);
      81             :                             getopt(options_environment const & opts
      82             :                                  , int argc
      83             :                                  , char * argv[]);
      84             : 
      85             :     options_environment const &
      86             :                             get_options_environment() const;
      87             :     bool                    has_flag(flag_t flag) const;
      88             :     void                    reset();
      89             : 
      90             :     void                    parse_options_info(
      91             :                                       option const * opts
      92             :                                     , bool ignore_duplicates = false);
      93             :     void                    parse_options_from_file(
      94             :                                       std::string const & filename
      95             :                                     , int min_section
      96             :                                     , int max_section);
      97             :     void                    add_option(option_info::pointer_t opt, bool ignore_duplicates = false);
      98             :     void                    link_aliases();
      99             :     void                    set_short_name(
     100             :                                       std::string const & name
     101             :                                     , short_name_t short_name);
     102             : 
     103             :     void                    finish_parsing(int argc, char * argv[]);
     104             : 
     105             :     void                    parse_program_name(char * argv[]);
     106             : 
     107             :     void                    parse_configuration_files(
     108             :                                       int argc = 0
     109             :                                     , char * argv[] = nullptr);
     110             :     void                    process_configuration_file(std::string const & filename);
     111             : 
     112             :     std::string             get_environment_variable() const;
     113             :     static string_list_t    split_environment(std::string const & environment);
     114             :     void                    parse_environment_variable();
     115             : 
     116             :     void                    parse_string(
     117             :                                       std::string const & str
     118             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     119             :                                     , bool only_environment_variable = false);
     120             :     void                    parse_arguments(
     121             :                                       int argc
     122             :                                     , char * argv[]
     123             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     124             :                                     , bool only_environment_variable = false);
     125             : 
     126             :     flag_t                  process_system_options(std::basic_ostream<char> & out);
     127             : 
     128             :     option_info::map_by_name_t const &
     129             :                             get_options() const;
     130             :     option_info::pointer_t  get_option(std::string const & name, bool exact_option = false) const;
     131             :     option_info::pointer_t  get_option(short_name_t name, bool exact_option = false) const;
     132             :     bool                    is_defined(std::string const & name) const;
     133             :     std::size_t             size(std::string const & name) const;
     134             :     bool                    has_default(std::string const & name) const;
     135             :     std::string             get_default(std::string const & name) const;
     136             :     long                    get_long(
     137             :                                       std::string const & name
     138             :                                     , int idx = 0
     139             :                                     , long min = std::numeric_limits<long>::min()
     140             :                                     , long max = std::numeric_limits<long>::max()) const;
     141             :     double                  get_double(
     142             :                                       std::string const & name
     143             :                                     , int idx = 0
     144             :                                     , double min = std::numeric_limits<double>::min()
     145             :                                     , double max = std::numeric_limits<double>::max()) const;
     146             :     std::string             get_string(
     147             :                                       std::string const & name
     148             :                                     , int idx = 0
     149             :                                     , bool raw = false) const;
     150             :     std::string             operator [] (std::string const & name) const;
     151             :     option_info_ref         operator [] (std::string const & name);
     152             : 
     153             :     std::string             get_program_name() const;
     154             :     std::string             get_program_fullname() const;
     155             :     std::string             get_project_name() const;
     156             :     std::string             get_group_name() const;
     157             :     std::string             get_environment_variable_name() const;
     158             :     size_t                  get_configuration_filename_size() const;
     159             :     std::string             get_configuration_filename(int idx) const;
     160             :     string_list_t           get_configuration_filenames(
     161             :                                       bool exists
     162             :                                     , bool writable
     163             :                                     , int argc = 0
     164             :                                     , char * argv[] = nullptr) const;
     165             : 
     166             :     group_description const *
     167             :                             find_group(flag_t group) const;
     168             :     std::string             usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
     169             :     std::string             process_help_string(char const * help) const;
     170             :     static std::string      breakup_line(
     171             :                                       std::string line
     172             :                                     , size_t const option_width
     173             :                                     , size_t const line_width);
     174             :     static std::string      format_usage_string(
     175             :                                       std::string const & argument
     176             :                                     , std::string const & help
     177             :                                     , size_t const option_width
     178             :                                     , size_t const line_width);
     179             :     static size_t           get_line_width();
     180             :     static std::string      sanitizer_details();
     181             : 
     182             :     variables::pointer_t    get_variables() const;
     183             : 
     184             : private:
     185             :     void                    initialize_parser(options_environment const & opt_env);
     186             :     void                    parse_options_from_group_names();
     187             :     void                    parse_options_from_file();
     188             :     void                    show_option_sources(std::basic_ostream<char> & out);
     189             :     option_info::pointer_t  get_alias_destination(option_info::pointer_t opt) const;
     190             :     void                    define_environment_variable_data();
     191             :     void                    is_parsed() const;
     192             :     static string_list_t    find_config_dir(int argc, char * argv[]);
     193             : 
     194             :     void                    add_options(
     195             :                                       option_info::pointer_t opt
     196             :                                     , int & i
     197             :                                     , int argc
     198             :                                     , char ** argv
     199             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     200             :     void                    add_option_from_string(
     201             :                                       option_info::pointer_t opt
     202             :                                     , std::string const & value
     203             :                                     , std::string const & filename
     204             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     205             : 
     206             :     std::string                         f_program_fullname = std::string();
     207             :     std::string                         f_program_name = std::string();
     208             : 
     209             :     options_environment                 f_options_environment = options_environment();
     210             :     option_info::map_by_name_t          f_options_by_name = option_info::map_by_name_t();
     211             :     option_info::map_by_short_name_t    f_options_by_short_name = option_info::map_by_short_name_t();
     212             :     option_info::pointer_t              f_default_option = option_info::pointer_t();
     213             :     std::string                         f_environment_variable = std::string();
     214             :     variables::pointer_t                f_variables = variables::pointer_t();
     215             :     bool                                f_parsed = false;
     216             : };
     217             : 
     218             : 
     219             : 
     220             : 
     221             : }   // namespace advgetopt
     222             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13