libamxrt  0.4.2
Ambiorix Run Time Library
Command Line Options

Functions

void amxrt_cmd_line_options_reset (void)
 Removes all default options. More...
 
int amxrt_cmd_line_add_option (int id, char short_option, const char *long_option, int has_args, const char *doc, const char *arg_doc)
 Adds a command line option definition. More...
 
void amxrt_cmd_line_set_usage_doc (const char *usage)
 Set the overall usage documentation string. More...
 
int amxrt_cmd_line_parse (int argc, char *argv[], amxrt_arg_fn_t fn)
 Starts parsing the command line options. More...
 
void amxrt_cmd_line_parse_assignment (const char *option, bool force)
 Parses an command line option or an argument with an assignment. More...
 
void amxrt_print_usage (void)
 Prints the usage information and all available options to stdout. More...
 

Detailed Description

The command line option parser, reads the options passed on the command line and passes them to the configuration.

The command line option parser can handle short options (starting with a single '-') and long options (staring with a double '-'). Options must be put before any other arguments on the command line. The command line option parser stops at the first argument that is not an option (not starting with a '-').

The command line option parser uses getopt_long to parse the options.

The default options handled are:

-h --help Print usage help and exit
-H --HELP Print extended help and exit
-B --backend so file Loads the shared object as bus backend
-u --uri uri Adds an uri to the list of uris
-A --no-auto-detect Do not auto detect unix domain sockets and back-ends
-C --no-connect Do not auto connect the provided or detected uris
-I --include-dir dir Adds include directory for odl parser, multiple allowed
-L --import-dir dir Adds import directory for odl parser, multiple allowed
-o --option name=value Adds a configuration option
-F --forced-option name=value Adds a configuration option, which can not be overwritten by odl files
-O --ODL odl-string An ODL in string format, only one ODL string allowed
-D --daemon Daemonize the process
-p --priority nice level Sets the process nice level
-N --no-pid-file Disables the creation of a pid-file in /var/run
-E --eventing Enables eventing during loading of ODL files
-d --dump [caps|config] Dumps configuration options or capabilities at start-up
-l --log Write to syslog instead of stdout and stderr
-R --requires root object Checks if datamodel objects are available or waits until they are available
-V --version Print version
&include test odl
config
Definition: test.odl:54

If the default options doesn't fit your usage, they can be removed by calling amxrt_cmd_line_options_reset.

Adding new options (or re-adding some of the default options) can be done using: amxrt_cmd_line_add_option

Use amxrt_cmd_line_parse to start parsing the given command line options.

The syntax for parsing options at command line is:

# option has no argument or an optional argument
-o
# option has a required or optional argument
-o arg
-o=arg

Function Documentation

◆ amxrt_cmd_line_add_option()

int amxrt_cmd_line_add_option ( int  id,
char  short_option,
const char *  long_option,
int  has_args,
const char *  doc,
const char *  arg_doc 
)

Adds a command line option definition.

This function adds a command line option definition to the list of accepted options.

Parameters
idthe id of the option, if 0 is given the short_option is used as id.
short_optiona single character representing the short option/
long_optiona string literal containing the name of the long option without the double '-'
has_argsmust be one of no_argument (if the option does not take an argument), required_argument (if the option requires an argument) or optional_argument (if the takes an optional argument).
doca string literal describing the option
arg_doca string literal describing the argument or NULL if the option doesn't have an argument
Returns
0 when option definition is added, any other value indicates an error.

Definition at line 206 of file amxrt_args.c.

206  {
207  int rv = -1;
208  amxrt_t* rt = amxrt_get();
209  amxrt_arg_t* option = NULL;
210 
211  option = (amxrt_arg_t*) calloc(1, sizeof(amxrt_arg_t));
212  when_null(option, exit);
213 
214  option->id = id == 0 ? (int) short_option : id;
215  option->short_option = short_option;
216  option->long_option = long_option;
217  option->has_args = has_args;
218  option->doc = doc;
219  option->arg_doc = arg_doc;
220 
221  rv = amxc_llist_append(&rt->cmd_line_args, &option->it);
222 
223 exit:
224  if(rv != 0) {
225  free(option);
226  }
227  return rv;
228 }
static amxrt_t rt
Definition: amxrt.c:74
PRIVATE amxrt_t * amxrt_get(void)
Definition: amxrt.c:297
const char * arg_doc
Definition: amxrt_priv.h:114
const char * long_option
Definition: amxrt_priv.h:110
amxc_llist_it_t it
Definition: amxrt_priv.h:115
const char * doc
Definition: amxrt_priv.h:113
amxc_llist_t cmd_line_args
Definition: amxrt_priv.h:102

◆ amxrt_cmd_line_options_reset()

void amxrt_cmd_line_options_reset ( void  )

Removes all default options.

Calling this function will remove all defined default options. If some of them are needed they can be re-added using amxrt_cmd_line_add_option

Definition at line 200 of file amxrt_args.c.

200  {
201  amxrt_t* rt = amxrt_get();
202  amxc_llist_clean(&rt->cmd_line_args, amxrt_cmd_line_free_option);
203 }
static void amxrt_cmd_line_free_option(amxc_llist_it_t *it)
Definition: amxrt_args.c:68

◆ amxrt_cmd_line_parse()

int amxrt_cmd_line_parse ( int  argc,
char *  argv[],
amxrt_arg_fn_t  fn 
)

Starts parsing the command line options.

Typically this function is called from within the applications main and the argc and argv arguments of the main are passed to this method.

Optionally a function pointer can be provided. This function will be called for each option encountered before the default option handler is used. If the argument handle function returns 0, it indicates that the option is handled and should not be passed to the default handler, when the callback function returns -1 the option parser will stop and the application should exit, when the callback function return -2, the default command line argument option handler is called. If the callback function returns anything else then 0,-1 or -2, it is considered an error and the command line option parser will stop.

The callback function must match this signature

typedef int (* amxrt_arg_fn_t) (amxc_var_t* config,
int arg_id,
const char* value);
int(* amxrt_arg_fn_t)(amxc_var_t *config, int arg_id, const char *value)
Definition: amxrt.h:115
Parameters
argcthe number of arguments available
argvthe vector containing the commandline arguments
fn(optional) a pointer to a callback function to handle the option or NULL
Returns
A negative number indicates an error, a positive number indicates the index in argv where the parsing has stopped (first non option argument). If the returned value equals argc, all command line options were parsed and handled.

Definition at line 236 of file amxrt_args.c.

236  {
237  amxrt_t* rt = amxrt_get();
238  amxc_var_t* config = amxrt_get_config();
239  int nr_options = amxc_llist_size(&rt->cmd_line_args);
240  struct option* long_options = (struct option*) calloc(nr_options + 1, sizeof(struct option));
241  amxc_string_t format;
242  int index = 0;
243  int rv = 0;
244  int c;
245 
246  if(long_options == NULL) {
247  // handle error case where calloc failed
248  return -1;
249  }
250 
251  optind = 1;
252  amxc_string_init(&format, 0);
253  amxc_llist_for_each(it, &rt->cmd_line_args) {
254  amxrt_arg_t* option = amxc_container_of(it, amxrt_arg_t, it);
255  long_options[index].name = option->long_option;
256  long_options[index].has_arg = option->has_args;
257  long_options[index].val = option->id;
258  if(option->has_args == no_argument) {
259  amxc_string_appendf(&format, "%c", option->short_option);
260  } else if(option->has_args == optional_argument) {
261  amxc_string_appendf(&format, "%c::", option->short_option);
262  } else {
263  amxc_string_appendf(&format, "%c:", option->short_option);
264  }
265  index++;
266  }
267 
268  index = 0;
269  while(1) {
270  c = getopt_long(argc, argv, amxc_string_get(&format, 0), long_options, &index);
271  if(c == -1) {
272  break;
273  }
274  rv = -2;
275  if(fn != NULL) {
276  // call provided function
277  // if it returns 0 the option is handled
278  // if it returns -1 stop option parsing and exit
279  // if it returns -2 call default argument handler
280  // all others are considered as an error
281  rv = fn(config, c, optarg);
282  }
283  if(rv == -2) {
284  rv = amxrt_cmd_line_default(config, c, optarg);
285  }
286  when_failed_status(rv, exit, rv = -1);
287  }
288 
289 exit:
290  free(long_options);
291  amxc_string_clean(&format);
292  return rv == 0 ? optind : rv;
293 }
static int amxrt_cmd_line_default(amxc_var_t *config, int arg_id, const char *value)
Definition: amxrt_args.c:73
#define c(x)
Definition: amxrt_priv.h:97
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301

◆ amxrt_cmd_line_parse_assignment()

void amxrt_cmd_line_parse_assignment ( const char *  option,
bool  force 
)

Parses an command line option or an argument with an assignment.

This function parses a command line option or command line argument with an assignment.

The key will be added to the config options. The config can be retrieved using amxrt_get_config.

Example:

amxrt -o key=value
amxrt key=value
int amxrt(int argc, char *argv[], amxrt_arg_fn_t fn)
This function can create full ambiorix application (data model provider or client).
Definition: amxrt.c:334

The key can be any string without spaces. The value can a simple value or a JSON string.

If the key contains dots (.), is is used as a path to the config option.

Parameters
optionthe command line option value or the command line argument.
forcewhen set to true, odl config sections can not overwrite this value.

Definition at line 296 of file amxrt_args.c.

