-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for providing command line options. #108
Add support for providing command line options. #108
Conversation
src/configuration.cc
Outdated
boost::program_options::options_description flags_desc; | ||
flags_desc.add_options() | ||
("help,h", "Print help message") | ||
("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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change options
to option
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/configuration.cc
Outdated
("options,o", | ||
boost::program_options::value<std::vector<std::string>>() | ||
->multitoken()->zero_tokens()->composing(), | ||
"Command line options") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "Explicit configuration option, e.g., -o CredentialsFile=/tmp/token.json"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/configuration.cc
Outdated
std::cout << "Options:\n" << input_str; | ||
} | ||
std::istringstream input { input_str }; | ||
ParseConfiguration(input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can inline the std::istringstream
construction into the ParseConfiguration
call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outdated line
src/configuration.cc
Outdated
@@ -156,6 +162,25 @@ int Configuration::ParseArguments(int ac, char** av) { | |||
<< std::endl; | |||
return -1; | |||
} | |||
if (flags.count("options")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Also updated ParseConfiguration
to actually pick up only the updated values.
src/configuration.cc
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/configuration.cc
Outdated
} | ||
input_str += key_value[0] + ": " + key_value[1] + "\n"; | ||
} | ||
if (verbose_logging_) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use #ifdef VERBOSE
. These are really debugging statements...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/configuration.cc
Outdated
if (verbose_logging_) { | ||
std::cout << "Options:\n" << input_str; | ||
} | ||
std::istringstream input { input_str }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention elsewhere in the code is to use parentheses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outdated line
src/configuration.cc
Outdated
std::cerr << "Invalid option " << option; | ||
return 1; | ||
} | ||
input_str += key_value[0] + ": " + key_value[1] + "\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than construct one long string, how about creating an std::stringstream
, using <<
to write to it, and then passing it into ParseConfiguration
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/configuration.cc
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/overwrite/override/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/configuration.cc
Outdated
|
||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs #include <sstream>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
src/configuration.cc
Outdated
@@ -16,10 +16,12 @@ | |||
|
|||
#include "configuration.h" | |||
|
|||
#include <boost/algorithm/string.hpp> | |||
#include <boost/program_options.hpp> | |||
#include <iostream> | |||
#include <map> | |||
#include <fstream> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's alphabetize these while we're at it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/configuration.cc
Outdated
@@ -16,10 +16,12 @@ | |||
|
|||
#include "configuration.h" | |||
|
|||
#include <boost/algorithm/string.hpp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct. removed
src/configuration.cc
Outdated
@@ -122,12 +124,18 @@ Configuration::Configuration(std::istream& input) : Configuration() { | |||
|
|||
int Configuration::ParseArguments(int ac, char** av) { | |||
std::string config_file; | |||
std::string command_line_options; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/configuration.cc
Outdated
|
||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/input_stream
/option_stream
/ — otherwise it looks weird that you're writing to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
f7a958c
to
210b5be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
LGTM |
No description provided.