mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
323 lines
8.1 KiB
323 lines
8.1 KiB
2 years ago
|
//
|
||
|
//
|
||
|
// Copyright 2015 gRPC authors.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
//
|
||
|
//
|
||
10 years ago
|
|
||
7 months ago
|
#include "test/core/test_util/cmdline.h"
|
||
10 years ago
|
|
||
|
#include <limits.h>
|
||
|
#include <stdio.h>
|
||
2 years ago
|
#include <stdlib.h>
|
||
10 years ago
|
#include <string.h>
|
||
|
|
||
2 years ago
|
#include <algorithm>
|
||
5 years ago
|
#include <vector>
|
||
|
|
||
8 months ago
|
#include "absl/log/check.h"
|
||
5 years ago
|
#include "absl/strings/str_cat.h"
|
||
|
#include "absl/strings/str_format.h"
|
||
|
#include "absl/strings/str_join.h"
|
||
|
|
||
10 years ago
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/log.h>
|
||
3 years ago
|
|
||
3 years ago
|
#include "src/core/lib/gprpp/memory.h"
|
||
10 years ago
|
|
||
|
typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
|
||
|
|
||
|
typedef struct arg {
|
||
7 years ago
|
const char* name;
|
||
|
const char* help;
|
||
10 years ago
|
argtype type;
|
||
7 years ago
|
void* value;
|
||
|
struct arg* next;
|
||
10 years ago
|
} arg;
|
||
|
|
||
|
struct gpr_cmdline {
|
||
7 years ago
|
const char* description;
|
||
|
arg* args;
|
||
|
const char* argv0;
|
||
10 years ago
|
|
||
7 years ago
|
const char* extra_arg_name;
|
||
|
const char* extra_arg_help;
|
||
|
void (*extra_arg)(void* user_data, const char* arg);
|
||
|
void* extra_arg_user_data;
|
||
10 years ago
|
|
||
7 years ago
|
int (*state)(gpr_cmdline* cl, char* arg);
|
||
|
arg* cur_arg;
|
||
9 years ago
|
|
||
|
int survive_failure;
|
||
10 years ago
|
};
|
||
|
|
||
4 years ago
|
static int normal_state(gpr_cmdline* cl, char* str);
|
||
10 years ago
|
|
||
7 years ago
|
gpr_cmdline* gpr_cmdline_create(const char* description) {
|
||
3 years ago
|
gpr_cmdline* cl = grpc_core::Zalloc<gpr_cmdline>();
|
||
10 years ago
|
|
||
|
cl->description = description;
|
||
|
cl->state = normal_state;
|
||
|
|
||
|
return cl;
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_cmdline_set_survive_failure(gpr_cmdline* cl) {
|
||
9 years ago
|
cl->survive_failure = 1;
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_cmdline_destroy(gpr_cmdline* cl) {
|
||
10 years ago
|
while (cl->args) {
|
||
7 years ago
|
arg* a = cl->args;
|
||
10 years ago
|
cl->args = a->next;
|
||
|
gpr_free(a);
|
||
|
}
|
||
|
gpr_free(cl);
|
||
|
}
|
||
|
|
||
7 years ago
|
static void add_arg(gpr_cmdline* cl, const char* name, const char* help,
|
||
|
argtype type, void* value) {
|
||
|
arg* a;
|
||
10 years ago
|
|
||
|
for (a = cl->args; a; a = a->next) {
|
||
8 months ago
|
CHECK_NE(strcmp(a->name, name), 0);
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
a = static_cast<arg*>(gpr_zalloc(sizeof(arg)));
|
||
10 years ago
|
a->name = name;
|
||
|
a->help = help;
|
||
|
a->type = type;
|
||
|
a->value = value;
|
||
|
a->next = cl->args;
|
||
|
cl->args = a;
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_cmdline_add_int(gpr_cmdline* cl, const char* name, const char* help,
|
||
|
int* value) {
|
||
10 years ago
|
add_arg(cl, name, help, ARGTYPE_INT, value);
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_cmdline_add_flag(gpr_cmdline* cl, const char* name, const char* help,
|
||
|
int* value) {
|
||
10 years ago
|
add_arg(cl, name, help, ARGTYPE_BOOL, value);
|
||
|
}
|
||
|
|
||
7 years ago
|
void gpr_cmdline_add_string(gpr_cmdline* cl, const char* name, const char* help,
|
||
7 years ago
|
const char** value) {
|
||
10 years ago
|
add_arg(cl, name, help, ARGTYPE_STRING, value);
|
||
|
}
|
||
|
|
||
|
void gpr_cmdline_on_extra_arg(
|
||
7 years ago
|
gpr_cmdline* cl, const char* name, const char* help,
|
||
|
void (*on_extra_arg)(void* user_data, const char* arg), void* user_data) {
|
||
8 months ago
|
CHECK(!cl->extra_arg);
|
||
|
CHECK(on_extra_arg);
|
||
10 years ago
|
|
||
|
cl->extra_arg = on_extra_arg;
|
||
|
cl->extra_arg_user_data = user_data;
|
||
|
cl->extra_arg_name = name;
|
||
|
cl->extra_arg_help = help;
|
||
|
}
|
||
|
|
||
2 years ago
|
// recursively descend argument list, adding the last element
|
||
|
// to s first - so that arguments are added in the order they were
|
||
|
// added to the list by api calls
|
||
5 years ago
|
static void add_args_to_usage(arg* a, std::vector<std::string>* s) {
|
||
|
if (a == nullptr) return;
|
||
|
add_args_to_usage(a->next, s);
|
||
10 years ago
|
switch (a->type) {
|
||
|
case ARGTYPE_BOOL:
|
||
5 years ago
|
s->push_back(absl::StrFormat(" [--%s|--no-%s]", a->name, a->name));
|
||
10 years ago
|
break;
|
||
|
case ARGTYPE_STRING:
|
||
5 years ago
|
s->push_back(absl::StrFormat(" [--%s=string]", a->name));
|
||
10 years ago
|
break;
|
||
|
case ARGTYPE_INT:
|
||
5 years ago
|
s->push_back(absl::StrFormat(" [--%s=int]", a->name));
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
std::string gpr_cmdline_usage_string(gpr_cmdline* cl, const char* argv0) {
|
||
7 years ago
|
const char* name = strrchr(argv0, '/');
|
||
5 years ago
|
if (name != nullptr) {
|
||
10 years ago
|
name++;
|
||
|
} else {
|
||
10 years ago
|
name = argv0;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
std::vector<std::string> s;
|
||
|
s.push_back(absl::StrCat("Usage: ", name));
|
||
|
add_args_to_usage(cl->args, &s);
|
||
10 years ago
|
if (cl->extra_arg) {
|
||
5 years ago
|
s.push_back(absl::StrFormat(" [%s...]", cl->extra_arg_name));
|
||
10 years ago
|
}
|
||
5 years ago
|
s.push_back("\n");
|
||
|
return absl::StrJoin(s, "");
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
static int print_usage_and_die(gpr_cmdline* cl) {
|
||
5 years ago
|
fprintf(stderr, "%s", gpr_cmdline_usage_string(cl, cl->argv0).c_str());
|
||
9 years ago
|
if (!cl->survive_failure) {
|
||
|
exit(1);
|
||
|
}
|
||
|
return 0;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
static int extra_state(gpr_cmdline* cl, char* str) {
|
||
9 years ago
|
if (!cl->extra_arg) {
|
||
|
return print_usage_and_die(cl);
|
||
|
}
|
||
9 years ago
|
cl->extra_arg(cl->extra_arg_user_data, str);
|
||
9 years ago
|
return 1;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
static arg* find_arg(gpr_cmdline* cl, char* name) {
|
||
|
arg* a;
|
||
10 years ago
|
|
||
|
for (a = cl->args; a; a = a->next) {
|
||
|
if (0 == strcmp(a->name, name)) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!a) {
|
||
|
fprintf(stderr, "Unknown argument: %s\n", name);
|
||
7 years ago
|
return nullptr;
|
||
10 years ago
|
}
|
||
|
|
||
|
return a;
|
||
|
}
|
||
|
|
||
7 years ago
|
static int value_state(gpr_cmdline* cl, char* str) {
|
||
10 years ago
|
long intval;
|
||
7 years ago
|
char* end;
|
||
10 years ago
|
|
||
8 months ago
|
CHECK(cl->cur_arg);
|
||
10 years ago
|
|
||
|
switch (cl->cur_arg->type) {
|
||
|
case ARGTYPE_INT:
|
||
9 years ago
|
intval = strtol(str, &end, 0);
|
||
10 years ago
|
if (*end || intval < INT_MIN || intval > INT_MAX) {
|
||
9 years ago
|
fprintf(stderr, "expected integer, got '%s' for %s\n", str,
|
||
10 years ago
|
cl->cur_arg->name);
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
10 years ago
|
}
|
||
7 years ago
|
*static_cast<int*>(cl->cur_arg->value) = static_cast<int>(intval);
|
||
10 years ago
|
break;
|
||
|
case ARGTYPE_BOOL:
|
||
9 years ago
|
if (0 == strcmp(str, "1") || 0 == strcmp(str, "true")) {
|
||
7 years ago
|
*static_cast<int*>(cl->cur_arg->value) = 1;
|
||
9 years ago
|
} else if (0 == strcmp(str, "0") || 0 == strcmp(str, "false")) {
|
||
7 years ago
|
*static_cast<int*>(cl->cur_arg->value) = 0;
|
||
10 years ago
|
} else {
|
||
9 years ago
|
fprintf(stderr, "expected boolean, got '%s' for %s\n", str,
|
||
10 years ago
|
cl->cur_arg->name);
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
10 years ago
|
}
|
||
|
break;
|
||
|
case ARGTYPE_STRING:
|
||
7 years ago
|
*static_cast<char**>(cl->cur_arg->value) = str;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
|
||
|
cl->state = normal_state;
|
||
9 years ago
|
return 1;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
static int normal_state(gpr_cmdline* cl, char* str) {
|
||
7 years ago
|
char* eq = nullptr;
|
||
|
char* tmp = nullptr;
|
||
|
char* arg_name = nullptr;
|
||
9 years ago
|
int r = 1;
|
||
10 years ago
|
|
||
9 years ago
|
if (0 == strcmp(str, "-help") || 0 == strcmp(str, "--help") ||
|
||
|
0 == strcmp(str, "-h")) {
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
cl->cur_arg = nullptr;
|
||
10 years ago
|
|
||
9 years ago
|
if (str[0] == '-') {
|
||
|
if (str[1] == '-') {
|
||
|
if (str[2] == 0) {
|
||
2 years ago
|
// handle '--' to move to just extra args
|
||
10 years ago
|
cl->state = extra_state;
|
||
9 years ago
|
return 1;
|
||
10 years ago
|
}
|
||
9 years ago
|
str += 2;
|
||
10 years ago
|
} else {
|
||
9 years ago
|
str += 1;
|
||
10 years ago
|
}
|
||
2 years ago
|
// first byte of str is now past the leading '-' or '--'
|
||
9 years ago
|
if (str[0] == 'n' && str[1] == 'o' && str[2] == '-') {
|
||
2 years ago
|
// str is of the form '--no-foo' - it's a flag disable
|
||
9 years ago
|
str += 3;
|
||
|
cl->cur_arg = find_arg(cl, str);
|
||
7 years ago
|
if (cl->cur_arg == nullptr) {
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
|
}
|
||
10 years ago
|
if (cl->cur_arg->type != ARGTYPE_BOOL) {
|
||
9 years ago
|
fprintf(stderr, "%s is not a flag argument\n", str);
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
10 years ago
|
}
|
||
7 years ago
|
*static_cast<int*>(cl->cur_arg->value) = 0;
|
||
2 years ago
|
return 1; // early out
|
||
10 years ago
|
}
|
||
9 years ago
|
eq = strchr(str, '=');
|
||
7 years ago
|
if (eq != nullptr) {
|
||
2 years ago
|
// copy the string into a temp buffer and extract the name
|
||
7 years ago
|
tmp = arg_name =
|
||
|
static_cast<char*>(gpr_malloc(static_cast<size_t>(eq - str + 1)));
|
||
7 years ago
|
memcpy(arg_name, str, static_cast<size_t>(eq - str));
|
||
9 years ago
|
arg_name[eq - str] = 0;
|
||
10 years ago
|
} else {
|
||
9 years ago
|
arg_name = str;
|
||
10 years ago
|
}
|
||
|
cl->cur_arg = find_arg(cl, arg_name);
|
||
7 years ago
|
if (cl->cur_arg == nullptr) {
|
||
9 years ago
|
return print_usage_and_die(cl);
|
||
|
}
|
||
7 years ago
|
if (eq != nullptr) {
|
||
2 years ago
|
// str was of the type --foo=value, parse the value
|
||
9 years ago
|
r = value_state(cl, eq + 1);
|
||
10 years ago
|
} else if (cl->cur_arg->type != ARGTYPE_BOOL) {
|
||
2 years ago
|
// flag types don't have a '--foo value' variant, other types do
|
||
10 years ago
|
cl->state = value_state;
|
||
|
} else {
|
||
2 years ago
|
// flag parameter: just set the value
|
||
7 years ago
|
*static_cast<int*>(cl->cur_arg->value) = 1;
|
||
10 years ago
|
}
|
||
|
} else {
|
||
9 years ago
|
r = extra_state(cl, str);
|
||
10 years ago
|
}
|
||
|
|
||
|
gpr_free(tmp);
|
||
9 years ago
|
return r;
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
int gpr_cmdline_parse(gpr_cmdline* cl, int argc, char** argv) {
|
||
10 years ago
|
int i;
|
||
|
|
||
8 months ago
|
CHECK_GE(argc, 1);
|
||
10 years ago
|
cl->argv0 = argv[0];
|
||
|
|
||
|
for (i = 1; i < argc; i++) {
|
||
9 years ago
|
if (!cl->state(cl, argv[i])) {
|
||
|
return 0;
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
return 1;
|
||
10 years ago
|
}
|