-
Notifications
You must be signed in to change notification settings - Fork 1
-
Extopts
This portion of API presents basic extopts library functionality. Here 'basic' simpliest CLI use case - parsing command line arguments, extracting their parameters, leaving the rest to be handeled by programmer directly.
To use basic extopts you should insert
#+begin_src c #include <extopts/extopts.h> #+end_src
into your source files.
** Functions
*** extopts_get
Definition:
#+begin_src c int extopts_get(int *argc, char *argv[], struct extopt *opts); #+end_src
This is main extopts function which parses command line arguments
according to passed extopts description structures array.
It function takes standard command line arguments starting from
executable path in argv[0].
After processing argv will be rearranged so that:
1. all non-option arguments will be moved to the beginning of argv
starting from 0th argument;
2. 0th argument, path to application will be moved right after all
non-option arguments followed by options and their
arguments. It also will be stored to 'extpath' variable.
3. Short form of application name (its basename) will be stored at
'extname'. In case of module execution it will be changed to
format <APPNAME>-<MODNAME>.
4. value by argc address will contain number of these non-option
arguments.
Before:
<IMG>
After:
<IMG>
*** extopts_usage
: void extopts_usage(struct extopt *opts);
*** extopt_find
: struct extopt *extopt_find(char *opt_str, struct extopt *opts);
*** extopt_is_end
: static char extopt_is_end(struct extopt opt);
** Types
*** extopt
#+begin_src c struct extopt { char *name_long; char name_short; char *desc;
/* Option argument */
int has_arg;
char *arg_name;
enum extopt_argtype arg_type;
union {
void *addr;
const char **const_str;
bool *flag;
int (*setter)(struct extopt *opt, const char *arg);
} arg;
}; #+end_src
*** extopt_argtype
#+begin_src c enum extopt_argtype { /* Field 'arg' will be used as 'flag' pointing to flag for whether ,* parameter was met in command line or not / EXTOPT_ARGTYPE_NO_ARG, / Field 'arg' will be used as 'setter' handler which will be ,* called for argument parsing / EXTOPT_ARGTYPE_SPECIAL, / Field 'arg' will be used as 'addr' pointing to the variable of ,* corresponding size where parsed parameter argument value will ,* be stored / / Signed integers / EXTOPT_ARGTYPE_INT, EXTOPT_ARGTYPE_LINT, EXTOPT_ARGTYPE_LLINT, / Unsigned integers / EXTOPT_ARGTYPE_UINT, EXTOPT_ARGTYPE_ULINT, EXTOPT_ARGTYPE_ULLINT, / Floating-point / EXTOPT_ARGTYPE_FLOAT, EXTOPT_ARGTYPE_DOUBLE, EXTOPT_ARGTYPE_LDOUBLE, / Strings / EXTOPT_ARGTYPE_STR, / Field 'const_str' is used instead of 'addr' */ EXTOPT_ARGTYPE_CHAR, }; #+end_src
*** extarg presence
#+begin_src c enum { no_extarg, required_extarg, }; #+end_src
** Macros
*** Extopt structure filling
**** Signed integer arguments
- EXTOPT_ARG_INT
- EXTOPT_ARG_LINT
- EXTOPT_ARG_LLINT
#+begin_src c #define EXTOPT_ARG_INT(NAME, ADDR) #define EXTOPT_ARG_LINT(NAME, ADDR) #define EXTOPT_ARG_LLINT(NAME, ADDR) #+end_src
**** Unsigned integer arguments
- EXTOPT_ARG_UINT
- EXTOPT_ARG_ULINT
- EXTOPT_ARG_ULLINT
#+begin_src c #define EXTOPT_ARG_UINT(NAME, ADDR) #define EXTOPT_ARG_ULINT(NAME, ADDR) #define EXTOPT_ARG_ULLINT(NAME, ADDR) #+end_src
**** Floating-point arguments
- EXTOPT_ARG_FLOAT
- EXTOPT_ARG_DOUBLE
- EXTOPT_ARG_LDOUBLE
#+begin_src c #define EXTOPT_ARG_FLOAT(NAME, ADDR) #define EXTOPT_ARG_DOUBLE(NAME, ADDR) #define EXTOPT_ARG_LDOUBLE(NAME, ADDR) #+end_src
**** String arguments
- EXTOPT_ARG_STR
- EXTOPT_ARG_CHAR
#+begin_src c #define EXTOPT_ARG_STR(NAME, ADDR) #define EXTOPT_ARG_CHAR(NAME, ADDR) #+end_src
**** No arguments
#+begin_src c #define EXTOPT_NO_ARG(FLAG_ADDR) #+end_src
**** Special argument parser
#+begin_src c #define EXTOPT_ARG_SPECIAL(NAME, SETTER_FUNC) #+end_src
*** Pre-defined extopt structures
- EXTOPTS_HELP #+begin_src c #define EXTOPTS_HELP(FLAG_ADDR) #+end_src
- EXTOPTS_VERSION #+begin_src c #define EXTOPTS_VERSION(FLAG_ADDR) #+end_src
- EXTOPTS_END
** Global variables
- extname : char *extname;
- extpath : char *extpath;
-
Extmods
Extmods API allows to create CLI utility based on commands system: application firstly receives command name, then - arguments for it. Nearest example is git: 'git' utility with commands 'commit', 'pull', 'push' etc.
Functions of 'extmods' group allow to easily implement such command operation. Main utility can set its own policy of extracting command name from command line arguments, parse arguments related to this command and pass rest of arguments to main function of module, which is basically usual:
#+begin_src c int func(int argc, char *argv[]) #+end_src
To use extmods functionality you should insert
#+begin_src c #include <extopts/extmods.h> #+end_src
into your source files.
** Functions
*** extmod_find
: struct extmod *extmod_find(char *name);
*** extmod_extract
: struct extmod *extmod_extract(int *argc, char *argv[]);
*** extmod_exec
: int extmod_exec(int argc, char *argv[], struct extmod *module);
*** extmod_print_desc
: void extmod_print_desc(struct extmod *module);
*** extmod_print_opts
: void extmod_print_opts(struct extmod *module);
*** extmod_has_name
: char extmod_has_name(struct extmod *module);
*** extmod_has_desc
: char extmod_has_desc(struct extmod *module);
*** extmod_has_opts
: char extmod_has_opts(struct extmod *module);
*** extmods_usage_list
: void extmods_usage_list(void);
** Types
*** extmod
#+begin_src c struct extmod { char *name; int (*exec)(int argc, char *argv[]); struct extopt *opts; char *desc_short; char *desc; }; #+end_src
** Macros
*** Extmod declaration
#+begin_src c
#define EXTMOD_DECL(NAME, EXEC, OPTS, DESC_SHORT, DESC)
struct extmod *extmod_##NAME;
void extmod_constr_##NAME() attribute ((constructor));
void extmod_constr_##NAME()
{
extmod_##NAME = &extmods[extmods_num++];
extmod_##NAME->name = #NAME;
extmod_##NAME->exec = EXEC;
extmod_##NAME->opts = OPTS;
extmod_##NAME->desc_short = DESC_SHORT;
extmod_##NAME->desc = DESC;
}
#+end_src
** Global variables
- extmods : struct extmod extmods[];
- extmods_num : int extmods_num;
- extmod : struct extmod *extmod;
- extmodname : char extmodname[];