38#include <snapdev/join_strings.h>
39#include <snapdev/stringize.h>
44#include <libexcept/file_inheritance.h>
59#include <snapdev/poison.h>
76 ,
advgetopt::Help(
"Create a backup before updating the configuration file. If the file exists, keep that old backup instead.")
88 ,
advgetopt::Help(
"Accept an equal sign as the assignment operator (this is the default is not assignment is specified).")
94 ,
advgetopt::Help(
"If the value does not exist, an error is printed and the process exits with 1.")
100 ,
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.")
113 ,
advgetopt::Help(
"By default, the tool tries to keep the comments intact. Use this flag to remove comments.")
120 ,
advgetopt::Help(
"Create a backup before updating the configuration file. If the file exists, replace it.")
133 ,
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.")
140 ,
advgetopt::Help(
"Output parameter names with underscores (default).")
147 ,
advgetopt::Help(
"Configuration filename, field name, optionally, a new value.")
172#pragma GCC diagnostic push
173#pragma GCC diagnostic ignored "-Wpedantic"
177 .f_group_name =
nullptr,
179 .f_options_files_directory =
nullptr,
181 .f_environment_variable_intro =
"EDIT_CONFIG_",
182 .f_section_variables_name =
nullptr,
183 .f_configuration_files =
nullptr,
184 .f_configuration_filename =
nullptr,
185 .f_configuration_directories =
nullptr,
187 .f_help_header =
"Usage: %p [-<opt>] <configuration filename> <field name> [<new value>]\n"
188 "where -<opt> is one or more of:",
189 .f_help_footer =
"%c",
191 .f_license =
"GNU GPL v2",
192 .f_copyright =
"Copyright (c) 2013-"
193 SNAPDEV_STRINGIZE(UTC_BUILD_YEAR)
194 " by Made to Order Software Corporation -- All Rights Reserved",
195 .f_build_date = UTC_BUILD_DATE,
196 .f_build_time = UTC_BUILD_TIME,
199#pragma GCC diagnostic pop
234 <<
":error: the --create-backup and --replace-backup command line options are mutually exclusive."
243 <<
":error: no configuration name, field name, and value defined."
252 std::cerr <<
f_opt.
get_program_name() <<
":error: to the minimum a configuration name and a field name are required." << std::endl;
258 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;
274 std::string sub_directory_name;
279 std::string filename(segments.back());
288 segments.push_back(filename);
291 sub_directory_name = snapdev::join_strings(segments,
"/");
292 if(access(sub_directory_name.c_str(), F_OK) == 0)
294 config_name = sub_directory_name;
318 std::cerr <<
"error: --dashes & --underscores are mutually exclusive.\n";
327 , assignment_operator
340 && !config->has_parameter(field_name))
342 std::cerr <<
"error: field \""
344 <<
"\" not found in \""
354 if(config->has_parameter(field_name))
356 std::cout << config->get_parameter(field_name) << std::endl;
360 std::cout << std::endl;
367 std::string::size_type colon(field_name.rfind(
':'));
368 if(colon != std::string::npos)
370 std::string
const name_only(field_name.substr(colon + 1));
372 && field_name[colon - 1] ==
':')
376 std::string
const sections(field_name.substr(0, colon));
377 config->set_parameter(sections, name_only, new_value);
381 config->set_parameter(std::string(), field_name, new_value);
384 bool replace_backup(
false);
385 std::string backup_extension;
392 replace_backup =
true;
395 config->save_configuration(
399 , sub_directory_name);
407int main(
int argc,
char * argv[])
409 libexcept::verify_inherited_files();
419 return except.
code();
421 catch(std::exception
const & e)
423 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