Skip to content

Commit

Permalink
add cmdl_test()
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgjogh committed Feb 27, 2016
1 parent e10519f commit a848e31
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
9 changes: 5 additions & 4 deletions libsim/sim_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ int enum_val_2_idx(int nenum, const opt_enum_t* enums, int val)
return i;
}
}
return 0;
return -1;
}

const char *enum_val_2_name(int nenum, const opt_enum_t* enums, int val)
Expand Down Expand Up @@ -472,7 +472,7 @@ int cmdl_parse_str (cmdl_iter_t *iter, void* dst, cmdl_act_t act, cmdl_opt_t *
}
else if (act == CMDL_ACT_RESULT)
{
xprint("'%s", *((char **)dst));
xprint("'%s'", *((char **)dst));
}
else if (act == CMDL_ACT_ARGFMT) {
xprint("<%%s>");
Expand Down Expand Up @@ -946,8 +946,9 @@ int cmdl_result(cmdl_iter_t *iter, void* dst, int optc, cmdl_opt_t optv[])
xlprint(iter->layer, "-%s = ", opt->names);
opt->parse(iter, (char *)dst + opt->dst_offset, CMDL_ACT_RESULT, opt);

//Caution: Define the optv[] in global scope. Or else the following
// way to get ref.name or enum.name would make no sense.
//FIXME: The following way to get ref.name or enum.name make no sense when:
// 1. optv[] was defined in local scope.
// 2. optv[] was reused by diff params.
if (opt->i_ref >= 0 && opt->i_ref < opt->nref) {
xprint(" (%%%s)", opt->refs[opt->i_ref].name);
}
Expand Down
4 changes: 4 additions & 0 deletions libsim/sim_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ extern "C" {
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif

#ifndef ARRAY_TUPLE
#define ARRAY_TUPLE(a) ARRAY_SIZE(a), a
#endif

#ifndef MAX
#define MAX(a,b) ((a)>(b) ? (a) : (b))
#endif
Expand Down
70 changes: 66 additions & 4 deletions ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,73 @@ int zhash_test(int argc, char** argv)
}


const opt_enum_t color_enum[] =
{
{"white", 0},
{"black", 1},
{"green", 2},
{"yellow", 6},
};

typedef struct pet{
char *name;
char *color;
int icolor;
int color; /* @see color_t */
int age;
}pet_t;
} pet_t;

#define PET_OPT_M(member) offsetof(pet_t, member)
cmdl_opt_t pet_opt[] =
{
{ 1, "name", 1, cmdl_parse_str, PET_OPT_M(name), 0, "pet's name", },
{ 0, "color", 1, cmdl_parse_int, PET_OPT_M(color), "%white", "pet's color", },
{ 0, "age", 1, cmdl_parse_int, PET_OPT_M(age), "0", "pet's age", },
};

int cmdl_pet_parser(cmdl_iter_t *iter, void* dst, cmdl_act_t act, cmdl_opt_t *opt)
{
cmdl_set_enum(ARRAY_TUPLE(pet_opt), "color", ARRAY_TUPLE(color_enum));

if (act == CMDL_ACT_PARSE) {
return cmdl_parse(iter, dst, ARRAY_TUPLE(pet_opt));
}
else if (act == CMDL_ACT_HELP) {
return cmdl_help(iter, 0, ARRAY_TUPLE(pet_opt));
}
else if (act == CMDL_ACT_RESULT) {
return cmdl_result(iter, dst, ARRAY_TUPLE(pet_opt));
}

return 0;
}

int cmdl_test(int argc, char **argv)
{
typedef struct cmdl_param {
pet_t cat, dog;
} cmdl_param_t;

#define CMDL_OPT_M(member) offsetof(cmdl_param_t, member)
cmdl_opt_t cmdl_opt[] =
{
{ 0, "h,help", 0, cmdl_parse_help, 0, 0, "show help"},
{ 1, "dog", 1, cmdl_pet_parser, CMDL_OPT_M(dog), 0, "dog's prop...", },
{ 1, "cat", 1, cmdl_pet_parser, CMDL_OPT_M(cat), 0, "cat's prop...", },
};

cmdl_param_t cfg;
cmdl_iter_t iter = cmdl_iter_init(argc, argv, 0);
int r = cmdl_parse(&iter, &cfg, ARRAY_TUPLE(cmdl_opt));
if (r == CMDL_RET_HELP) {
return cmdl_help(&iter, 0, ARRAY_TUPLE(cmdl_opt));
} else if (r < 0) {
xerr("cmdl_parse() failed, ret=%d\n", r);
return 1;
}

cmdl_result(&iter, &cfg, ARRAY_TUPLE(cmdl_opt));

return 0;
}

/*
int zopt_test(int argc, char **argv)
Expand Down Expand Up @@ -358,13 +419,14 @@ int main(int argc, char **argv)
const static yuv_module_t sub_main[] = {
{"list", zlist_test, ""},
//{"opt", zopt_test, ""},
{"cmdl", cmdl_test, ""},
{"array", zarray_test, ""},
{"strq", zstrq_test, ""},
{"hash", zhash_test, ""},
{"zhtree", zhtree_test, ""},
};

xlog_init(SLOG_DBG-1);
xlog_init(SLOG_DEFULT);
i = arg_parse_xlevel(1, argc, argv);

xdbg("@cmdl>> argv[%d] = %s\n", i, i<argc ? argv[i] : "?");
Expand Down

0 comments on commit a848e31

Please sign in to comment.