Commander-API V3.0.0
Lightweight Command Parser
Loading...
Searching...
No Matches
Argument Class Reference

Argument class. More...

#include <Commander-Arguments.hpp>

Collaboration diagram for Argument:

Classes

struct  boolFields
 
union  returnData
 

Public Member Functions

 Argument (const char *source_p, int place_p)
 Constructor for place dependent argument. More...
 
 Argument (const char *source_p, char shortName_p)
 Constructor for optional or place independent argument with only short name. More...
 
 Argument (const char *source_p, char shortName_p, const char *longName_p)
 Constructor for optional or place independent argument with short and long name. More...
 
 operator int ()
 Override return behaviour for int. More...
 
 operator float ()
 Override return behaviour for float. More...
 
 operator char * ()
 Override return behaviour for char array. More...
 
 operator bool ()
 Override return behaviour for boolean. More...
 
bool parseInt ()
 
bool parseFloat ()
 
bool parseStringFunction (char *buffer, int bufferSize)
 
bool find ()
 
bool isFound ()
 
Commander::systemVariable_tgetSystemVariable ()
 

Private Types

typedef bool boolField
 

Private Member Functions

struct Argument::boolFields __attribute__ ((packed))
 
int findShortName ()
 Find the start position of a short named argument. More...
 
int findLongName ()
 Find the start position of a long named argument. More...
 
int findByPlace ()
 Find the start position of a place dependent argument. More...
 
int findStart ()
 
bool inString (int index)
 
int substring (char *str1, char *str2)
 

Private Attributes

const char * source = NULL
 
int sourceSize = -1
 
int place = -1
 
char shortName = '\0'
 
const char * longName = NULL
 
Commander::systemVariable_tsystemVariable = NULL
 
boolFields bFields
 
union returnData ret
 

Static Private Attributes

static const char failedString = '\0'
 

Friends

class ArgumentUT
 

Detailed Description

Argument class.

This class is made to parse simply int, float and string arguments from a source character array with the syntax style of the getopt library. Sadly getopt can not be used with an embedded system, like an AVR. It is too resource hungry. Also the C version of getopt does not support long argument names. This is a lightweight argument parser and it can be used for 4 kinds of arguments:

  • Place Dependent Argument - A place dependent argument is usually not optional. Also it has a fixed place in the argument string. It can be indexed like a regular array element from 0 to N-1 where N is the number of total arguments. The argument string must be start with the fixed position regular arguments. The first place dependent argument can be optional. It is easy to detect if it exists or not. But it is a good practice to allow only the first place dependent argument to be optional.
  • Optional Argument - These are optional settings for the command. They must be identified by a name. The name can be short or long. short: -h long: –help. The short argument name always starts with a dash character( '-' ). The long argument name always starts with two dash characters( '–' ). After the dashes, the name comes. In case of a short argument the name is always one characters long( this is why it is called short name ). Both long and short names are case sensitive. After the name, at least one white space character is required between the name and the value. Supported white space characters are space or tabulator( ' ' or '\t' ).
  • Place Independent Argument - The syntax is the same as the optional argument. Only difference is, that this is required by the user code, the value is not optional.
  • Flag - The flag is basically an optional argument without a value. The naming syntax is the same. Only difference is, that the flag does not have a value. It can be used to indicate something.

This library supports 3 kinds of values:

  • int - A regular int value. It is parsed by sscanf. It does not require an external buffer to parse the data. The parsed value is stored internally after parsing.
  • float - A regular float value. It is parsed by atof. It does not require an external buffer to parse the data. The parsed value is stored internally after parsing. Exponent notation is supported, for more information check here.
  • string - The string value parsing requires an external buffer to store the parsed string. If the string value contains white space characters, the value must be encapsulated with brackets like this: –text "this is a text with white space characters"
    Note
    Be careful with long strings on low power devices like Arduino Uno / Nano with AVR chips.

Definition at line 90 of file Commander-Arguments.hpp.

Member Typedef Documentation

◆ boolField

typedef bool Argument::boolField
private

Definition at line 180 of file Commander-Arguments.hpp.

Constructor & Destructor Documentation

◆ Argument() [1/3]

Argument::Argument ( const char *  source_p,
int  place_p 
)

Constructor for place dependent argument.

Use this constructor to create a place dependent argument object.

Parameters
source_pThe source argument string. This character array must contain the whole argument string. It must be terminated with string end character( '\0' ).
place_pThe place of the argument in the argument string. It is valid from 0 - N-1, where N is the number of white space separated tokens in the argument string.

Definition at line 3 of file Commander-Arguments.cpp.

◆ Argument() [2/3]

Argument::Argument ( const char *  source_p,
char  shortName_p 
)

Constructor for optional or place independent argument with only short name.

Use this constructor to create an optional or place independent argument object. This version only supports short argument names.

Note
If your system does not have a relatively high amount of dynamic memory, please use this version. It uses much less memory than, the long name version. It is perfect for UNO / NANO with AVR chipset.
Parameters
source_pThe source argument string. This character array must contain the whole argument string. It must be terminated with string end character( '\0' ).
shortName_pThis is a simple character. It is valid from a-z or A-Z.
Note
Be careful shortName_p is case sensitive!

Definition at line 33 of file Commander-Arguments.cpp.

◆ Argument() [3/3]

Argument::Argument ( const char *  source_p,
char  shortName_p,
const char *  longName_p 
)

Constructor for optional or place independent argument with short and long name.

Use this constructor to create an optional or place independent argument object. This version supports both short and long names.

