Skip to content
This repository has been archived by the owner on Aug 19, 2019. It is now read-only.

Add support for providing command line options. #108

Merged
merged 5 commits into from
Mar 31, 2018

Conversation

supriyagarg
Copy link
Contributor

No description provided.

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",
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

("options,o",
boost::program_options::value<std::vector<std::string>>()
->multitoken()->zero_tokens()->composing(),
"Command line options")
Copy link
Member

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"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

std::cout << "Options:\n" << input_str;
}
std::istringstream input { input_str };
ParseConfiguration(input);
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated line

@@ -156,6 +162,25 @@ int Configuration::ParseArguments(int ac, char** av) {
<< std::endl;
return -1;
}
if (flags.count("options")) {
Copy link
Member

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.

Copy link
Contributor Author

@supriyagarg supriyagarg Mar 30, 2018

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.

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("="));
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
input_str += key_value[0] + ": " + key_value[1] + "\n";
}
if (verbose_logging_) {
Copy link
Member

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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (verbose_logging_) {
std::cout << "Options:\n" << input_str;
}
std::istringstream input { input_str };
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated line

std::cerr << "Invalid option " << option;
return 1;
}
input_str += key_value[0] + ": " + key_value[1] + "\n";
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

std::string input_str;
ParseConfigFile(config_file);

// Command line options overwrite the options provided in the config file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/overwrite/override/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// Command line options overwrite the options provided in the config file.
if (flags.count("option")) {
std::stringstream input_stream;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs #include <sstream>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@igorpeshansky igorpeshansky requested review from rbuskens and removed request for bmoyles0117 March 30, 2018 19:11
@@ -16,10 +16,12 @@

#include "configuration.h"

#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <map>
#include <fstream>
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -16,10 +16,12 @@

#include "configuration.h"

#include <boost/algorithm/string.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. removed

@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


// Command line options override the options provided in the config file.
if (flags.count("option")) {
std::stringstream input_stream;
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@supriyagarg supriyagarg force-pushed the command-line-config branch from f7a958c to 210b5be Compare March 30, 2018 20:30
Copy link
Member

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

@igorpeshansky igorpeshansky requested review from bmoyles0117 and removed request for rbuskens March 31, 2018 04:05
@rbuskens
Copy link

LGTM

@igorpeshansky igorpeshansky requested review from rbuskens and removed request for bmoyles0117 March 31, 2018 15:15
@igorpeshansky igorpeshansky merged commit c11744a into Stackdriver:master Mar 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants