Group PJ_ARGPARSE

group PJ_ARGPARSE

This module provides header only utilities to parse command line arguments. This is mostly used by PJSIP test and sample apps. Note that there is getopt() implementation in PJLIB-UTIL (but it’s in PJLIB-UTIL, so it can’t be used by PJLIB).

Limitations:

  • the utility only supports white space(s) as separator between an option and its value. Equal sign is not supported.

  • the utility does not support double dash (—) as separator between options and operands. It will keep treating arguments after — as possible options.

Functions

char *pj_argparse_peek_next_option(char *const argv[])

Peek the next possible option from argv. An argument is considered an option if it starts with “-“.

Parameters:

argv – The argv, which must be null terminated.

Returns:

next option or NULL.

pj_bool_t pj_argparse_exists(char *const argv[], const char *opt)

Check that an option exists, without modifying argv.

Parameters:
  • argv – The argv, which must be null terminated.

  • opt – The option to find, e.g. “-h”, “–help”

Returns:

PJ_TRUE if the option exists, else PJ_FALSE.

pj_bool_t pj_argparse_get_bool(int *argc, char *argv[], const char *opt)

Check for an option and if it exists remove that option from argc/argv and returns PJ_TRUE.

Parameters:
  • argc – Pointer to argc.

  • argv – Null terminated argv.

  • opt – The option to find, e.g. “-h”, “–help”

Returns:

PJ_TRUE if the option exists, else PJ_FALSE.

pj_status_t pj_argparse_get_str(int *argc, char *argv[], const char *opt, char **ptr_value)

Check for an option and if it exists get the value and remove both the option and the value from argc/argv. Note that the function only supports whitespace(s) as separator between option and value (i.e. equal sign is not supported, e.g. “–server=127.0.0.1” will not be parsed correctly).

Parameters:
  • argc – Pointer to argc.

  • argv – Null terminated argv.

  • opt – The option to find, e.g. “-t”, “–type”

  • ptr_value – Pointer to receive the value.

Returns:

- PJ_SUCCESS if the option exists and value is found or if the option does not exist

  • PJ_EINVAL if the option exits but value is not found

pj_status_t pj_argparse_get_int(int *argc, char *argv[], const char *opt, int *ptr_value)

Check for an option and if it exists, get the integer value and remove both the option and the value from argc/argv. Note that the function only supports whitespace(s) as separator between option and value (i.e. equal sign is not supported, e.g. “–port=80” will not be parsed correctly).

Parameters:
  • opt – The option to find, e.g. “-h”, “–help”

  • argc – Pointer to argc.

  • argv – Null terminated argv.

  • ptr_value – Pointer to receive the value.

Returns:

- PJ_SUCCESS if the option exists and value is found or if the option does not exist

  • PJ_EINVAL if the option exits but value is not found, or if the value is not an integer.