Skip to content

Commit

Permalink
add cmdlgrp_default_entry()
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgjogh committed Jan 31, 2017
1 parent 92a7e64 commit 68c8bec
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 73 deletions.
21 changes: 20 additions & 1 deletion libsim/sim_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,15 @@ int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt
int fieldCount = 0;
char *fieldArr[256] = {cmdl_iter_curr(iter)};
char fieldBuf[1024] = {0};

ENTER_FUNC();

char *arg = cmdl_peek_next(iter);
xlog_cmdl("[L%d] parse_opt(-%s) : %s\n", iter->layer, opt->names, SAFE_STR(arg, "?"));

if (arg && arg[0] == '%')
{
/* expand ref or enum */
xdbg("expand ref or enum '%s'\n", arg);
if (opt->refs) {
opt->p_ref = sim_name_2_ref(opt->refs, &arg[1]);
if (opt->p_ref == 0) {
Expand Down Expand Up @@ -718,6 +720,8 @@ int cmdl_parse_opt(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t* opt
return r;
}

LEAVE_FUNC();

return (arg && arg[0] == '%') ? 1 : r;
}

Expand Down Expand Up @@ -946,3 +950,18 @@ int cmdlgrp_print_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])

return 0;
}

int cmdlgrp_default_entry(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t optv[])
{
if (act == CMDL_ACT_PARSE) {
return cmdlgrp_parse(iter, dst, optv);
}
else if (act == CMDL_PRI_HELP) {
return cmdlgrp_print_help(iter, 0, optv);
}
else if (act == CMDL_PRI_RESULT) {
return cmdlgrp_print_result(iter, dst, optv);
}

return 0;
}
4 changes: 4 additions & 0 deletions libsim/sim_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ cmdl_opt_t* cmdl_set_ref (cmdl_opt_t optv[], const char *opt_name, const cmdl_re
cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const cmdl_enum_t *enums);


/**
* default implentation for group options
*/
int cmdlgrp_parse(cmdl_iter_t *iter, void *dst, cmdl_opt_t optv[]);
int cmdlgrp_print_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[]);
int cmdlgrp_print_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[]);
int cmdlgrp_default_entry(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t optv[]);


#ifdef __cplusplus
Expand Down
134 changes: 62 additions & 72 deletions libsim/sim_test.c
Original file line number Diff line number Diff line change
@@ -1,72 +1,62 @@
#include "sim_opt.h"

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

typedef struct pet{
char *name;
int color; /* @see color_t */
int age;
} 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", },
{ 0, SIM_NULL_END },
};

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

if (act == CMDL_ACT_PARSE) {
return cmdlgrp_parse(iter, dst, pet_opt);
}
else if (act == CMDL_PRI_HELP) {
return cmdlgrp_print_help(iter, 0, pet_opt);
}
else if (act == CMDL_PRI_RESULT) {
return cmdlgrp_print_result(iter, dst, 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...", },
{ 0, SIM_NULL_END, },
};

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

cmdlgrp_print_result(&iter, &cfg, cmdl_opt);

return 0;
}
#include "sim_opt.h"

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

typedef struct pet{
char *name;
int color; /* @see color_t */
int age;
} 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", },
{ 0, SIM_NULL_END },
};

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

return cmdlgrp_default_entry(iter, dst, act, pet_opt);
}

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...", },
{ 0, SIM_NULL_END, },
};

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

cmdlgrp_default_entry(&iter, &cfg, CMDL_PRI_RESULT, cmdl_opt);

return 0;
}

0 comments on commit 68c8bec

Please sign in to comment.