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-05-26 21:41:34 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             : constexpr flag_t            SYSTEM_OPTION_SHOW_OPTION_VALUE             = 0x0200;
      67             : 
      68             : // system options
      69             : constexpr flag_t            SYSTEM_OPTION_CONFIG_DIR                    = 0x1000;
      70             : 
      71             : constexpr flag_t            SYSTEM_OPTION_COMMANDS_MASK                 = 0x0FFF;
      72             : constexpr flag_t            SYSTEM_OPTION_OPTIONS_MASK                  = 0xF000;
      73             : 
      74             : 
      75             : 
      76         328 : class getopt
      77             : {
      78             : public:
      79             :     typedef std::shared_ptr<getopt>     pointer_t;
      80             : 
      81             :                             getopt(options_environment const & opts);
      82             :                             getopt(options_environment const & opts
      83             :                                  , int argc
      84             :                                  , char * argv[]);
      85             : 
      86             :     options_environment const &
      87             :                             get_options_environment() const;
      88             :     bool                    has_flag(flag_t flag) const;
      89             :     void                    reset();
      90             : 
      91             :     void                    parse_options_info(
      92             :                                       option const * opts
      93             :                                     , bool ignore_duplicates = false);
      94             :     void                    parse_options_from_file(
      95             :                                       std::string const & filename
      96             :                                     , int min_sections
      97             :                                     , int max_sections
      98             :                                     , bool ignore_duplicates = false);
      99             :     void                    add_option(
     100             :                                       option_info::pointer_t opt
     101             :                                     , bool ignore_duplicates = false);
     102             :     void                    link_aliases();
     103             :     void                    set_short_name(
     104             :                                       std::string const & name
     105             :                                     , short_name_t short_name);
     106             : 
     107             :     void                    finish_parsing(int argc, char * argv[]);
     108             : 
     109             :     void                    parse_program_name(char * argv[]);
     110             : 
     111             :     void                    parse_configuration_files(
     112             :                                       int argc = 0
     113             :                                     , char * argv[] = nullptr);
     114             :     void                    process_configuration_file(std::string const & filename);
     115             : 
     116             :     std::string             get_environment_variable() const;
     117             :     static string_list_t    split_environment(std::string const & environment);
     118             :     void                    parse_environment_variable();
     119             : 
     120             :     void                    parse_string(
     121             :                                       std::string const & str
     122             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     123             :                                     , bool only_environment_variable = false);
     124             :     void                    parse_arguments(
     125             :                                       int argc
     126             :                                     , char * argv[]
     127             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT
     128             :                                     , bool only_environment_variable = false);
     129             : 
     130             :     flag_t                  process_system_options(std::basic_ostream<char> & out);
     131             : 
     132             :     option_info::map_by_name_t const &
     133             :                             get_options() const;
     134             :     option_info::pointer_t  get_option(
     135             :                                       std::string const & name
     136             :                                     , bool exact_option = false) const;
     137             :     option_info::pointer_t  get_option(
     138             :                                       short_name_t name
     139             :                                     , bool exact_option = false) const;
     140             :     std::string             options_to_string(
     141             :                                       bool include_progname = false
     142             :                                     , bool keep_defaults = false) const;
     143             :     bool                    is_defined(std::string const & name) const;
     144             :     std::size_t             size(std::string const & name) const;
     145             :     bool                    has_default(std::string const & name) const;
     146             :     std::string             get_default(std::string const & name) const;
     147             :     long                    get_long(
     148             :                                       std::string const & name
     149             :                                     , int idx = 0
     150             :                                     , long min = std::numeric_limits<long>::min()
     151             :                                     , long max = std::numeric_limits<long>::max()) const;
     152             :     double                  get_double(
     153             :                                       std::string const & name
     154             :                                     , int idx = 0
     155             :                                     , double min = std::numeric_limits<double>::min()
     156             :                                     , double max = std::numeric_limits<double>::max()) const;
     157             :     std::string             get_string(
     158             :                                       std::string const & name
     159             :                                     , int idx = 0
     160             :                                     , bool raw = false) const;
     161             :     std::string             operator [] (std::string const & name) const;
     162             :     option_info_ref         operator [] (std::string const & name);
     163             : 
     164             :     std::string             get_program_name() const;
     165             :     std::string             get_program_fullname() const;
     166             :     std::string             get_project_name() const;
     167             :     std::string             get_group_name() const;
     168             :     void                    define_environment_variable_data();
     169             :     std::string             get_environment_variable_name() const;
     170             :     size_t                  get_configuration_filename_size() const;
     171             :     std::string             get_configuration_filename(int idx) const;
     172             :     string_list_t           get_configuration_filenames(
     173             :                                       bool exists
     174             :                                     , bool writable
     175             :                                     , int argc = 0
     176             :                                     , char * argv[] = nullptr) const;
     177             : 
     178             :     group_description const *
     179             :                             find_group(flag_t group) const;
     180             :     std::string             usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
     181             :     std::string             process_help_string(char const * help) const;
     182             :     static std::string      breakup_line(
     183             :                                       std::string line
     184             :                                     , size_t const option_width
     185             :                                     , size_t const line_width);
     186             :     static std::string      format_usage_string(
     187             :                                       std::string const & argument
     188             :                                     , std::string const & help
     189             :                                     , size_t const option_width
     190             :                                     , size_t const line_width);
     191             :     static size_t           get_line_width();
     192             :     static std::string      sanitizer_details();
     193             :     static std::string      escape_shell_argument(std::string const & arg);
     194             : 
     195             :     variables::pointer_t    get_variables() const;
     196             : 
     197             : private:
     198             :     void                    initialize_parser(options_environment const & opt_env);
     199             :     void                    parse_options_from_group_names();
     200             :     void                    parse_options_from_file();
     201             :     void                    show_option_sources(std::basic_ostream<char> & out);
     202             :     option_info::pointer_t  get_alias_destination(option_info::pointer_t opt) const;
     203             :     void                    is_parsed() const;
     204             :     static string_list_t    find_config_dir(int argc, char * argv[]);
     205             : 
     206             :     void                    add_options(
     207             :                                       option_info::pointer_t opt
     208             :                                     , int & i
     209             :                                     , int argc
     210             :                                     , char ** argv
     211             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     212             :     void                    add_option_from_string(
     213             :                                       option_info::pointer_t opt
     214             :                                     , std::string const & value
     215             :                                     , std::string const & filename
     216             :                                     , option_source_t source = option_source_t::SOURCE_DIRECT);
     217             : 
     218             :     std::string                         f_program_fullname = std::string();
     219             :     std::string                         f_program_name = std::string();
     220             : 
     221             :     options_environment                 f_options_environment = options_environment();
     222             :     option_info::map_by_name_t          f_options_by_name = option_info::map_by_name_t();
     223             :     option_info::map_by_short_name_t    f_options_by_short_name = option_info::map_by_short_name_t();
     224             :     option_info::pointer_t              f_default_option = option_info::pointer_t();
     225             :     std::string                         f_environment_variable = std::string();
     226             :     variables::pointer_t                f_variables = variables::pointer_t();
     227             :     bool                                f_parsed = false;
     228             : };
     229             : 
     230             : 
     231             : 
     232             : 
     233             : }   // namespace advgetopt
     234             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13