Skip to content
Dmitry Lavnikevich edited this page Aug 20, 2014 · 4 revisions

Extopts allows to create convinient CLI. Its key features are described on this page. For thorough description of extopts functionality please see [[https://github.com/githaff/extopts/wiki/api][API]] page. Working example of most of library capabilities can be found within [[https://github.com/githaff/extopts/tree/master/src/example][source tree]].

  • Basic command line arguments parsing

    Extopts understands only arguments with or without parameter. Optional parameters are too rarely used and usualy bring undesirable complexity into usage, so this concept was dropped in extopts.

    Typical arguments parsing:

#+begin_src c #include <stdio.h> #include <extopts/extopts.h>

int opts_int = 0; const char *opts_str = NULL;

struct extopt opts[] = { { .name_long = "int", .name_short = 'i', EXTOPT_ARG_INT("INT", &opts_int), .desc = "some int option", }, { .name_long = "str", EXTOPT_ARG_STR("STR", &opts_str), .desc = "some string option", }, EXTOPTS_END };

int main(int argc, char *argv[]) { int i; int ret = 0; struct extmod *module = 0;

  if (extopts_get(&argc, argv, opts))
      return 1;

  printf("Configured options:\n");
  printf("  integer: %d\n", opts_int);
  printf("  string: \"%s\"\n", opts_str ? opts_str : "-empty-");

  return ret;

} #+end_src

Arguments description is presented with array of [[https://github.com/githaff/extopts/wiki/api][extopt]] structures, terminated with all-zero structure which is presented by macro EXTOPTS_END.

Extopt structure contains a number of descriptive fields (both mandatory and optional), but for convinience some of them can be presented by [[https://github.com/githaff/extopts/wiki/api][macro]].

Command line arguments parsing is performed with function [[https://github.com/githaff/extopts/wiki/api][extopts_get()]]. In case of parsing error it will return non-zero code and print error message to stderr.

Note: extopts_get() call will rearrange argv command line arguments and change argc value. For details see extopts_get [[https://github.com/githaff/extopts/wiki/api][description]].

Clone this wiki locally