3#include <boost/program_options.hpp>
10boost::program_options::options_description DescribeOptions() {
11 namespace po = boost::program_options;
12 po::options_description description(
"Options");
13 description.add_options()
14 (
"help,h",
"Show usage")
15 (
"endpoint-url", po::value<std::string>(),
"S3 HTTP(S) endpoint without the bucket")
16 (
"bucket", po::value<std::string>(),
"Bucket with virtual-hosted addressing")
17 (
"key", po::value<std::string>(),
"Object key, without URL encoding")
18 (
"credentials-file", po::value<std::string>(),
"JSON file with access_key_id and secret_key")
19 (
"signature-version", po::value<std::string>()->default_value(
"v4"),
"v2 or v4")
20 (
"region", po::value<std::string>(),
"Signing region, required for v4")
21 (
"timeout-ms", po::value<
int>()->default_value(kDefaultTimeoutMs),
"Positive request timeout in milliseconds")
22 (
"retries", po::value<
int>()->default_value(kDefaultRetries),
"Positive HTTP driver attempt count")
23 (
"body", po::value<std::string>(),
"Input file for put-object")
24 (
"if-match", po::value<std::string>(),
"Expected ETag for conditional put-object")
25 (
"if-none-match", po::value<std::string>(),
"Use '*' to create an object only if absent");
31void RequireNonempty(
const std::string& value,
const char* message) {
33 throw std::invalid_argument(message);
37void ValidateEndpoint(std::string& endpoint) {
38 const auto scheme_size = endpoint.starts_with(kHttpsScheme) ? kHttpsScheme.size() : kHttpScheme.size();
39 if (!endpoint.starts_with(kHttpsScheme) && !endpoint.starts_with(kHttpScheme)) {
40 throw std::invalid_argument(
"--endpoint-url must start with http:// or https://");
42 if (endpoint.ends_with(
'/')) {
45 if (endpoint.size() <= scheme_size || endpoint.find_first_of(
"/?#@", scheme_size) != std::string::npos) {
46 throw std::invalid_argument(
"--endpoint-url must contain only a scheme and host, optionally a port");
50void ValidateAuthentication(
const Options& options) {
51 if (options.signature_version !=
"v2" && options.signature_version !=
"v4") {
52 throw std::invalid_argument(
"--signature-version must be v2 or v4");
54 if (options.signature_version ==
"v4" && (!options.region || options.region->empty())) {
55 throw std::invalid_argument(
"Specify --region for signature version v4");
57 if (options.signature_version ==
"v2" && options.region) {
58 throw std::invalid_argument(
"Remove --region when using signature version v2");
60 if (options.timeout_ms <= 0 || options.retries <= 0) {
61 throw std::invalid_argument(
"--timeout-ms and --retries must be positive");
65void ValidateOperation(
const Options& options,
bool body_specified,
bool output_specified) {
66 if (options.if_match && options.if_none_match) {
67 throw std::invalid_argument(
"Use only one of --if-match and --if-none-match");
69 if (options.command == Command::kGetObject) {
70 RequireNonempty(options.output_file,
"get-object requires an output file after the command");
71 if (body_specified || options.if_match || options.if_none_match) {
72 throw std::invalid_argument(
"--body, --if-match and --if-none-match are supported only for put-object");
75 RequireNonempty(options.body_file,
"put-object requires --body with an input file path");
76 if (output_specified) {
77 throw std::invalid_argument(
"put-object does not accept an output file");
84Options ParseOptions(
const std::vector<std::string>& arguments) {
85 namespace po = boost::program_options;
86 auto description = DescribeOptions();
87 po::options_description positional_options;
88 positional_options.add_options()(
"command", po::value<std::string>())(
"outfile", po::value<std::string>());
89 description.add(positional_options);
90 po::positional_options_description positional;
91 positional.add(
"command", 1).add(
"outfile", 1);
92 po::variables_map values;
93 po::store(po::command_line_parser(arguments).options(description).positional(positional).run(), values);
96 options.help = values.count(
"help") != 0;
100 const auto read_string = [&values](
const char* name) {
101 return values.count(name) ? values[name].as<std::string>() : std::string{};
103 const auto command_name = read_string(
"command");
104 options.endpoint = read_string(
"endpoint-url");
105 options.bucket = read_string(
"bucket");
106 options.key = read_string(
"key");
107 options.credentials_file = read_string(
"credentials-file");
108 options.signature_version = read_string(
"signature-version");
109 options.body_file = read_string(
"body");
110 options.output_file = read_string(
"outfile");
111 options.timeout_ms = values[
"timeout-ms"].as<
int>();
112 options.retries = values[
"retries"].as<
int>();
113 if (values.count(
"region")) {
114 options.region = read_string(
"region");
116 if (values.count(
"if-match")) {
117 options.if_match = read_string(
"if-match");
118 RequireNonempty(*options.if_match,
"--if-match requires a nonempty ETag");
120 if (values.count(
"if-none-match")) {
121 if (read_string(
"if-none-match") !=
"*") {
122 throw std::invalid_argument(
"--if-none-match supports only '*'");
124 options.if_none_match =
true;
126 if (command_name ==
"get-object") {
127 options.command = Command::kGetObject;
128 }
else if (command_name ==
"put-object") {
130 .command = options.if_match || options.if_none_match ? Command::kPutObjectConditional : Command::kPutObject;
132 throw std::invalid_argument(
"Specify get-object or put-object; use --help for usage");
134 RequireNonempty(options.endpoint,
"Specify --endpoint-url");
135 RequireNonempty(options.bucket,
"Specify --bucket");
136 RequireNonempty(options.key,
"Specify --key");
137 RequireNonempty(options.credentials_file,
"Specify --credentials-file");
138 ValidateEndpoint(options.endpoint);
139 ValidateAuthentication(options);
140 ValidateOperation(options, values.count(
"body") != 0, values.count(
"outfile") != 0);