Note
Only use this version if your system has a relatively high amount of dynamic memory. My recommendation is at least 10k-bytes of RAM if you need to use this( ESP8266, ESP32, STM32... ).
Parameters
source_pThe source argument string. This character array must contain the whole argument string. It must be terminated with string end character( '\0' ).
shortName_pThis is a simple character. It is valid from a-z or A-Z.
longName_pPointer to a character array, that contains the long name.
Note
Be careful both shortName_p and longName_p are case sensitive!

Definition at line 63 of file Commander-Arguments.cpp.

Member Function Documentation

◆ __attribute__()

struct Argument::boolFields Argument::__attribute__ ( (packed)  )
private

◆ find()

bool Argument::find ( )

Definition at line 674 of file Commander-Arguments.cpp.

Here is the call graph for this function:

◆ findByPlace()

int Argument::findByPlace ( )
private

Find the start position of a place dependent argument.

This function will return the position( index ) of the start location of a place dependent argument, if the argument is found in the source string.

Returns
The index of the argument. If the argument is not found, it will return a negative number.

Definition at line 303 of file Commander-Arguments.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findLongName()

int Argument::findLongName ( )
private

Find the start position of a long named argument.

This function will return the position( index ) of the start location of a long named argument, if the argument is found in the source string.

Returns
The index of the argument. If the argument is not found, it will return a negative number.
Note
If the short argument name is found, it will return the index of the first character of the argument. If the first character is a string terminator, it will return its index as well. It is necessary to find arguments without argument data( flags ).

Definition at line 175 of file Commander-Arguments.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findShortName()

int Argument::findShortName ( )
private

Find the start position of a short named argument.

This function will return the position( index ) of the start location of a short named argument, if the argument is found in the source string.

Returns
The index of the argument. If the argument is not found, it will return a negative number.
Note
If the short argument name is found, it will return the index of the first character of the argument. If the first character is a string terminator, it will return its index as well. It is necessary to find arguments without argument data( flags ).

Definition at line 92 of file Commander-Arguments.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findStart()

int Argument::findStart ( )
private

Definition at line 377 of file Commander-Arguments.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSystemVariable()

Commander::systemVariable_t * Argument::getSystemVariable ( )

Definition at line 790 of file Commander-Arguments.cpp.

◆ inString()

bool Argument::inString ( int  index)
private

Definition at line 764 of file Commander-Arguments.cpp.

Here is the caller graph for this function:

◆ isFound()

bool Argument::isFound ( )

Definition at line 727 of file Commander-Arguments.cpp.

◆ operator bool()

Argument::operator bool ( )

Override return behaviour for boolean.

The return behaviour is overriden in such a way, that it will return true value if the parsing process for the specified type was successful. Otherwise it will return false.

Definition at line 707 of file Commander-Arguments.cpp.

◆ operator char *()

Argument::operator char * ( )

Override return behaviour for char array.

The return behaviour is overriden in such a way, that it will return a pointer to the parsed string if the parseString function was successfully parsed the input argument string. Otherwise it will return a pointer to an empty string( "" ).

Definition at line 714 of file Commander-Arguments.cpp.

◆ operator float()

Argument::operator float ( )

Override return behaviour for float.

The return behaviour is overriden in such a way, that it will return the parsed float number if the parseFloat function was successfully parsed the input argument string. Otherwise it will return 0.0.

Definition at line 697 of file Commander-Arguments.cpp.

◆ operator int()

Argument::operator int ( )

Override return behaviour for int.

The return behaviour is overriden in such a way, that it will return the parsed integer number if the parseInt function was successfully parsed the input argument string. Otherwise it will return 0.

Definition at line 687 of file Commander-Arguments.cpp.

◆ parseFloat()

bool Argument::parseFloat ( )

Definition at line 470 of file Commander-Arguments.cpp.

Here is the call graph for this function:

◆ parseInt()

bool Argument::parseInt ( )

Definition at line 419 of file Commander-Arguments.cpp.

Here is the call graph for this function:

◆ parseStringFunction()

bool Argument::parseStringFunction ( char *  buffer,
int  bufferSize 
)

Definition at line 520 of file Commander-Arguments.cpp.

Here is the call graph for this function:

◆ substring()

int Argument::substring ( char *  str1,
char *  str2 
)
private

Definition at line 732 of file Commander-Arguments.cpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ ArgumentUT

friend class ArgumentUT
friend

Definition at line 260 of file Commander-Arguments.hpp.

Member Data Documentation

◆ bFields

boolFields Argument::bFields
private

Definition at line 210 of file Commander-Arguments.hpp.

◆ failedString

const char Argument::failedString = '\0'
staticprivate

Definition at line 216 of file Commander-Arguments.hpp.

◆ longName

const char* Argument::longName = NULL
private

Definition at line 203 of file Commander-Arguments.hpp.

◆ place

int Argument::place = -1
private

Definition at line 197 of file Commander-Arguments.hpp.

◆ ret

union returnData Argument::ret
private

Definition at line 212 of file Commander-Arguments.hpp.

◆ shortName

char Argument::shortName = '\0'
private

Definition at line 200 of file Commander-Arguments.hpp.

◆ source

const char* Argument::source = NULL
private

Definition at line 190 of file Commander-Arguments.hpp.

◆ sourceSize

int Argument::sourceSize = -1
private

Definition at line 194 of file Commander-Arguments.hpp.

◆ systemVariable

Commander::systemVariable_t* Argument::systemVariable = NULL
private

Definition at line 207 of file Commander-Arguments.hpp.