1717 * License-Filename: LICENSE
1818 */
1919
20+ #include " Options.h"
2021#include " ProtectedCacheExample.h"
2122#include " ReadExample.h"
2223#include " WriteExample.h"
2324
24- #include < getopt.h >
25+ #include < iostream >
2526#include < stdio.h>
2627#include < string.h>
27- #include < unistd.h>
28- #include < iostream>
28+
29+ bool IsMatch (const std::string& name, const tools::Option& option) {
30+ return name == option.short_name || name == option.long_name ;
31+ }
2932
3033enum Examples : int {
3134 read_example = 0b1 ,
@@ -39,7 +42,7 @@ constexpr auto usage =
3942 " example [read, write, cache] \n -i [--key_id] here.access.key.id \n -s "
4043 " [--key_secret] here.access.key.secret \n "
4144 " -c [--catalog] catalog HRN (HERE Resource Name). \n "
42- " -l [--layer_id] t layer ID inside the catalog where you want to "
45+ " -l [--layer_id] the layer ID inside the catalog where you want to "
4346 " publish data to(required for write example). \n "
4447 " -h [--help]: show usage \n For "
4548 " instructions on how to get the access key ID and access key secret, see "
@@ -48,6 +51,80 @@ constexpr auto usage =
4851 " user-guide/topics/get-credentials.html) section in the Terms and "
4952 " Permissions User Guide." ;
5053
54+ int RequiredArgumentError (const tools::Option& arg) {
55+ std::cout << " option requires an argument -- '" << arg.short_name << ' \' '
56+ << " [" << arg.long_name << " ] " << arg.description << std::endl;
57+ return 0 ;
58+ }
59+ int ParseArguments (const int argc, char ** argv, AccessKey& access_key,
60+ std::string& catalog, std::string& layer_id) {
61+ int examples_to_run = 0 ;
62+
63+ const std::vector<std::string> arguments (argv + 1 , argv + argc);
64+ auto it = arguments.begin ();
65+
66+ while (it != arguments.end ()) {
67+ if (IsMatch (*it, tools::kHelpOption )) {
68+ std::cout << usage << std::endl;
69+ return 0 ;
70+ }
71+
72+ if (IsMatch (*it, tools::kKeyIdOption )) {
73+ // Here access key ID.
74+ if (++it == arguments.end ()) {
75+ return RequiredArgumentError (tools::kKeyIdOption );
76+ }
77+ access_key.id = *it;
78+ } else if (IsMatch (*it, tools::kKeySecretOption )) {
79+ // Here access key secret.
80+ if (++it == arguments.end ()) {
81+ return RequiredArgumentError (tools::kKeySecretOption );
82+ }
83+ access_key.secret = *it;
84+ } else if (IsMatch (*it, tools::kExampleOption )) {
85+ if (++it == arguments.end ()) {
86+ return RequiredArgumentError (tools::kExampleOption );
87+ }
88+
89+ if (*it == " read" ) {
90+ examples_to_run = Examples::read_example;
91+ } else if (*it == " write" ) {
92+ examples_to_run = Examples::write_example;
93+ } else if (*it == " cache" ) {
94+ examples_to_run = Examples::cache_example;
95+ } else {
96+ std::cout
97+ << " Example was not found. Please use values:read, write, cache"
98+ << std::endl;
99+ return 0 ;
100+ }
101+
102+ } else if (IsMatch (*it, tools::kCatalogOption )) {
103+ if (++it == arguments.end ()) {
104+ return RequiredArgumentError (tools::kCatalogOption );
105+ }
106+ catalog = *it;
107+ } else if (IsMatch (*it, tools::kLayerIdOption )) {
108+ if (++it == arguments.end ()) {
109+ return RequiredArgumentError (tools::kLayerIdOption );
110+ }
111+ layer_id = *it;
112+ } else if (IsMatch (*it, tools::kAllOption )) {
113+ examples_to_run = Examples::all_examples;
114+ } else {
115+ fprintf (stderr, usage);
116+ }
117+ ++it;
118+ }
119+
120+ if (examples_to_run == 0 ) {
121+ std::cout << " Please specify command line arguments." << std::endl;
122+ std::cout << usage << std::endl;
123+ }
124+
125+ return examples_to_run;
126+ }
127+
51128int RunExamples (const AccessKey& access_key, int examples_to_run,
52129 const std::string& catalog, const std::string& layer_id) {
53130 if (examples_to_run & Examples::read_example) {
@@ -77,63 +154,17 @@ int RunExamples(const AccessKey& access_key, int examples_to_run,
77154}
78155
79156int main (int argc, char ** argv) {
80- static struct option long_options[] = {
81- {" example" , required_argument, 0 , ' e' },
82- {" key_id" , required_argument, 0 , ' i' },
83- {" key_secret" , required_argument, 0 , ' s' },
84- {" catalog" , required_argument, 0 , ' c' },
85- {" layer_id" , required_argument, 0 , ' l' },
86- {" all" , no_argument, 0 , ' a' },
87- {" help" , no_argument, 0 , ' h' },
88- {0 , 0 , 0 , 0 }};
89-
90157 AccessKey access_key{}; // You can specify your here.access.key.id
91158 // and here.access.key.secret
92159 std::string catalog; // the HRN of the catalog to which you to publish data
93160 std::string layer_id; // the of the layer inside the catalog to which you
94161 // want to publish data
95162
96- int examples_to_run = 0 ;
97- int opt, option_index;
98- while ((opt = getopt_long (argc, argv, " e:i:s:c:l:ah" , long_options,
99- &option_index)) != EOF)
100- switch (opt) {
101- case ' c' :
102- catalog = optarg;
103- break ;
104- case ' l' :
105- layer_id = optarg;
106- break ;
107- case ' i' :
108- access_key.id = optarg;
109- break ;
110- case ' s' :
111- access_key.secret = optarg;
112- break ;
113- case ' e' :
114- std::cout << " Run example " << optarg << std::endl;
115- if (strcmp (optarg, " read" ) == 0 )
116- examples_to_run = Examples::read_example;
117- else if (strcmp (optarg, " write" ) == 0 )
118- examples_to_run = Examples::write_example;
119- else if (strcmp (optarg, " cache" ) == 0 )
120- examples_to_run = Examples::cache_example;
121- else
122- std::cout
123- << " Example was not found. Please use values:read, write, cache"
124- << std::endl;
125- break ;
126- case ' a' :
127- examples_to_run = Examples::all_examples;
128- break ;
129- case ' h' :
130- std::cout << usage << std::endl;
131- return 0 ;
132- case ' ?' :
133- default :
134- fprintf (stderr, usage);
135- return 0 ;
136- }
163+ int examples_to_run =
164+ ParseArguments (argc, argv, access_key, catalog, layer_id);
165+ if (examples_to_run == 0 ) {
166+ return 0 ;
167+ }
137168
138169 if (access_key.id .empty () || access_key.secret .empty ()) {
139170 std::cout << " Please specify your access key ID and access key secret. For "
0 commit comments