38#include <snapdev/join_strings.h>
43#include <libexcept/file_inheritance.h>
48#include <boost/preprocessor/stringize.hpp>
63#include <snapdev/poison.h>
80 ,
advgetopt::Help(
"Create a backup before updating the configuration file. If the file exists, keep that old backup instead.")
92 ,
advgetopt::Help(
"Accept an equal sign as the assignment operator (this is the default is not assignment is specified).")
98 ,
advgetopt::Help(
"If the value does not exist, an error is printed and the process exits with 1.")
104 ,
advgetopt::Help(
"By default, if the --remove-comments option is used, the tool writes a warning at the beginning of the file. Use this flag to avoid that warning.")
117 ,
advgetopt::Help(
"By default, the tool tries to keep the comments intact. Use this flag to remove comments.")
124 ,
advgetopt::Help(
"Create a backup before updating the configuration file. If the file exists, replace it.")
137 ,
advgetopt::Help(
"If defined, try reading the file from that sub-directory. If not found there, try in the parent (as defined on the command line). Always save in that sub-directory if editing.")
144 ,
advgetopt::Help(
"Output parameter names with underscores (default).")
151 ,
advgetopt::Help(
"Configuration filename, field name, optionally, a new value.")
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wpedantic"
181 .f_group_name =
nullptr,
183 .f_options_files_directory =
nullptr,
185 .f_environment_variable_intro =
"EDIT_CONFIG_",
186 .f_section_variables_name =
nullptr,
187 .f_configuration_files =
nullptr,
188 .f_configuration_filename =
nullptr,
189 .f_configuration_directories =
nullptr,
191 .f_help_header =
"Usage: %p [-<opt>] <configuration filename> <field name> [<new value>]\n"
192 "where -<opt> is one or more of:",
193 .f_help_footer =
"%c",
195 .f_license =
"GNU GPL v2",
196 .f_copyright =
"Copyright (c) 2013-"
197 BOOST_PP_STRINGIZE(UTC_BUILD_YEAR)
198 " by Made to Order Software Corporation -- All Rights Reserved",
199 .f_build_date = UTC_BUILD_DATE,
200 .f_build_time = UTC_BUILD_TIME,
203#pragma GCC diagnostic pop
238 <<
":error: the --create-backup and --replace-backup command line options are mutually exclusive."
247 <<
":error: no configuration name, field name, and value defined."
256 std::cerr <<
f_opt.
get_program_name() <<
":error: to the minimum a configuration name and a field name are required." << std::endl;
262 std::cerr <<
f_opt.
get_program_name() <<
":error: to the maximum a configuration name, a field name, and a value can be defined." << std::endl;
278 std::string sub_directory_name;
283 std::string filename(segments.back());
292 segments.push_back(filename);
295 sub_directory_name = snapdev::join_strings(segments,
"/");
296 if(access(sub_directory_name.c_str(), F_OK) == 0)
298 config_name = sub_directory_name;
322 std::cerr <<
"error: --dashes & --underscores are mutually exclusive.\n";
331 , assignment_operator
344 && !config->has_parameter(field_name))
346 std::cerr <<
"error: field \""
348 <<
"\" not found in \""
358 if(config->has_parameter(field_name))
360 std::cout << config->get_parameter(field_name) << std::endl;
364 std::cout << std::endl;
371 std::string::size_type colon(field_name.rfind(
':'));
372 if(colon != std::string::npos)
374 std::string
const name_only(field_name.substr(colon + 1));
376 && field_name[colon - 1] ==
':')
380 std::string
const sections(field_name.substr(0, colon));
381 config->set_parameter(sections, name_only, new_value);
385 config->set_parameter(std::string(), field_name, new_value);
388 bool replace_backup(
false);
389 std::string backup_extension;
396 replace_backup =
true;
399 config->save_configuration(
403 , sub_directory_name);
411int main(
int argc,
char * argv[])
413 libexcept::verify_inherited_files();
423 return except.
code();
425 catch(std::exception
const & e)
427 std::cerr <<
"edit-config: exception: " << e.what() << std::endl;
Definitions of the advanced getopt class.
std::shared_ptr< conf_file > pointer_t
static pointer_t get_conf_file(conf_file_setup const &setup)
Create and read a conf_file.
Class used to parse command line options.
std::string get_string(std::string const &name, int idx=0, bool raw=false) const
Get the content of an option as a string.
std::size_t size(std::string const &name) const
Retrieve the number of arguments.
bool is_defined(std::string const &name) const
Check whether a parameter is defined.
std::string get_program_name() const
Get the basename of the program.
void run()
Run the command.
edit_config(int argc, char *argv[])
Initialize the DNS options object.
Declaration of the conf_file class used to read a configuration file.
int main(int argc, char *argv[])
advgetopt::option const g_options[]
advgetopt::group_description const g_group_descriptions[]
advgetopt::options_environment const g_options_environment
Definitions of the advanced getopt exceptions.
constexpr group_description define_group(ARGS ...args)
constexpr comment_t COMMENT_SAVE
void split_string(std::string const &str, string_list_t &result, string_list_t const &separators)
Split a string in sub-strings separated by separators.
constexpr group_description end_groups()
constexpr section_operator_t SECTION_OPERATOR_INI_FILE
std::uint_fast16_t assignment_operator_t
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
constexpr option define_option(ARGS ...args)
constexpr comment_t COMMENT_SHELL
constexpr flag_t command_flags()
constexpr option end_options()
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_EQUAL
constexpr name_separator_t NAME_SEPARATOR_DASHES
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_SPACE
constexpr flag_t all_flags()
constexpr name_separator_t NAME_SEPARATOR_UNDERSCORES
static constexpr flag_t GETOPT_FLAG_DEFAULT_OPTION
constexpr comment_t COMMENT_INI
static constexpr flag_t GETOPT_FLAG_MULTIPLE
static constexpr flag_t GETOPT_FLAG_REQUIRED
std::uint_fast16_t name_separator_t
constexpr flag_t standalone_all_flags()
std::vector< std::string > string_list_t
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_COLON
Structure representing an option.
char const * f_environment_variable_name
char const * f_project_name
Definitions of the advanced getopt class version.
#define LIBADVGETOPT_VERSION_STRING