BSB Developer Docs
Loading...
Searching...
No Matches
argparse.h File Reference

Command line argument parsing library. More...

#include <core/string.h>

Go to the source code of this file.

Typedefs

typedef void(* OptionCallback) (char opt, const char *optarg, void *context)
 Option callback function type.
 

Functions

bool parse_args (FuriString *args, const char *opts, OptionCallback callback, void *context)
 Parse command options from an argument string.
 

Detailed Description

Command line argument parsing library.

Supports a getopt-like syntax with single-character options and positional arguments. Options may have a required argument or be argument-less.

Strings containing space characters must be enclosed in single or double quotes. If quotes need to be present inside of a quoted string, they must be of opposite type, e.g. "String with 'quotes'" or 'String with "quotes"'.

Escaping is not supported.

Typedef Documentation

◆ OptionCallback

typedef void(* OptionCallback) (char opt, const char *optarg, void *context)

Option callback function type.

If opt is a zero (NUL) character, the parsed option is a positional argument.

If an option argument was not required, optarg will be NULL.

Parameters
[in]optoption character from the parsed argument string
[in]optargoption argument (if required and present, may be NULL)
[in,out]contextpointer to a user-specified object from parse_args invocation

Function Documentation

◆ parse_args()

bool parse_args ( FuriString * args,
const char * opts,
OptionCallback callback,
void * context )

Parse command options from an argument string.

opts must be a string containing all of the options to be looked for, e.g. "abcDEF".

To make an option require an argument, : must be added after the option character, i.e. "a:b:c" means that -a and -b options require an argument, while -c does not.

If only positional arguments are needed, opts can be NULL or an empty string.

The following situations are treated as a success:

  • Input string contains options that match the opts option specifier string
  • Input string contains only positional arguments regardless of opts contents
  • Input string is empty or contains only space characters

The following situations are treated as errors:

  • An option is found in the input string that is not in the option specifier string
  • An option argument is required, but is not present in the input string
  • No option specifier is provided, but options are present in the input string
  • A non-paired quote was found in the input string
Parameters
[in,out]argspointer to a FuriString containing the argument string
[in]optspointer to a zero-terminated string containing option specifiers (may be NULL)
[in]callbackpointer to a function to be called for each option or positional argument
[in,out]contextpointer to a user-specified object (will be passed to the callback)
Returns
true on success, false otherwise