Skip to content

Commit

Permalink
move cmdl_test() to libsim/sim_test.c
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgjogh committed Jan 30, 2017
1 parent 1e4eef0 commit 36af342
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 94 deletions.
4 changes: 2 additions & 2 deletions libsim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CFLAGS = -c -O3
LIBS = -lm

TMPDIR = mk.tmp
LIBSIMSRCS = sim_log.c sim_utils.c sim_opt.c
LIBSIMSRCS = sim_log.c sim_utils.c sim_opt.c sim_test.c
LIBSIMOBJS = $(LIBSIMSRCS:%.c=$(TMPDIR)/%.o)
LIBSIM = libsim.a

Expand All @@ -31,4 +31,4 @@ $(LIBSIMOBJS): $(TMPDIR)/%.o:%.c | $(TMPDIR)
$(CC) $(CFLAGS) -I$(LIBSIMDIRS) -o $@ $<

$(TMPDIR):
mkdir -p $@
mkdir -p $@
78 changes: 58 additions & 20 deletions libsim/sim_test.c
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;
}
}
73 changes: 1 addition & 72 deletions ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,78 +240,6 @@ int zhash_test(int argc, char** argv)
return 0;
}


const opt_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 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);
}

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 = cmdl_parse(&iter, &cfg, cmdl_opt);
if (r == CMDL_RET_HELP) {
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, cmdl_opt);

return 0;
}

/*
int zopt_test(int argc, char **argv)
{
Expand Down Expand Up @@ -413,6 +341,7 @@ int main(int argc, char **argv)
{
int i=0, j = 0;
int exit_code = 0;
int cmdl_test(int argc, char **argv);

typedef struct yuv_module {
const char *name;
Expand Down

0 comments on commit 36af342

Please sign in to comment.