advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
advgetopt_string.cpp
Go to the documentation of this file.
1// Copyright (c) 2006-2024 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
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
44namespace advgetopt
45{
46
47
48namespace
49{
50
51constexpr char const g_space = ' ';
52
53
54} // no name namespace
55
70std::string getopt::options_to_string(bool include_progname, bool keep_defaults) const
71{
72 std::string result;
73
74 if(include_progname)
75 {
77 }
78
80 for(auto const & opt : f_options_by_name)
81 {
82 if(!opt.second->is_defined())
83 {
84 continue;
85 }
86 if(opt.second->is_default_option())
87 {
88 default_option = opt.second;
89 continue;
90 }
91
92 if(!keep_defaults
93 && !opt.second->has_flag(advgetopt::GETOPT_FLAG_FLAG)
94 && opt.second->get_default() == opt.second->get_value())
95 {
96 // same as default, no need to add that parameter
97 //
98 continue;
99 }
100
101 if(!result.empty())
102 {
103 result += g_space;
104 }
105
106 result += "--";
107 result += opt.second->get_name();
108
109 if(!opt.second->has_flag(advgetopt::GETOPT_FLAG_FLAG))
110 {
111 result += g_space;
112 result += escape_shell_argument(opt.second->get_value());
113 std::size_t const max(opt.second->size());
114 for(std::size_t idx(1); idx < max; ++idx)
115 {
116 result += g_space;
117 result += escape_shell_argument(opt.second->get_value(idx));
118 }
119 }
120 }
121
122 if(default_option != nullptr)
123 {
124 result += " -- ";
125
126 result += escape_shell_argument(default_option->get_value());
127 std::size_t const max(default_option->size());
128 for(std::size_t idx(1); idx < max; ++idx)
129 {
130 result += g_space;
131 result += escape_shell_argument(default_option->get_value(idx));
132 }
133 }
134
135 return result;
136}
137
138
139
140} // namespace advgetopt
141// vim: ts=4 sw=4 et
Definitions of the advanced getopt class.
std::string options_to_string(bool include_progname=false, bool keep_defaults=false) const
Transform all the defined options back in a string.
option_info::map_by_name_t f_options_by_name
Definition advgetopt.h:225
std::string get_program_fullname() const
Get the full name of the program.
std::shared_ptr< option_info > pointer_t
Definition option_info.h:78
The advgetopt environment to parse command line options.
static constexpr flag_t GETOPT_FLAG_FLAG
Definition flags.h:51
std::string escape_shell_argument(std::string const &arg)
Escape special characters from a shell argument.
Definition utils.cpp:959

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.