-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move cmdl_test() to libsim/sim_test.c
- Loading branch information
Showing
3 changed files
with
61 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,72 @@ | ||
#include "sim_opt.h" | ||
|
||
int main(int argc, char **argv) | ||
const opt_enum_t color_enum[] = | ||
{ | ||
xlog_init(SLOG_DEFULT); | ||
{"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 cmdl_parse(iter, dst, pet_opt); | ||
} | ||
else if (act == CMDL_ACT_HELP) { | ||
return cmdl_help(iter, 0, pet_opt); | ||
} | ||
else if (act == CMDL_ACT_RESULT) { | ||
return cmdl_result(iter, dst, pet_opt); | ||
} | ||
|
||
typedef struct pet{ | ||
char *name; | ||
int color; /* @see color_t */ | ||
int age; | ||
} pet_t; | ||
return 0; | ||
} | ||
|
||
int cmdl_test(int argc, char **argv) | ||
{ | ||
typedef struct cmdl_param { | ||
pet_t cat, dog; | ||
} cmdl_param_t; | ||
|
||
#define PET_OPT_M(member) offsetof(pet_t, member) | ||
cmdl_opt_t pet_opt[] = { | ||
#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, "name", 1, cmdl_parse_str, PET_OPT_M(name), 0, "pet's name", }, | ||
{ 0, "color", 1, cmdl_parse_int, PET_OPT_M(color), "0", "pet's color", }, | ||
{ 0, "age", 1, cmdl_parse_int, PET_OPT_M(age), "0", "pet's age", }, | ||
{ 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, }, | ||
}; | ||
|
||
pet_t cfg; | ||
cmdl_param_t cfg; | ||
cmdl_iter_t iter = cmdl_iter_init(argc, argv, 0); | ||
int r = cmdl_parse(&iter, &cfg, ARRAY_TUPLE(pet_opt)); | ||
int r = cmdl_parse(&iter, &cfg, cmdl_opt); | ||
if (r == CMDL_RET_HELP) { | ||
return cmdl_help(&iter, 0, ARRAY_TUPLE(pet_opt)); | ||
return cmdl_help(&iter, 0, cmdl_opt); | ||
} else if (r < 0) { | ||
xerr("cmdl_parse() failed, ret=%d\n", r); | ||
return 1; | ||
} | ||
|
||
cmdl_result(&iter, &cfg, ARRAY_TUPLE(pet_opt)); | ||
|
||
cmdl_result(&iter, &cfg, cmdl_opt); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters