Skip to content

Commit

Permalink
Merge pull request ceph#4208 from dmitryya/ceph_argparse_improvement
Browse files Browse the repository at this point in the history
ceph_argparse: Remove code duplication and generalize ceph_argparse_with* routines

Reviewed-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed Apr 1, 2015
2 parents 01b5085 + aa77a46 commit ba5bfce
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 109 deletions.
87 changes: 37 additions & 50 deletions src/common/ceph_argparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@
#undef generic_dout
#undef dendl

struct strict_str_convert {
const char *str;
std::string *err;
strict_str_convert(const char *str, std::string *err)
: str(str), err(err) {}

inline operator float() const
{
return strict_strtof(str, err);
}
inline operator int() const
{
return strict_strtol(str, 10, err);
}
inline operator long long() const
{
return strict_strtoll(str, 10, err);
}
};

void string_to_vec(std::vector<std::string>& args, std::string argstr)
{
istringstream iss(argstr);
Expand Down Expand Up @@ -314,19 +334,9 @@ static bool va_ceph_argparse_witharg(std::vector<const char*> &args,
}
}

template<class T>
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret, ...)
{
bool r;
va_list ap;
va_start(ap, ret);
r = va_ceph_argparse_witharg(args, i, ret, ap);
va_end(ap);
return r;
}

bool ceph_argparse_withint(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::vector<const char*>::iterator &i, T *ret,
std::ostream *oss, ...)
{
bool r;
Expand All @@ -340,58 +350,35 @@ bool ceph_argparse_withint(std::vector<const char*> &args,
}

std::string err;
int myret = strict_strtol(str.c_str(), 10, &err);
T myret = strict_str_convert(str.c_str(), &err);
*ret = myret;
if (!err.empty()) {
*oss << err;
}
return true;
}

bool ceph_argparse_withlonglong(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, long long *ret,
std::ostream *oss, ...)
{
bool r;
va_list ap;
std::string str;
va_start(ap, oss);
r = va_ceph_argparse_witharg(args, i, &str, ap);
va_end(ap);
if (!r) {
return false;
}
template bool ceph_argparse_witharg<int>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream *oss, ...);

std::string err;
long long myret = strict_strtoll(str.c_str(), 10, &err);
*ret = myret;
if (!err.empty()) {
*oss << err;
}
return true;
}
template bool ceph_argparse_witharg<long long>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, long long *ret,
std::ostream *oss, ...);

bool ceph_argparse_withfloat(std::vector<const char*> &args,
template bool ceph_argparse_witharg<float>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, float *ret,
std::ostream *oss, ...)
std::ostream *oss, ...);

bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret, ...)
{
bool r;
va_list ap;
std::string str;
va_start(ap, oss);
r = va_ceph_argparse_witharg(args, i, &str, ap);
va_start(ap, ret);
r = va_ceph_argparse_witharg(args, i, ret, ap);
va_end(ap);
if (!r) {
return false;
}

std::string err;
float myret = strict_strtof(str.c_str(), &err);
*ret = myret;
if (!err.empty()) {
*oss << err;
}
return true;
return r;
}

CephInitParameters ceph_argparse_early_args
Expand Down
13 changes: 4 additions & 9 deletions src/common/ceph_argparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,16 @@ bool ceph_argparse_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, ...);
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret, ...);
template<class T>
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, T *ret,
std::ostream *oss, ...);
bool ceph_argparse_binary_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream *oss, ...);
extern CephInitParameters ceph_argparse_early_args
(std::vector<const char*>& args, uint32_t module_type, int flags,
std::string *cluster, std::string *conf_file_list);
extern bool ceph_argparse_withint(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream *oss, ...);
extern bool ceph_argparse_withfloat(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, float *ret,
std::ostream *oss, ...);
extern bool ceph_argparse_withlonglong(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, long long *ret,
std::ostream *oss, ...);
extern void generic_server_usage();
extern void generic_client_usage();

