-
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.
- Loading branch information
Showing
3 changed files
with
86 additions
and
73 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
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,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; | ||
} |