14 #include "zclk_common.h"
16 #include <coll_arraylist.h>
23 #include "zclk_table.h"
24 #include "zclk_dict.h"
25 #include "zclk_progress.h"
32 #define ZCLK_OPTION_HELP_LONG "help"
34 #define ZCLK_OPTION_HELP_SHORT "h"
36 #define ZCLK_OPTION_HELP_DESC "Print help for command."
44 ZCLK_RES_IS_RUNNING = -1,
46 ZCLK_RES_ERR_UNKNOWN = 1,
47 ZCLK_RES_ERR_ALLOC_FAILED = 2,
48 ZCLK_RES_ERR_COMMAND_NOT_FOUND = 3,
49 ZCLK_RES_ERR_OPTION_NOT_FOUND = 4,
50 ZCLK_RES_ERR_ARG_NOT_FOUND = 5,
51 ZCLK_RES_ERR_EXTRA_ARGS_FOUND = 6
60 ZCLK_TYPE_BOOLEAN = 0,
67 #define ZCLK_BOOL_TRUE 1
68 #define ZCLK_BOOL_FALSE 0
69 #define ZCLK_FLAG_ON 1
70 #define ZCLK_FLAG_OFF 0
78 ZCLK_RESULT_STRING = 0,
79 ZCLK_RESULT_TABLE = 1,
81 ZCLK_RESULT_PROGRESS = 3
107 #define zclk_val_is_bool(val) zclk_val_is_type(val, ZCLK_TYPE_BOOLEAN)
115 #define zclk_val_is_int(val) zclk_val_is_type(val, ZCLK_TYPE_INT)
123 #define zclk_val_is_double(val) zclk_val_is_type(val, ZCLK_TYPE_DOUBLE)
131 #define zclk_val_is_string(val) zclk_val_is_type(val, ZCLK_TYPE_STRING)
139 #define zclk_val_is_flag(val) zclk_val_is_type(val, ZCLK_TYPE_FLAG)
229 MODULE_API
int zclk_val_to_lua(lua_State *L,
zclk_val* val);
277 #define zclk_flag(v) new_zclk_val_flag(v)
284 #define zclk_boolean(v) new_zclk_val_bool(v)
291 #define zclk_int(v) new_zclk_val_int(v)
298 #define zclk_double(v) new_zclk_val_double(v)
305 #define zclk_string(v) new_zclk_val_string(v)
327 MODULE_API
int zclk_option_to_lua(lua_State *L,
zclk_option* option);
336 MODULE_API
void arraylist_zclk_option_to_lua(lua_State *L,
int index,
void *data);
359 MODULE_API
int zclk_argument_to_lua(lua_State *L,
zclk_argument* arg);
368 MODULE_API
void arraylist_zclk_argument_to_lua(lua_State *L,
int index,
void *data);
466 const char* description);
487 MODULE_API
zclk_option *new_zclk_option_bool(
const char *name,
488 const char *short_name,
int default_val,
const char *desc);
489 MODULE_API
zclk_option *new_zclk_option_int(
const char *name,
490 const char *short_name,
int default_val,
const char *desc);
491 MODULE_API
zclk_option *new_zclk_option_double(
const char *name,
492 const char *short_name,
double default_val,
const char *desc);
493 MODULE_API
zclk_option *new_zclk_option_string(
const char *name,
494 const char *short_name,
const char* default_val,
496 MODULE_API
zclk_option *new_zclk_option_flag(
const char *name,
497 const char *short_name,
int default_val,
const char *desc);
499 MODULE_API
const char *zclk_option_get_name(
zclk_option *opt);
500 MODULE_API
const char *zclk_option_get_short_name(
zclk_option *opt);
501 MODULE_API
const char *zclk_option_get_desc(
zclk_option *opt);
503 MODULE_API
int zclk_option_get_val_bool(
zclk_option *opt);
504 MODULE_API
int zclk_option_get_val_int(
zclk_option *opt);
505 MODULE_API
double zclk_option_get_val_double(
zclk_option *opt);
506 MODULE_API
const char* zclk_option_get_val_string(
zclk_option *opt);
507 MODULE_API
int zclk_option_get_val_flag(
zclk_option *opt);
509 MODULE_API
int zclk_option_get_default_val_bool(
zclk_option *opt);
510 MODULE_API
int zclk_option_get_default_val_int(
zclk_option *opt);
511 MODULE_API
double zclk_option_get_default_val_double(
zclk_option *opt);
512 MODULE_API
const char* zclk_option_get_default_val_string(
zclk_option *opt);
513 MODULE_API
int zclk_option_get_default_val_flag(
zclk_option *opt);
566 zclk_val* default_val,
const char* desc,
int nargs);
568 MODULE_API
zclk_argument *new_zclk_argument_bool(
const char *name,
569 int default_val,
const char *desc,
int nargs);
570 MODULE_API
zclk_argument *new_zclk_argument_int(
const char *name,
571 int default_val,
const char *desc,
int nargs);
572 MODULE_API
zclk_argument *new_zclk_argument_double(
const char *name,
573 double default_val,
const char *desc,
int nargs);
574 MODULE_API
zclk_argument *new_zclk_argument_string(
const char *name,
575 const char* default_val,
const char *desc,
int nargs);
576 MODULE_API
zclk_argument *new_zclk_argument_flag(
const char *name,
577 int default_val,
const char *desc,
int nargs);
579 MODULE_API
const char *zclk_argument_get_name(
zclk_argument *opt);
580 MODULE_API
const char *zclk_argument_get_desc(
zclk_argument *opt);
582 MODULE_API
int zclk_argument_get_val_bool(
zclk_argument *opt);
583 MODULE_API
int zclk_argument_get_val_int(
zclk_argument *opt);
584 MODULE_API
double zclk_argument_get_val_double(
zclk_argument *opt);
585 MODULE_API
const char* zclk_argument_get_val_string(
zclk_argument *opt);
586 MODULE_API
int zclk_argument_get_val_flag(
zclk_argument *opt);
588 MODULE_API
int zclk_argument_get_default_val_bool(
zclk_argument *opt);
589 MODULE_API
int zclk_argument_get_default_val_int(
zclk_argument *opt);
590 MODULE_API
double zclk_argument_get_default_val_double(
zclk_argument *opt);
591 MODULE_API
const char* zclk_argument_get_default_val_string(
zclk_argument *opt);
592 MODULE_API
int zclk_argument_get_default_val_flag(
zclk_argument *opt);
613 const char* short_name,
const char* description,
zclk_command_fn handler);
627 const char* short_name,
628 const char* description,
677 const char* short_name,
const char *desc);
689 const char* short_name,
int default_val,
const char *desc);
701 const char* short_name,
double default_val,
const char *desc);
713 const char* short_name,
const char* default_val,
725 const char* short_name,
const char *desc);
737 int default_val,
const char *desc,
int nargs);
749 int default_val,
const char *desc,
int nargs);
761 double default_val,
const char *desc,
int nargs);
773 const char* default_val,
const char *desc,
int nargs);
785 int default_val,
const char *desc,
int nargs);
815 #define zclk_command_option_foreach(cmd, opt) \
816 zclk_option *opt = NULL; \
817 size_t len##opt = arraylist_length(cmd->options); \
818 for (size_t i = 0; ((i < len##opt)? \
819 (opt = (zclk_option *) \
820 (arraylist_get(cmd->options, i))) \
832 #define zclk_command_argument_foreach(cmd, arg) \
833 zclk_argument *arg = NULL; \
834 size_t len##arg = arraylist_length(cmd->args); \
835 for (size_t i = 0; ((i < len##arg)? \
836 (arg = (zclk_argument *) \
837 (arraylist_get(cmd->args, i))) \
853 int argc,
char *argv[]);
881 void* handler_args,
int argc,
char** argv,
893 arraylist* arg_commands);
906 int argc,
char** argv);
CLI Argument object.
Definition: zclk.h:343
char * description
textual description
Definition: zclk.h:347
zclk_val * default_val
default value of the argument
Definition: zclk.h:346
char * name
name of the argument
Definition: zclk.h:344
zclk_val * val
value of the argument
Definition: zclk.h:345
int optional
flag indicating if argument is optional
Definition: zclk.h:348
A CLI Command Ojbect.
Definition: zclk.h:398
arraylist * sub_commands
list of subcommands
Definition: zclk.h:402
zclk_command_output_handler error_handler
error handler for the command
Definition: zclk.h:407
arraylist * args
args list
Definition: zclk.h:404
int lua_udata_ref
lua ref for the udata of this object
Definition: zclk.h:411
zclk_command_output_handler success_handler
success handler for the command
Definition: zclk.h:409
char * short_name
short name of the command
Definition: zclk.h:400
arraylist * options
options list
Definition: zclk.h:403
char * name
name of the command
Definition: zclk.h:399
int lua_handler_ref
lua ref for handler
Definition: zclk.h:410
zclk_command_fn handler
command handler function
Definition: zclk.h:405
char * description
textual description
Definition: zclk.h:401
CLI Option Object.
Definition: zclk.h:311
char * name
name of the option
Definition: zclk.h:312
zclk_val * val
value of the option
Definition: zclk.h:314
char * description
textural description of the option
Definition: zclk.h:316
zclk_val * default_val
default value of the option
Definition: zclk.h:315
char * short_name
short_name of the option
Definition: zclk.h:313
This struct holds the value of the argument or option.
Definition: zclk.h:89
char * str_value
string value
Definition: zclk.h:95
int bool_value
boolean value
Definition: zclk.h:92
int int_value
integer value
Definition: zclk.h:93
union zclk_val_t::@0 data
data of the value
zclk_type type
type of value
Definition: zclk.h:90
double dbl_value
double value
Definition: zclk.h:94
MODULE_API void copy_zclk_val(zclk_val *to, zclk_val *from)
Definition: zclk.c:185
MODULE_API void zclk_command_flag_option(zclk_command *cmd, const char *name, const char *short_name, const char *desc)
Create a new flag option and add it to the given command.
Definition: zclk.c:930
MODULE_API char * get_help_for_command(arraylist *cmds_to_exec)
Definition: zclk.c:1129
struct zclk_argument_t zclk_argument
CLI Argument object.
MODULE_API void zclk_command_flag_argument(zclk_command *cmd, const char *name, int default_val, const char *desc, int nargs)
Create a new bool argument and add it to the given command.
Definition: zclk.c:990
zclk_result_type
This enum defines the possible types of result cli program might output.
Definition: zclk.h:77
MODULE_API void zclk_val_set_flag(zclk_val *val, int fval)
set the flag value
Definition: zclk.c:168
MODULE_API void free_option(zclk_option *option)
Definition: zclk.c:475
MODULE_API void free_command(zclk_command *command)
Definition: zclk.c:1061
struct zclk_option_t zclk_option
CLI Option Object.
MODULE_API zclk_res zclk_command_option_add(zclk_command *cmd, zclk_option *option)
Add an option to the given command.
Definition: zclk.c:851
MODULE_API zclk_option * zclk_command_get_option(zclk_command *cmd, const char *name)
Get the option object corresponding to given name.
Definition: zclk.c:1001
MODULE_API zclk_res parse_zclk_val(zclk_val *val, char *input)
Definition: zclk.c:210
MODULE_API void clear_zclk_val(zclk_val *val)
MODULE_API zclk_res make_command(zclk_command **command, const char *name, const char *short_name, const char *description, zclk_command_fn handler)
Definition: zclk.c:790
MODULE_API zclk_res make_zclk_val(zclk_val **val, zclk_type type)
Definition: zclk.c:29
MODULE_API zclk_res help_cmd_handler(arraylist *commands, void *handler_args, int argc, char **argv, zclk_command_output_handler success_handler, zclk_command_output_handler error_handler)
Definition: zclk.c:1329
MODULE_API void zclk_command_string_argument(zclk_command *cmd, const char *name, const char *default_val, const char *desc, int nargs)
Create a new bool argument and add it to the given command.
Definition: zclk.c:978
MODULE_API void zclk_command_int_argument(zclk_command *cmd, const char *name, int default_val, const char *desc, int nargs)
Create a new bool argument and add it to the given command.
Definition: zclk.c:954
MODULE_API zclk_command * new_zclk_command(const char *name, const char *short_name, const char *description, zclk_command_fn handler)
Create a command object with automatic error handling.
Definition: zclk.c:830
MODULE_API zclk_val * new_zclk_val_string(const char *string_val)
Create a new string value.
Definition: zclk.c:73
MODULE_API zclk_res get_help_for(char **help_str, arraylist *commands, arraylist *arg_commands)
Definition: zclk.c:1354
MODULE_API zclk_option * new_zclk_option(const char *name, const char *short_name, zclk_val *val, zclk_val *default_val, const char *desc)
(Internal Use) Create an option object
Definition: zclk.c:313
MODULE_API void zclk_command_int_option(zclk_command *cmd, const char *name, const char *short_name, int default_val, const char *desc)
Create a new int option and add it to the given command.
Definition: zclk.c:891
MODULE_API zclk_val * new_zclk_val_bool(int bool_val)
Create a new boolean value.
Definition: zclk.c:40
MODULE_API void free_zclk_val(zclk_val *val)
Definition: zclk.c:176
MODULE_API zclk_res zclk_command_argument_add(zclk_command *cmd, zclk_argument *arg)
Add an argument to the given command.
Definition: zclk.c:865
MODULE_API double zclk_val_get_double(zclk_val *val)
get the double value
Definition: zclk.c:122
MODULE_API zclk_res exec_command(arraylist *commands, void *handler_args, int argc, char **argv)
Definition: zclk.c:1562
MODULE_API zclk_res zclk_command_exec(zclk_command *cmd, void *exec_args, int argc, char *argv[])
Execute the command with the given args.
Definition: zclk.c:1038
MODULE_API zclk_res make_argument(zclk_argument **arg, const char *name, zclk_val *val, zclk_val *default_val, const char *desc)
Definition: zclk.c:565
struct zclk_val_t zclk_val
This struct holds the value of the argument or option.
zclk_res
This enum defines the possible error codes generated by functions in the API.
Definition: zclk.h:43
MODULE_API zclk_res print_handler(zclk_res result_flag, zclk_result_type res_type, void *result)
A Print handler prints the result of the command.
Definition: zclk.c:1776
MODULE_API int zclk_val_get_flag(zclk_val *val)
get the flag value
Definition: zclk.c:132
MODULE_API zclk_val * new_zclk_val_double(double double_val)
Create a new double value.
Definition: zclk.c:62
zclk_type
This enum defines the possible datatypes of argument or option values.
Definition: zclk.h:59
MODULE_API void zclk_val_set_int(zclk_val *val, int ival)
set the int value
Definition: zclk.c:145
MODULE_API int zclk_val_get_int(zclk_val *val)
get the int value
Definition: zclk.c:116
MODULE_API void zclk_command_bool_option(zclk_command *cmd, const char *name, const char *short_name, const char *desc)
Create a new boolean option and add it to the given command.
Definition: zclk.c:879
MODULE_API void free_argument(zclk_argument *arg)
Definition: zclk.c:739
MODULE_API void print_table_result(void *result)
Print a tabular result object to the stdout.
Definition: zclk.c:1688
MODULE_API void zclk_command_double_option(zclk_command *cmd, const char *name, const char *short_name, double default_val, const char *desc)
Create a new double option and add it to the given command.
Definition: zclk.c:904
MODULE_API int zclk_val_get_bool(zclk_val *val)
get the boolean value
Definition: zclk.c:111
MODULE_API zclk_res make_option(zclk_option **option, const char *name, const char *short_name, zclk_val *val, zclk_val *default_val, const char *description)
Definition: zclk.c:297
MODULE_API zclk_option * get_option_by_name(arraylist *options, const char *name)
Get the option by name object.
Definition: zclk.c:495
MODULE_API void zclk_fill_options_in_list(arraylist *optlist, zclk_option *options[])
Fill the entries in the given option array into an arraylist.
Definition: zclk.c:554
zclk_res(* zclk_command_fn)(struct zclk_command_t *cmd, void *handler_args)
defines a function to run a command
Definition: zclk.h:391
zclk_res(* zclk_command_output_handler)(zclk_res result_flag, zclk_result_type result_type, void *result)
Defines a function to handle command output.
Definition: zclk.h:382
MODULE_API zclk_argument * new_zclk_argument(const char *name, zclk_val *val, zclk_val *default_val, const char *desc, int nargs)
(Internal use) Create an argument object
Definition: zclk.c:581
MODULE_API void zclk_command_bool_argument(zclk_command *cmd, const char *name, int default_val, const char *desc, int nargs)
Create a new bool argument and add it to the given command.
Definition: zclk.c:943
MODULE_API zclk_argument * zclk_command_get_argument(zclk_command *cmd, const char *name)
Get the argument object corresponding to given name.
Definition: zclk.c:1020
MODULE_API zclk_res zclk_command_subcommand_add(zclk_command *cmd, zclk_command *subcommand)
Add a subcommand to the given command.
Definition: zclk.c:839
MODULE_API zclk_val * new_zclk_val_int(int int_val)
Create a new int value.
Definition: zclk.c:51
MODULE_API void zclk_command_double_argument(zclk_command *cmd, const char *name, double default_val, const char *desc, int nargs)
Create a new bool argument and add it to the given command.
Definition: zclk.c:966
MODULE_API void zclk_val_set_bool(zclk_val *val, int bval)
set the boolean value
Definition: zclk.c:137
MODULE_API const char * zclk_val_get_string(zclk_val *val)
get the string value
Definition: zclk.c:127
MODULE_API void zclk_command_string_option(zclk_command *cmd, const char *name, const char *short_name, const char *default_val, const char *desc)
Create a new string option and add it to the given command.
Definition: zclk.c:917
MODULE_API zclk_val * new_zclk_val_flag(int flag_val)
Create a new flag value.
Definition: zclk.c:84
MODULE_API void zclk_val_set_string(zclk_val *val, const char *sval)
set the string value
Definition: zclk.c:160
struct zclk_command_t zclk_command
A CLI Command Ojbect.
MODULE_API void zclk_val_set_dobule(zclk_val *val, double dval)
set the double value
Definition: zclk.c:152