296  {
297  amxrt_t* rt = amxrt_get();
298  amxc_var_t* config = NULL;
299  amxc_string_t str_option;
300  amxc_llist_t options;
301  amxc_string_t* name = NULL;
302  amxc_string_t* value = NULL;
303  amxc_var_t var_option;
304 
305  amxc_var_init(&var_option);
306  amxc_llist_init(&options);
307  amxc_string_init(&str_option, 0);
308 
309  amxc_string_set(&str_option, option);
310  amxc_string_split_to_llist(&str_option, &options, '=');
311  when_true(amxc_llist_is_empty(&options), leave);
312  when_true(amxc_llist_size(&options) != 2, leave);
313 
314  name = amxc_string_from_llist_it(amxc_llist_get_first(&options));
315  value = amxc_string_from_llist_it(amxc_llist_get_last(&options));
316 
317  amxc_string_trim(name, NULL);
318  amxc_string_trim(value, NULL);
319 
320  if(amxc_var_set(jstring_t, &var_option, amxc_string_get(value, 0)) != 0) {
321  amxc_var_set(cstring_t, &var_option, amxc_string_get(value, 0));
322  } else {
323  amxc_var_cast(&var_option, AMXC_VAR_ID_ANY);
324  }
325 
326  config = force ? &rt->forced_options : &rt->parser.config;
327  amxc_var_set_path(config, amxc_string_get(name, 0), &var_option,
328  AMXC_VAR_FLAG_AUTO_ADD | AMXC_VAR_FLAG_COPY);
329 
330 leave:
331  amxc_string_clean(&str_option);
332  amxc_llist_clean(&options, amxc_string_list_it_free);
333  amxc_var_clean(&var_option);
334  return;
335 }
amxo_parser_t parser
Definition: amxrt_priv.h:101
amxc_var_t forced_options
Definition: amxrt_priv.h:103

◆ amxrt_cmd_line_set_usage_doc()

void amxrt_cmd_line_set_usage_doc ( const char *  usage)

Set the overall usage documentation string.

When the function amxrt_print_usage the usage documentation is printed to stdout. On the first line the global usage is printed.

Example (default string)

amxrt [OPTIONS] <odl files>

The first part (until the first space) is the name of the application, this is added automatically by amxrt_print_usage (taken from argv[0] and should not be added to the provided string.

Example:

amxrt_cmd_line_set_usage_doc("[OPTIONS] <OBJECT PATH> <FILTER>");
void amxrt_cmd_line_set_usage_doc(const char *usage)
Set the overall usage documentation string.
Definition: amxrt_args.c:230

if after this line amxrt_print_usage is called (assuming that the name of the application is "amxrt") the output will look like this:

amxrt [OPTIONS] <OBJECT PATH> <FILTER>
Parameters
usageA string literal containing the usage overview.

Definition at line 230 of file amxrt_args.c.

230  {
231  amxrt_t* rt = amxrt_get();
232  rt->usage_doc = usage == NULL ? "[OPTIONS] <odl files>" : usage;
233 }
const char * usage_doc
Definition: amxrt_priv.h:105

◆ amxrt_print_usage()

void amxrt_print_usage ( void  )

Prints the usage information and all available options to stdout.

Using the usage doc string and the defined options the usage information is created and printed to stdout.

The usage doc string can be set using amxrt_cmd_line_set_usage_doc.

Options can be added using amxrt_cmd_line_add_option or all removed using amxrt_cmd_line_options_reset.

The default usage information is: Example:

amxrt [OPTIONS] <odl files>
-h --help Print usage help and exit
-H --HELP Print extended help and exit
-B --backend <so file> Loads the shared object as bus backend
-u --uri <uri> Adds an uri to the list of uris
-A --no-auto-detect Do not auto detect unix domain sockets and back-ends
-C --no-connect Do not auto connect the provided or detected uris
-I --include-dir <dir> Adds include directory for odl parser, multiple allowed
-L --import-dir <dir> Adds import directory for odl parser, multiple allowed
-o --option <name=value> Adds a configuration option
-F --forced-option <name=value> Adds a configuration option, which can not be overwritten by odl files
-O --ODL <odl-string> An ODL in string format, only one ODL string allowed
-D --daemon Daemonize the process
-p --priority <nice level> Sets the process nice level
-N --no-pid-file Disables the creation of a pid-file in /var/run
-E --eventing Enables eventing during loading of ODL files
-d --dump-config Dumps configuration options at start-up
-l --log Write to syslog instead of stdout and stderr
-R --requires <root object> Checks if datamodel objects are available or waits until they are available
-V --version Print version

Definition at line 137 of file amxrt_user_output.c.

137  {
138  amxrt_t* rt = amxrt_get();
139  const char* name = GET_CHAR(&rt->parser.config, "name");
140 
141  printf("%s %s\n", name, rt->usage_doc);
142 
143  if(!amxc_llist_is_empty(&rt->cmd_line_args)) {
144  printf("\n");
145  printf("%sOptions%s:\n", c(CYAN), c(RESET));
146  printf("\n");
147 
148  amxc_llist_for_each(it, &rt->cmd_line_args) {
149  amxrt_arg_t* arg = amxc_container_of(it, amxrt_arg_t, it);
151  arg->long_option == NULL ? "" : arg->long_option,
152  arg->arg_doc,
153  arg->doc);
154  }
155  }
156 
157  printf("\n");
158 }
#define RESET
Definition: amxrt_priv.h:95
#define CYAN
Definition: amxrt_priv.h:93
static void amxrt_cmd_line_arg(const char so, const char *lo, const char *args, const char *description)