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: 2021-09-08 17:05:25 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2021  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             : 
      35             : 
      36             : // C++ lib
      37             : //
      38             : #include    <limits>
      39             : #include    <map>
      40             : #include    <memory>
      41             : #include    <ostream>
      42             : #include    <vector>
      43             : 
      44             : 
      45             : 
      46             : namespace advgetopt
      47             : {
      48             : 
      49             : 
      50             : constexpr char const        CONFIGURATION_SECTIONS[] = "configuration_sections";
      51             : 
      52             : 
      53             : constexpr flag_t            SYSTEM_OPTION_NONE                          = 0x0000;
      54             : 
      55             : constexpr flag_t            SYSTEM_OPTION_HELP                          = 0x0001;
      56             : constexpr flag_t            SYSTEM_OPTION_VERSION                       = 0x0002;
      57             : constexpr flag_t            SYSTEM_OPTION_COPYRIGHT                     = 0x0004;
      58             : constexpr flag_t            SYSTEM_OPTION_LICENSE                       = 0x0008;
      59             : constexpr flag_t            SYSTEM_OPTION_BUILD_DATE                    = 0x0010;
      60             : constexpr flag_t            SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME     = 0x0020;
      61             : constexpr flag_t            SYSTEM_OPTION_CONFIGURATION_FILENAMES       = 0x0040;
      62             : constexpr flag_t            SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS    = 0x0080;
      63             : constexpr flag_t            SYSTEM_OPTION_SHOW_OPTION_SOURCES           = 0x0100;
      64             : 
      65             : constexpr flag_t            SYSTEM_OPTION_CONFIG_DIR                    = 0x1000;   // option
      66             : 
      67             : constexpr flag_t            SYSTEM_OPTION_COMMANDS_MASK                 = 0x0FFF;
      68             : constexpr flag_t            SYSTEM_OPTION_OPTIONS_MASK                  = 0xF000;
      69             : 
      70             : 
      71             : 
      72         319 : class getopt
      73             : {
      74             : public:
      75             :     typedef std::shared_ptr<getopt>     pointer_t;
      76             : 
      77             :                             getopt(options_environment const & opts);
      78             :                             getopt(options_environment const & opts
      79             :                                  , int argc
      80             :                                  , char * argv[]);
      81             : 
      82             :     options_environment const &
      83             :                             get_options_environment() const;
      84             :     bool                    has_flag(flag_t flag) const;
      85             :     void                    reset();
      86             : 
      87             :     void                    parse_options_info(
      88             :                                       option const * opts
      89             :                                     , bool ignore_duplicates = false);
      90             :     void                    parse_options_from_file(
      91             :                                       std::string const & filename
      92             :                                     , int min_section
      93             :                                     , int max_section);
      94             :     void                    add_option(option_info::pointer_t opt, bool ignore_duplicates = false);
      95             :     void                    link_aliases();
      96             :     void                    set_short_name(
      97             :                                       std::string const & name
      98             :                                     , short_name_t short_name);
      99             : 
     100             :     void                    finish_parsing(int argc, char * argv[]);
     101             : 
     102             :     void                    parse_program_name(char * argv[]);
     103             : 
     104             :     void                    parse_configuration_files(
     105             :                                       int argc = 0
     106             :                                     , char * argv[] = nullptr);
     107             :     void                    process_configuration_file(std::string const & filename);
     108             : 
     109             :     std::string             get_environment_variable() const;
     110             :     static string_list_t    split_environment(std::string const & environment);
     111             :     void                    parse_environment_variable();
     112             : 
     113             :     void                    parse_string(
     114             :                                       std::string const & str
     115             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     116             :                                     , bool only_environment_variable = false);
     117             :     void                    parse_arguments(
     118             :                                       int argc
     119             :                                     , char * argv[]
     120             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     121             :                                     , bool only_environment_variable = false);
     122             : 
     123             :     flag_t                  process_system_options(std::basic_ostream<char> & out);
     124             : 
     125             :     option_info::map_by_name_t const &
     126             :                             get_options() const;
     127             :     option_info::pointer_t  get_option(std::string const & name, bool exact_option = false) const;
     128             :     option_info::pointer_t  get_option(short_name_t name, bool exact_option = false) const;
     129             :     bool                    is_defined(std::string const & name) const;
     130             :     std::size_t             size(std::string const & name) const;
     131             :     bool                    has_default(std::string const & name) const;
     132             :     std::string             get_default(std::string const & name) const;
     133             :     long                    get_long(
     134             :                                       std::string const & name
     135             :                                     , int idx = 0
     136             :                                     , long min = std::numeric_limits<long>::min()
     137             :                                     , long max = std::numeric_limits<long>::max()) const;
     138             :     std::string             get_string(std::string const & name, int idx = 0) const;
     139             :     std::string             operator [] (std::string const & name) const;
     140             :     option_info_ref         operator [] (std::string const & name);
     141             : 
     142             :     std::string             get_program_name() const;
     143             :     std::string             get_program_fullname() const;
     144             :     std::string             get_project_name() const;
     145             :     std::string             get_group_name() const;
     146             :     std::string             get_environment_variable_name() const;
     147             :     size_t                  get_configuration_filename_size() const;
     148             :     std::string             get_configuration_filename(int idx) const;
     149             :     string_list_t           get_configuration_filenames(
     150             :                                       bool exists
     151             :                                     , bool writable
     152             :                                     , int argc = 0
     153             :                                     , char * argv[] = nullptr) const;
     154             : 
     155             :     group_description const *
     156             :                             find_group(flag_t group) const;
     157             :     std::string             usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
     158             :     std::string             process_help_string(char const * help) const;
     159             :     static std::string      breakup_line(
     160             :                                       std::string line
     161             :                                     , size_t const option_width
     162             :                                     , size_t const line_width);
     163             :     static std::string      format_usage_string(
     164             :                                       std::string const & argument
     165             :                                     , std::string const & help
     166             :                                     , size_t const option_width
     167             :                                     , size_t const line_width);
     168             :     static size_t           get_line_width();
     169             :     static std::string      sanitizer_details();
     170             : 
     171             : private:
     172             :     void                    initialize_parser(options_environment const & opt_env);
     173             :     void                    parse_options_from_group_names();
     174             :     void                    parse_options_from_file();
     175             :     void                    show_option_sources(std::basic_ostream<char> & out);
     176             :     option_info::pointer_t  get_alias_destination(option_info::pointer_t opt) const;
     177             :     void                    define_environment_variable_data();
     178             :     void                    is_parsed() const;
     179             :     static string_list_t    find_config_dir(int argc, char * argv[]);
     180             : 
     181             :     void                    add_options(
     182             :                                       option_info::pointer_t opt
     183             :                                     , int & i
     184             :                                     , int argc
     185             :                                     , char ** argv
     186             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     187             :     void                    add_option_from_string(
     188             :                                       option_info::pointer_t opt
     189             :                                     , std::string const & value
     190             :                                     , std::string const & filename
     191             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     192             : 
     193             :     std::string                         f_program_fullname = std::string();
     194             :     std::string                         f_program_name = std::string();
     195             : 
     196             :     options_environment                 f_options_environment = options_environment();
     197             :     option_info::map_by_name_t          f_options_by_name = option_info::map_by_name_t();
     198             :     option_info::map_by_short_name_t    f_options_by_short_name = option_info::map_by_short_name_t();
     199             :     option_info::pointer_t              f_default_option = option_info::pointer_t();
     200             :     std::string                         f_environment_variable = std::string();
     201             :     bool                                f_parsed = false;
     202             : };
     203             : 
     204             : 
     205             : 
     206             : 
     207             : }   // namespace advgetopt
     208             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13