Argument Parsing

Contains functions to simplify processing command line args.

The args library analyzes args as generally as possible. It recognizes three different types. First is the ‘flag’ or ‘regular flag’, which is simply one character. Their order doesn’t matter. They’re placed on the command line after a single hyphen.

Author
Stephen Brennan
Date
Created Sunday, 3 August 2014

Then there are long flags, which are strings that come after two hyphens. Both flags and long flags are allowed to have parameters passed with them.

Finally are bare strings, which are things that aren’t passed as parameters or as flags.

Copyright
Copyright (c) 2013-2016, Stephen Brennan. Released under the Revised BSD License. See the LICENSE.txt file for details.

Defines

MAX_FLAGS

The number of regular flags. 52 = 26 + 26.

Typedefs

typedef smb_ad

Data structure to store information on arguments passed to the program.

Functions

void arg_data_init(smb_ad * data)

Initialize an smb_ad structure in memory that has already been allocated.

Parameters
  • data: The pointer to the memory to allocate in.

smb_ad* arg_data_create()

Allocate and initialize a smb_ad structure for argument parsing.

Return
A pointer to the structure.

void arg_data_destroy(smb_ad * data)

Free the resources of an arg_data object, but do not free it.

Parameters
  • data: The arg data to destroy.

void arg_data_delete(smb_ad * data)

Free the resources of an arg data object, and then free the object itself.

Parameters
  • data: The arg data to delete.

void process_args(smb_ad * data, int argc, char ** argv)

Analyze the arguments passed to the program.

Pass in the argc and argv, but make sure to decrement and increment each respective variable so they do not include the name of the program. Unless, of course, you want the program name to be processed as an argument. Once you have called this function, you can use the querying functions to find all the arguments.

Parameters
  • data: A pointer to an smb_ad object.
  • argc: The number of arguments (not including program name).
  • argv: The arguments themselves (not including program name).

int check_flag(smb_ad * data, char flag)

Check whether a flag is raised.

Parameters
  • data: The smb_ad returned by process_args().
  • flag: The character flag to check. Alphabetical only.
Return Value
  • 0: if the flag was not set.
  • 1: otherwise.

int check_long_flag(smb_ad * data, char * flag)

Check whether a long flag appeared. It must occur verbatim.

Return
the index of the flag + 1 if it is set.
Parameters
Return Value
  • 0: iff the flag was not set.

int check_bare_string(smb_ad * data, char * string)

Check whether a bare string appeared. It must occur verbatim.

Return
the index of the flag + 1 if it is set.
Parameters
Return Value
  • 0: iff the flag was not set.

char* get_flag_parameter(smb_ad * data, char flag)

Return the string parameter associated with the flag.

Return
The parameter of the flag.
Parameters

char* get_long_flag_parameter(smb_ad * data, char * string)

Return the string parameter associated with the long string.

Return
The parameter of the long flag.
Parameters
Return Value
  • NULL: if no parameter or if flag not found.

void ad_print(smb_ad * data, FILE * f)

Print the contents of the arg data struct to a file for debugging.

Parameters
  • data: The struct to print.
  • f: The file to print to.

struct smb_ad
#include <ad.h>

Data structure to store information on arguments passed to the program.

Public Members

uint64_t flags

Holds boolean value for whether each character flag is set.

char* flag_strings[MAX_FLAGS]

Holds the parameters for each regular (character) flag.

struct smb_ll* long_flags

Holds the long flags.

struct smb_ll* long_flag_strings

Holds the parameters of the long flags.

struct smb_ll* bare_strings

Holds the bare strings (strings that aren’t flags or flag params).