3636# pragma warning(disable : 4221) /* Local var in declared initializer */
3737#endif
3838
39+ #define ELASTICURL_VERSION "0.2.0"
40+
3941struct elasticurl_ctx {
4042 struct aws_allocator * allocator ;
4143 const char * verb ;
@@ -60,7 +62,7 @@ struct elasticurl_ctx {
6062 bool exchange_completed ;
6163};
6264
63- static void s_usage (void ) {
65+ static void s_usage (int exit_code ) {
6466
6567 fprintf (stderr , "usage: elasticurl [options] url\n" );
6668 fprintf (stderr , " url: url to make a request to. The default is a GET request.\n" );
@@ -81,10 +83,11 @@ static void s_usage(void) {
8183 fprintf (stderr , " -k, --insecure: turns off SSL/TLS validation.\n" );
8284 fprintf (stderr , " -o, --output FILE: dumps content-body to FILE instead of stdout.\n" );
8385 fprintf (stderr , " -t, --trace FILE: dumps logs to FILE instead of stderr.\n" );
84- fprintf (stderr , " -v, --verbose ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.\n" );
86+ fprintf (stderr , " -v, --verbose: ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.\n" );
87+ fprintf (stderr , " --version: print the version of elasticurl.\n" );
8588 fprintf (stderr , " -h, --help\n" );
8689 fprintf (stderr , " Display this message and quit.\n" );
87- exit (1 );
90+ exit (exit_code );
8891}
8992
9093static struct aws_cli_option s_long_options [] = {
@@ -105,6 +108,7 @@ static struct aws_cli_option s_long_options[] = {
105108 {"output" , AWS_CLI_OPTIONS_REQUIRED_ARGUMENT , NULL , 'o' },
106109 {"trace" , AWS_CLI_OPTIONS_REQUIRED_ARGUMENT , NULL , 't' },
107110 {"verbose" , AWS_CLI_OPTIONS_REQUIRED_ARGUMENT , NULL , 'v' },
111+ {"version" , AWS_CLI_OPTIONS_NO_ARGUMENT , NULL , 'V' },
108112 {"help" , AWS_CLI_OPTIONS_NO_ARGUMENT , NULL , 'h' },
109113 /* Per getopt(3) the last element of the array has to be filled with all zeros */
110114 {NULL , AWS_CLI_OPTIONS_NO_ARGUMENT , NULL , 0 },
@@ -113,7 +117,7 @@ static struct aws_cli_option s_long_options[] = {
113117static void s_parse_options (int argc , char * * argv , struct elasticurl_ctx * ctx ) {
114118 while (true) {
115119 int option_index = 0 ;
116- int c = aws_cli_getopt_long (argc , argv , "a:b:c:e:f:H:d:g:M:GPHiko:t:v:h " , s_long_options , & option_index );
120+ int c = aws_cli_getopt_long (argc , argv , "a:b:c:e:f:H:d:g:M:GPHiko:t:v:Vh " , s_long_options , & option_index );
117121 if (c == -1 ) {
118122 break ;
119123 }
@@ -140,7 +144,7 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
140144 case 'H' :
141145 if (ctx -> header_line_count >= sizeof (ctx -> header_lines ) / sizeof (const char * )) {
142146 fprintf (stderr , "currently only 10 header lines are supported.\n" );
143- s_usage ();
147+ s_usage (1 );
144148 }
145149 ctx -> header_lines [ctx -> header_line_count ++ ] = aws_cli_optarg ;
146150 break ;
@@ -152,7 +156,7 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
152156 ctx -> data_file = fopen (aws_cli_optarg , "rb" );
153157 if (!ctx -> data_file ) {
154158 fprintf (stderr , "unable to open file %s.\n" , aws_cli_optarg );
155- s_usage ();
159+ s_usage (1 );
156160 }
157161 break ;
158162 case 'M' :
@@ -178,7 +182,7 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
178182
179183 if (!ctx -> output ) {
180184 fprintf (stderr , "unable to open file %s.\n" , aws_cli_optarg );
181- s_usage ();
185+ s_usage (1 );
182186 }
183187 break ;
184188 case 't' :
@@ -195,15 +199,18 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
195199 ctx -> log_level = AWS_LL_ERROR ;
196200 } else {
197201 fprintf (stderr , "unsupported log level %s.\n" , aws_cli_optarg );
198- s_usage ();
202+ s_usage (1 );
199203 }
200204 break ;
205+ case 'V' :
206+ fprintf (stderr , "elasticurl %s\n" , ELASTICURL_VERSION );
207+ exit (0 );
201208 case 'h' :
202- s_usage ();
209+ s_usage (0 );
203210 break ;
204211 default :
205212 fprintf (stderr , "Unknown option\n" );
206- s_usage ();
213+ s_usage (1 );
207214 }
208215 }
209216
@@ -216,11 +223,11 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
216223 "Failed to parse uri %s with error %s\n" ,
217224 (char * )uri_cursor .ptr ,
218225 aws_error_debug_str (aws_last_error ()));
219- s_usage ();
226+ s_usage (1 );
220227 };
221228 } else {
222229 fprintf (stderr , "A URI for the request must be supplied.\n" );
223- s_usage ();
230+ s_usage (1 );
224231 }
225232}
226233
0 commit comments