CDI SDK
SDK for transporting chunks of data reliably and with low latency using a polled mode network driver.
Loading...
Searching...
No Matches
optarg.c File Reference

This file contains the definitions of the functions used for option parsing, originally designed for the cdi_test program. More...

#include "optarg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "cdi_logger_api.h"
#include "cdi_test.h"
#include "test_console.h"
#include "test_common.h"

Enumerations

enum  OptionTypes { kArgError , kArgOnly , kOptShort , kOptLong }
 Enum for labeling different command line arguments as the command line is parsed. More...
 

Functions

static bool SearchOptions (const char *opt_str, int type, const OptDef *opt_array_ptr, OptArg *found_opt_ptr)
 Search the user options array for a given option. The type (long/short) indicates which option array to search (the long options or the short ones), and the len indicates how many characters to match.
 
static int CheckArg (const char *arg_str, const OptDef *opt_array_ptr, OptArg *found_opt_ptr, bool expecting_opt)
 Takes a command line argument and checks if it conforms to expected formatting and if it is a valid option or argument. If it is formatted as an option, check the user-defined options array. If it is a valid user option, then fill out the found_opt_ptr structure with name and number of arguments.
 
void RigWhitespaces (const char *long_name_str, const char *argument_str, int *max_long_name_length_ptr, int *max_argument_length_ptr)
 
void PrintKeyArrayNames (const CdiEnumStringKey *key_array, const int indent)
 
void PrintOption (const OptDef *option_ptr)
 
void PrintUsage (const OptDef *opt_array_ptr, bool has_error)
 
bool GetOpt (int argc, const char *argv[], int *index_ptr, OptDef *opt_array_ptr, OptArg *this_opt_ptr)
 

Detailed Description

This file contains the definitions of the functions used for option parsing, originally designed for the cdi_test program.

Enumeration Type Documentation

◆ OptionTypes

Enum for labeling different command line arguments as the command line is parsed.

Enumerator
kArgError 

An argument error was detected.

kArgOnly 

An orphaned argument has been found without a parent option.

kOptShort 

A short option has been found on the command line.

kOptLong 

A long option has been found on the command line.

Function Documentation

◆ CheckArg()

static int CheckArg ( const char * arg_str,
const OptDef * opt_array_ptr,
OptArg * found_opt_ptr,
bool expecting_opt )
static

Takes a command line argument and checks if it conforms to expected formatting and if it is a valid option or argument. If it is formatted as an option, check the user-defined options array. If it is a valid user option, then fill out the found_opt_ptr structure with name and number of arguments.

Parameters
arg_strThe command line argument we want to check.
opt_array_ptrThe user-defined options array we will search if we determine arg_str is a properly-formatted option.
found_opt_ptrIf a match is found, this is a structure describing the option.
expecting_optWhen true, we are expecting the arg_str input is a long or short option; when false, we are expecting it to be an argument.
Returns
Return 1 if we get an error; 0 if arg_str is valid.

◆ GetOpt()

bool GetOpt ( int argc,
const char * argv_ptr[],
int * index_ptr,
OptDef * opt_array_ptr,
OptArg * this_opt_ptr )

A user-facing function that takes in the argv system command line args array and an index and finds the next option and its associated arguments, incrementing the index accordingly. Returns false for any error condition or if the end of the arguments array has been reached. The contents of index_ptr are incremented as options and arguments are processed and will be equal to argc on a normal exit after processing all options and arguments. However, if we hit the end of the argv array and are still expecting arguments to the last option, we will increment the contents of index_ptr beyond argc so that the calling routine can detect that it was not a normal exit.

Parameters
argcThe system command line argument count variable.
argv_ptrThe pointer to the system command line arguments array.
index_ptrThe pointer to the current argv index, which gets incremented by the function as options and arguments are retrieved. Should match argc after all argv elements have been processed.
opt_array_ptrPointer to array of user-defined options.
this_opt_ptrA structure describing the single option (and arguments) retrieved.
Returns
True for success; false for failure or if we are at the end of argv.

◆ PrintKeyArrayNames()

void PrintKeyArrayNames ( const CdiEnumStringKey * key_array,
const int indent )

Print all the name_str elements of a key-value array in the format: <key_array[0].name_str, key_array[1].name_str ... >

Parameters
key_arrayA key-value array to search for name_str.
indentThe number of space to indent before printing the array of names from the name_str member of each key-value pair.

◆ PrintOption()

void PrintOption ( const OptDef * option_ptr)

Print the usage message of a single option based on the user-defined usage options.

Parameters
option_ptrPointer to a single user-defined option.

◆ PrintUsage()

void PrintUsage ( const OptDef * opt_array_ptr,
bool has_error )

Print the usage message based on the user-defined usage options.

Parameters
opt_array_ptrPointer to array of user-defined options.
has_errorTrue if we got an error and wish to print an error statement after printing usage.

◆ RigWhitespaces()

void RigWhitespaces ( const char * long_name_str,
const char * argument_str,
int * max_long_name_length_ptr,
int * max_argument_length_ptr )

When the length of either the long option string or the argument string exceed their allotted length, but taken together they don't exceed the combined allotted lengh, then we adjust the whitespaces so that the printout meets the target length.

Parameters
long_name_strLong option string to be printed. May be NULL.
argument_strArgument description to be printed. May be NULL.
max_long_name_length_ptrPointer to alloted length on input. Pointer to adjusted length on output.
max_argument_length_ptrPointer to alloted length on input. Pointer to adjuted length on output.

◆ SearchOptions()

static bool SearchOptions ( const char * opt_str,
int type,
const OptDef * opt_array_ptr,
OptArg * found_opt_ptr )
static

Search the user options array for a given option. The type (long/short) indicates which option array to search (the long options or the short ones), and the len indicates how many characters to match.

Parameters
opt_strThe option we are going to search for in the user options array.
typeThe type of option we are searching for; long or short.
opt_array_ptrThe user-defined options array we will search.
found_opt_ptrIf a match is found, this is a structure describing the option short name and number of expected arguments.
Returns
Return true if a match is found, otherwise return false.