1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "cli/cmd_config.hpp"
33 #include "cli/common.ipp"
34 #include "utils/cmdline/parser.ipp"
35 #include "utils/cmdline/ui.hpp"
36 #include "utils/config/tree.ipp"
37 #include "utils/format/macros.hpp"
39 namespace cmdline = utils::cmdline;
40 namespace config = utils::config;
42 using cli::cmd_config;
48 /// Prints all configuration variables.
50 /// \param ui Object to interact with the I/O of the program.
51 /// \param properties The key/value map representing all the configuration
54 /// \return 0 for success.
56 print_all(cmdline::ui* ui, const config::properties_map& properties)
58 for (config::properties_map::const_iterator iter = properties.begin();
59 iter != properties.end(); iter++)
60 ui->out(F("%s = %s") % (*iter).first % (*iter).second);
65 /// Prints the configuration variables that the user requests.
67 /// \param ui Object to interact with the I/O of the program.
68 /// \param properties The key/value map representing all the configuration
70 /// \param filters The names of the configuration variables to print.
72 /// \return 0 if all specified filters are valid; 1 otherwise.
74 print_some(cmdline::ui* ui, const config::properties_map& properties,
75 const cmdline::args_vector& filters)
79 for (cmdline::args_vector::const_iterator iter = filters.begin();
80 iter != filters.end(); iter++) {
81 const config::properties_map::const_iterator match =
82 properties.find(*iter);
83 if (match == properties.end()) {
84 cmdline::print_warning(ui, F("'%s' is not defined.") % *iter);
87 ui->out(F("%s = %s") % (*match).first % (*match).second);
90 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
94 } // anonymous namespace
97 /// Default constructor for cmd_config.
98 cmd_config::cmd_config(void) : cli_command(
99 "config", "[variable1 .. variableN]", 0, -1,
100 "Inspects the values of configuration variables")
105 /// Entry point for the "config" subcommand.
107 /// \param ui Object to interact with the I/O of the program.
108 /// \param cmdline Representation of the command line to the subcommand.
109 /// \param user_config The runtime configuration of the program.
111 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
114 cmd_config::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
115 const config::tree& user_config)
117 const config::properties_map properties = user_config.all_properties();
118 if (cmdline.arguments().empty())
119 return print_all(ui, properties);
121 return print_some(ui, properties, cmdline.arguments());