~kdub/mir/devel-to-0.1.7

« back to all changes in this revision

Viewing changes to src/platform/options/default_configuration.cpp

  • Committer: Tarmac
  • Author(s): Alan Griffiths
  • Date: 2014-03-03 11:39:57 UTC
  • mfrom: (1439.1.1 mir2)
  • Revision ID: tarmac-20140303113957-lnvo46btpcf312cz
options: Make it easier to customize DefaultConfiguration.

Approved by PS Jenkins bot, Alberto Aguirre, Kevin DuBois.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
namespace mo = mir::options;
24
24
 
25
 
namespace
26
 
{
27
 
void parse_arguments(
28
 
    boost::program_options::options_description desc,
29
 
    mo::ProgramOption& options,
30
 
    int argc,
31
 
    char const* argv[])
32
 
{
33
 
    namespace po = boost::program_options;
34
 
 
35
 
    try
36
 
    {
37
 
        desc.add_options()
38
 
            ("help,h", "this help text");
39
 
 
40
 
        options.parse_arguments(desc, argc, argv);
41
 
 
42
 
        if (options.is_set("help"))
43
 
        {
44
 
            std::ostringstream help_text;
45
 
            help_text << desc;
46
 
            BOOST_THROW_EXCEPTION(mir::AbnormalExit(help_text.str()));
47
 
        }
48
 
    }
49
 
    catch (po::error const& error)
50
 
    {
51
 
        std::ostringstream help_text;
52
 
        help_text << "Failed to parse command line options: " << error.what() << "." << std::endl << desc;
53
 
        BOOST_THROW_EXCEPTION(mir::AbnormalExit(help_text.str()));
54
 
    }
55
 
}
56
 
 
57
 
void parse_environment(
58
 
    boost::program_options::options_description& desc,
59
 
    mo::ProgramOption& options)
60
 
{
61
 
    // If MIR_SERVER_HOST_SOCKET is unset, we want to substitute the value of
62
 
    // MIR_SOCKET.  Do this now, because MIR_SOCKET will get overwritten later.
63
 
    auto host_socket = getenv("MIR_SERVER_HOST_SOCKET");
64
 
    auto mir_socket = getenv("MIR_SOCKET");
65
 
    if (!host_socket && mir_socket)
66
 
        setenv("MIR_SERVER_HOST_SOCKET", mir_socket, 1);
67
 
 
68
 
    options.parse_environment(desc, "MIR_SERVER_");
69
 
}
70
 
}
71
 
 
72
25
char const* const mo::server_socket_opt           = "file,f";
73
26
char const* const mo::no_server_socket_opt        = "no-file";
74
27
char const* const mo::enable_input_opt            = "enable-input,i";
177
130
    return program_options->add_options();
178
131
}
179
132
 
180
 
void mo::DefaultConfiguration::parse_options(boost::program_options::options_description& options_description, ProgramOption& options) const
181
 
{
182
 
    parse_arguments(options_description, options, argc, argv);
183
 
    parse_environment(options_description, options);
184
 
}
185
 
 
186
133
std::shared_ptr<mo::Option> mo::DefaultConfiguration::the_options() const
187
134
{
188
135
    if (!options)
189
136
    {
190
137
        auto options = std::make_shared<ProgramOption>();
191
 
        parse_options(*program_options, *options);
 
138
        parse_arguments(*program_options, *options, argc, argv);
 
139
        parse_environment(*program_options, *options);
 
140
        parse_config_file(*program_options, *options);
192
141
        this->options = options;
193
142
    }
194
143
    return options;
195
144
}
 
145
 
 
146
 
 
147
void mo::DefaultConfiguration::parse_arguments(
 
148
    boost::program_options::options_description desc,
 
149
    mo::ProgramOption& options,
 
150
    int argc,
 
151
    char const* argv[]) const
 
152
{
 
153
    namespace po = boost::program_options;
 
154
 
 
155
    try
 
156
    {
 
157
        desc.add_options()
 
158
            ("help,h", "this help text");
 
159
 
 
160
        options.parse_arguments(desc, argc, argv);
 
161
 
 
162
        if (options.is_set("help"))
 
163
        {
 
164
            std::ostringstream help_text;
 
165
            help_text << desc;
 
166
            BOOST_THROW_EXCEPTION(mir::AbnormalExit(help_text.str()));
 
167
        }
 
168
    }
 
169
    catch (po::error const& error)
 
170
    {
 
171
        std::ostringstream help_text;
 
172
        help_text << "Failed to parse command line options: " << error.what() << "." << std::endl << desc;
 
173
        BOOST_THROW_EXCEPTION(mir::AbnormalExit(help_text.str()));
 
174
    }
 
175
}
 
176
 
 
177
void mo::DefaultConfiguration::parse_environment(
 
178
    boost::program_options::options_description& desc,
 
179
    mo::ProgramOption& options) const
 
180
{
 
181
    // If MIR_SERVER_HOST_SOCKET is unset, we want to substitute the value of
 
182
    // MIR_SOCKET.  Do this now, because MIR_SOCKET will get overwritten later.
 
183
    auto host_socket = getenv("MIR_SERVER_HOST_SOCKET");
 
184
    auto mir_socket = getenv("MIR_SOCKET");
 
185
    if (!host_socket && mir_socket)
 
186
        setenv("MIR_SERVER_HOST_SOCKET", mir_socket, 1);
 
187
 
 
188
    options.parse_environment(desc, "MIR_SERVER_");
 
189
}
 
190
 
 
191
void mo::DefaultConfiguration::parse_config_file(
 
192
    boost::program_options::options_description& /*desc*/,
 
193
    mo::ProgramOption& /*options*/) const
 
194
{
 
195
}