LCOV - code coverage report
Current view: top level - advgetopt - advgetopt_string.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 35 100.0 %
Date: 2022-07-15 17:40:56 Functions: 3 3 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             : 
      20             : /** \file
      21             :  * \brief Implementation of the options_to_strings() command.
      22             :  *
      23             :  * The getopt object holds all the command line options your program was
      24             :  * started with. Here we transforms these options back to shell command
      25             :  * line options so one can start the command again with the same options.
      26             :  */
      27             : 
      28             : // self
      29             : //
      30             : #include    "advgetopt/advgetopt.h"
      31             : 
      32             : 
      33             : // cppthread
      34             : //
      35             : #include    <cppthread/log.h>
      36             : 
      37             : 
      38             : // last include
      39             : //
      40             : #include    <snapdev/poison.h>
      41             : 
      42             : 
      43             : 
      44             : namespace advgetopt
      45             : {
      46             : 
      47             : 
      48             : namespace
      49             : {
      50             : 
      51             : constexpr char const g_space = ' ';
      52             : 
      53             : 
      54             : } // no name namespace
      55             : 
      56             : /** \brief Transform all the defined options back in a string.
      57             :  *
      58             :  * This function creates a string which system() can use to start the
      59             :  * command again with the same options. You may, of course, tweak the
      60             :  * options first.
      61             :  *
      62             :  * \param[in] include_progname  Whether the program name should be included
      63             :  * in the output string. In some cases, you may want to start a different
      64             :  * program with similar command line options. This gives you that option.
      65             :  * \param[in] keep_defaults  If the value is equal to the default value, it
      66             :  * gets ignored unless this parameter is set to true.
      67             :  *
      68             :  * \return The string representing the command line options.
      69             :  */
      70           4 : std::string getopt::options_to_string(bool include_progname, bool keep_defaults) const
      71             : {
      72           4 :     std::string result;
      73             : 
      74           4 :     if(include_progname)
      75             :     {
      76           2 :         result += escape_shell_argument(get_program_fullname());
      77             :     }
      78             : 
      79           8 :     advgetopt::option_info::pointer_t default_option;
      80          72 :     for(auto const & opt : f_options_by_name)
      81             :     {
      82          68 :         if(!opt.second->is_defined())
      83             :         {
      84          48 :             continue;
      85             :         }
      86          24 :         if(opt.second->is_default_option())
      87             :         {
      88           4 :             default_option = opt.second;
      89           4 :             continue;
      90             :         }
      91             : 
      92          42 :         if(!keep_defaults
      93           8 :         && !opt.second->has_flag(advgetopt::GETOPT_FLAG_FLAG)
      94          44 :         && opt.second->get_default() == opt.second->get_value())
      95             :         {
      96             :             // same as default, no need to add that parameter
      97             :             //
      98           2 :             continue;
      99             :         }
     100             : 
     101          14 :         if(!result.empty())
     102             :         {
     103          12 :             result += g_space;
     104             :         }
     105             : 
     106          14 :         result += "--";
     107          14 :         result += opt.second->get_name();
     108             : 
     109          14 :         if(!opt.second->has_flag(advgetopt::GETOPT_FLAG_FLAG))
     110             :         {
     111          10 :             result += g_space;
     112          10 :             result += escape_shell_argument(opt.second->get_value());
     113          10 :             std::size_t const max(opt.second->size());
     114          26 :             for(std::size_t idx(1); idx < max; ++idx)
     115             :             {
     116          16 :                 result += g_space;
     117          16 :                 result += escape_shell_argument(opt.second->get_value(idx));
     118             :             }
     119             :         }
     120             :     }
     121             : 
     122           4 :     if(default_option != nullptr)
     123             :     {
     124           4 :         result += " -- ";
     125             : 
     126           4 :         result += escape_shell_argument(default_option->get_value());
     127           4 :         std::size_t const max(default_option->size());
     128          12 :         for(std::size_t idx(1); idx < max; ++idx)
     129             :         {
     130           8 :             result += g_space;
     131           8 :             result += escape_shell_argument(default_option->get_value(idx));
     132             :         }
     133             :     }
     134             : 
     135           8 :     return result;
     136             : }
     137             : 
     138             : 
     139             : 
     140           6 : } // namespace advgetopt
     141             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13