Expand Down
16 changes: 8 additions & 8 deletions src/rbd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,7 @@ int main(int argc, const char **argv)
fromsnapname = strdup(val.c_str());
} else if (ceph_argparse_witharg(args, i, &val, "-i", "--image", (char*)NULL)) {
imgname = strdup(val.c_str());
} else if (ceph_argparse_withlonglong(args, i, &sizell, &err, "-s", "--size", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &sizell, &err, "-s", "--size", (char*)NULL)) {
if (!err.str().empty()) {
cerr << "rbd: " << err.str() << std::endl;
return EXIT_FAILURE;
Expand All @@ -2734,14 +2734,14 @@ int main(int argc, const char **argv)
size_set = true;
} else if (ceph_argparse_flag(args, i, "-l", "--long", (char*)NULL)) {
lflag = true;
} else if (ceph_argparse_withlonglong(args, i, &stripe_unit, &err, "--stripe-unit", (char*)NULL)) {
} else if (ceph_argparse_withlonglong(args, i, &stripe_count, &err, "--stripe-count", (char*)NULL)) {
} else if (ceph_argparse_withint(args, i, &order, &err, "--order", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &stripe_unit, &err, "--stripe-unit", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &stripe_count, &err, "--stripe-count", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &order, &err, "--order", (char*)NULL)) {
if (!err.str().empty()) {
cerr << "rbd: " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &bench_io_size, &err, "--io-size", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &bench_io_size, &err, "--io-size", (char*)NULL)) {
if (!err.str().empty()) {
cerr << "rbd: " << err.str() << std::endl;
return EXIT_FAILURE;
Expand All @@ -2750,9 +2750,9 @@ int main(int argc, const char **argv)
cerr << "rbd: io-size must be > 0" << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &bench_io_threads, &err, "--io-threads", (char*)NULL)) {
} else if (ceph_argparse_withlonglong(args, i, &bench_bytes, &err, "--io-total", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &bench_pattern, &err, "--io-pattern", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &bench_io_threads, &err, "--io-threads", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &bench_bytes, &err, "--io-total", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &bench_pattern, "--io-pattern", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &val, "--path", (char*)NULL)) {
path = strdup(val.c_str());
} else if (ceph_argparse_witharg(args, i, &val, "--dest", (char*)NULL)) {
Expand Down
2 changes: 1 addition & 1 deletion src/rbd_replay/rbd-replay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, const char **argv) {
break;
} else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
pool_name = val;
} else if (ceph_argparse_withfloat(args, i, &latency_multiplier, &err, "--latency-multiplier",
} else if (ceph_argparse_witharg(args, i, &latency_multiplier, &err, "--latency-multiplier",
(char*)NULL)) {
if (!err.str().empty()) {
cerr << err.str() << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/rgw/rgw_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ int main(int argc, char **argv)
// do nothing
} else if (ceph_argparse_binary_flag(args, i, &system, NULL, "--system", (char*)NULL)) {
system_specified = true;
} else if (ceph_argparse_withlonglong(args, i, &tmp, &errs, "-a", "--auth-uid", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &tmp, &errs, "-a", "--auth-uid", (char*)NULL)) {
if (!errs.str().empty()) {
cerr << errs.str() << std::endl;
exit(EXIT_FAILURE);
Expand Down
10 changes: 5 additions & 5 deletions src/test/ceph_argparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ TEST(CephArgParse, WithInt) {
{
if (ceph_argparse_double_dash(bazstuff1.arr, i)) {
break;
} else if (ceph_argparse_withint(bazstuff1.arr, i, &foo, &err, "--foo", (char*)NULL)) {
} else if (ceph_argparse_witharg(bazstuff1.arr, i, &foo, &err, "--foo", (char*)NULL)) {
ASSERT_EQ(string(""), err.str());
} else if (ceph_argparse_withint(bazstuff1.arr, i, &bar, &err, "--bar", (char*)NULL)) {
} else if (ceph_argparse_witharg(bazstuff1.arr, i, &bar, &err, "--bar", (char*)NULL)) {
ASSERT_EQ(string(""), err.str());
}
else {
Expand All @@ -306,7 +306,7 @@ TEST(CephArgParse, WithInt) {
{
if (ceph_argparse_double_dash(bazstuff2.arr, i)) {
break;
} else if (ceph_argparse_withint(bazstuff2.arr, i, &foo, &err2, "--foo", (char*)NULL)) {
} else if (ceph_argparse_witharg(bazstuff2.arr, i, &foo, &err2, "--foo", (char*)NULL)) {
ASSERT_NE(string(""), err2.str());
}
else {
Expand All @@ -322,9 +322,9 @@ TEST(CephArgParse, WithInt) {
{
if (ceph_argparse_double_dash(bazstuff3.arr, i)) {
break;
} else if (ceph_argparse_withint(bazstuff3.arr, i, &foo, &err, "--foo", (char*)NULL)) {
} else if (ceph_argparse_witharg(bazstuff3.arr, i, &foo, &err, "--foo", (char*)NULL)) {
ASSERT_EQ(string(""), err.str());
} else if (ceph_argparse_withint(bazstuff3.arr, i, &bar, &err, "--bar", (char*)NULL)) {
} else if (ceph_argparse_witharg(bazstuff3.arr, i, &bar, &err, "--bar", (char*)NULL)) {
ASSERT_EQ(string(""), err.str());
}
else {
Expand Down
14 changes: 7 additions & 7 deletions src/test/osdc/object_cacher_stress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,37 +187,37 @@ int main(int argc, const char **argv)
std::ostringstream err;
std::vector<const char*>::iterator i;
for (i = args.begin(); i != args.end();) {
if (ceph_argparse_withlonglong(args, i, &delay_ns, &err, "--delay-ns", (char*)NULL)) {
if (ceph_argparse_witharg(args, i, &delay_ns, &err, "--delay-ns", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &num_ops, &err, "--ops", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &num_ops, &err, "--ops", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &num_objs, &err, "--objects", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &num_objs, &err, "--objects", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &obj_bytes, &err, "--obj-size", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &obj_bytes, &err, "--obj-size", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withlonglong(args, i, &max_len, &err, "--max-op-size", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &max_len, &err, "--max-op-size", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withfloat(args, i, &percent_reads, &err, "--percent-read", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &percent_reads, &err, "--percent-read", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
}
} else if (ceph_argparse_withint(args, i, &seed, &err, "--seed", (char*)NULL)) {
} else if (ceph_argparse_witharg(args, i, &seed, &err, "--seed", (char*)NULL)) {
if (!err.str().empty()) {
cerr << argv[0] << ": " << err.str() << std::endl;
return EXIT_FAILURE;
Expand Down
Loading

0 comments on commit ba5bfce

Please sign in to comment.