Add support for providing command line options.#108
Add support for providing command line options.#108igorpeshansky merged 5 commits intoStackdriver:masterfrom
Conversation
| ("version,V", "Print the agent version") | ||
| ("verbose,v", boost::program_options::bool_switch(&verbose_logging_), | ||
| "Enable verbose logging") | ||
| ("options,o", |
There was a problem hiding this comment.
Let's change options to option.
| ("options,o", | ||
| boost::program_options::value<std::vector<std::string>>() | ||
| ->multitoken()->zero_tokens()->composing(), | ||
| "Command line options") |
There was a problem hiding this comment.
How about "Explicit configuration option, e.g., -o CredentialsFile=/tmp/token.json"?
| std::cout << "Options:\n" << input_str; | ||
| } | ||
| std::istringstream input { input_str }; | ||
| ParseConfiguration(input); |
There was a problem hiding this comment.
You can inline the std::istringstream construction into the ParseConfiguration call.
| << std::endl; | ||
| return -1; | ||
| } | ||
| if (flags.count("options")) { |
There was a problem hiding this comment.
We should probably do this after the ParseConfigFile call, to allow explicit config option settings to override the configuration file.
There was a problem hiding this comment.
Done. Also updated ParseConfiguration to actually pick up only the updated values.
| flags["options"].as<std::vector<std::string>>(); | ||
| for (const std::string& option: options) { | ||
| std::vector<std::string> key_value; | ||
| boost::algorithm::split(key_value, option, boost::is_any_of("=")); |
There was a problem hiding this comment.
Careful — the value may actually have = signs in it, and there's no good way to quote. We should just use std::string::find to get the first = sign, and leave the rest alone.
| } | ||
| input_str += key_value[0] + ": " + key_value[1] + "\n"; | ||
| } | ||
| if (verbose_logging_) { |
There was a problem hiding this comment.
Let's use #ifdef VERBOSE. These are really debugging statements...
| if (verbose_logging_) { | ||
| std::cout << "Options:\n" << input_str; | ||
| } | ||
| std::istringstream input { input_str }; |
There was a problem hiding this comment.
The convention elsewhere in the code is to use parentheses.
| std::cerr << "Invalid option " << option; | ||
| return 1; | ||
| } | ||
| input_str += key_value[0] + ": " + key_value[1] + "\n"; |
There was a problem hiding this comment.
Rather than construct one long string, how about creating an std::stringstream, using << to write to it, and then passing it into ParseConfiguration?
| std::string input_str; | ||
| ParseConfigFile(config_file); | ||
|
|
||
| // Command line options overwrite the options provided in the config file. |
There was a problem hiding this comment.
s/overwrite/override/
|
|
||
| // Command line options overwrite the options provided in the config file. | ||
| if (flags.count("option")) { | ||
| std::stringstream input_stream; |
There was a problem hiding this comment.
Needs #include <sstream>.
| #include <boost/program_options.hpp> | ||
| #include <iostream> | ||
| #include <map> | ||
| #include <fstream> |
There was a problem hiding this comment.
Let's alphabetize these while we're at it.
|
|
||
| #include "configuration.h" | ||
|
|
||
| #include <boost/algorithm/string.hpp> |
There was a problem hiding this comment.
No longer needed, right?
There was a problem hiding this comment.
correct. removed
|
|
||
| int Configuration::ParseArguments(int ac, char** av) { | ||
| std::string config_file; | ||
| std::string command_line_options; |
|
|
||
| // Command line options override the options provided in the config file. | ||
| if (flags.count("option")) { | ||
| std::stringstream input_stream; |
There was a problem hiding this comment.
s/input_stream/option_stream/ — otherwise it looks weird that you're writing to it.
f7a958c to
210b5be
Compare
|
LGTM |
No description provided.