Skip to content

Commit

Permalink
updated command-line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-nv committed Jun 25, 2020
1 parent 52d9d51 commit c71f5a7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 25 deletions.
127 changes: 107 additions & 20 deletions commandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#define ARGC_START 0


// strRemoveDelimiter
static inline int strRemoveDelimiter( char delimiter, const char* string )
// search for the end of a leading character in a string (e.g. '--foo')
static inline int strFindDelimiter( char delimiter, const char* string )
{
int string_start = 0;

Expand All @@ -46,6 +46,49 @@ static inline int strRemoveDelimiter( char delimiter, const char* string )
}


// replace hyphens for underscores and vice-versa (returns NULL if no changes)
static inline char* strSwapDelimiter( const char* string )
{
if( !string )
return NULL;

// determine if the original char is in the string
bool found = false;
const int str_length = strlen(string);

for( int n=0; n < str_length; n++ )
{
if( string[n] == '-' || string[n] == '_' )
{
found = true;
break;
}
}

if( !found )
return NULL;

// allocate a new string to modify
char* new_str = (char*)malloc(str_length);

if( !new_str )
return NULL;

strcpy(new_str, string);

// replace instances of the old char
for( int n=0; n < str_length; n++ )
{
if( new_str[n] == '-' )
new_str[n] = '_';
else if( new_str[n] == '_' )
new_str[n] = '-';
}

return new_str;
}


// constructor
commandLine::commandLine( const int pArgc, char** pArgv, const char* extraFlag )
{
Expand All @@ -71,17 +114,17 @@ commandLine::commandLine( const int pArgc, char** pArgv, const char** extraArgs


// GetInt
int commandLine::GetInt( const char* string_ref, int default_value ) const
int commandLine::GetInt( const char* string_ref, int default_value, bool allowOtherDelimiters ) const
{
if( argc < 1 )
return 0;

bool bFound = false;
int value = -1;
int value = -1;

for( int i=ARGC_START; i < argc; i++ )
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start == 0 )
continue;
Expand All @@ -107,17 +150,28 @@ int commandLine::GetInt( const char* string_ref, int default_value ) const
}


if (bFound)
if( bFound )
return value;

return default_value;
if( !allowOtherDelimiters )
return default_value;

// try looking for the argument with delimiters swapped
char* swapped_ref = strSwapDelimiter(string_ref);

if( !swapped_ref )
return default_value;

value = GetInt(swapped_ref, default_value, false);
free(swapped_ref);
return value;
}


// GetUnsignedInt
uint32_t commandLine::GetUnsignedInt( const char* argName, uint32_t defaultValue ) const
uint32_t commandLine::GetUnsignedInt( const char* argName, uint32_t defaultValue, bool allowOtherDelimiters ) const
{
const int val = GetInt(argName, (int)defaultValue);
const int val = GetInt(argName, (int)defaultValue, allowOtherDelimiters);

if( val < 0 )
return defaultValue;
Expand All @@ -127,7 +181,7 @@ uint32_t commandLine::GetUnsignedInt( const char* argName, uint32_t defaultValue


// GetFloat
float commandLine::GetFloat( const char* string_ref, float default_value ) const
float commandLine::GetFloat( const char* string_ref, float default_value, bool allowOtherDelimiters ) const
{
if( argc < 1 )
return 0;
Expand All @@ -137,7 +191,7 @@ float commandLine::GetFloat( const char* string_ref, float default_value ) const

for (int i=ARGC_START; i < argc; i++)
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start == 0 )
continue;
Expand Down Expand Up @@ -165,19 +219,30 @@ float commandLine::GetFloat( const char* string_ref, float default_value ) const
if( bFound )
return value;

return default_value;
if( !allowOtherDelimiters )
return default_value;

// try looking for the argument with delimiters swapped
char* swapped_ref = strSwapDelimiter(string_ref);

if( !swapped_ref )
return default_value;

value = GetFloat(swapped_ref, default_value, false);
free(swapped_ref);
return value;
}


// GetFlag
bool commandLine::GetFlag( const char* string_ref ) const
bool commandLine::GetFlag( const char* string_ref, bool allowOtherDelimiters ) const
{
if( argc < 1 )
return false;

for (int i=ARGC_START; i < argc; i++)
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start == 0 )
continue;
Expand All @@ -192,19 +257,30 @@ bool commandLine::GetFlag( const char* string_ref ) const
return true;
}

return false;
if( !allowOtherDelimiters )
return false;

// try looking for the argument with delimiters swapped
char* swapped_ref = strSwapDelimiter(string_ref);

if( !swapped_ref )
return false;

const bool value = GetFlag(swapped_ref, false);
free(swapped_ref);
return value;
}


// GetString
const char* commandLine::GetString( const char* string_ref, const char* default_value ) const
const char* commandLine::GetString( const char* string_ref, const char* default_value, bool allowOtherDelimiters ) const
{
if( argc < 1 )
return 0;

for (int i=ARGC_START; i < argc; i++)
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start == 0 )
continue;
Expand All @@ -217,7 +293,18 @@ const char* commandLine::GetString( const char* string_ref, const char* default_
//*string_retval = &string_argv[length+1];
}

return default_value;
if( !allowOtherDelimiters )
return default_value;

// try looking for the argument with delimiters swapped
char* swapped_ref = strSwapDelimiter(string_ref);

if( !swapped_ref )
return default_value;

const char* value = GetString(swapped_ref, default_value, false);
free(swapped_ref);
return value;
}


Expand All @@ -231,7 +318,7 @@ const char* commandLine::GetPosition( unsigned int position, const char* default

for (int i=1/*ARGC_START*/; i < argc; i++)
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start != 0 )
continue;
Expand All @@ -253,7 +340,7 @@ unsigned int commandLine::GetPositionArgs() const

for (int i=1/*ARGC_START*/; i < argc; i++)
{
const int string_start = strRemoveDelimiter('-', argv[i]);
const int string_start = strFindDelimiter('-', argv[i]);

if( string_start != 0 )
continue;
Expand Down
30 changes: 25 additions & 5 deletions commandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,67 @@ class commandLine
* command line. For example, if argv contained `--foo`, then
* `GetFlag("foo")` would return `true`
*
* @param allowOtherDelimiters if true (default), the argName will be
* matched against occurances containing either `-` or `_`.
* For example, `--foo-bar` and `--foo_bar` would be the same.
*
* @returns `true`, if the flag with argName was found
* `false`, if the flag with argName was not found
*/
bool GetFlag( const char* argName ) const;
bool GetFlag( const char* argName, bool allowOtherDelimiters=true ) const;

/**
* Get float argument. For example if argv contained `--foo=3.14159`,
* then `GetInt("foo")` would return `3.14159f`
*
* @param allowOtherDelimiters if true (default), the argName will be
* matched against occurances containing either `-` or `_`.
* For example, `--foo-bar` and `--foo_bar` would be the same.
*
* @returns `defaultValue` if the argument couldn't be found. (`0.0` by default).
* Otherwise, returns the value of the argument.
*/
float GetFloat( const char* argName, float defaultValue=0.0f ) const;
float GetFloat( const char* argName, float defaultValue=0.0f, bool allowOtherDelimiters=true ) const;

/**
* Get integer argument. For example if argv contained `--foo=100`,
* then `GetInt("foo")` would return `100`
*
* @param allowOtherDelimiters if true (default), the argName will be
* matched against occurances containing either `-` or `_`.
* For example, `--foo-bar` and `--foo_bar` would be the same.
*
* @returns `defaultValue` if the argument couldn't be found (`0` by default).
* Otherwise, returns the value of the argument.
*/
int GetInt( const char* argName, int defaultValue=0 ) const;
int GetInt( const char* argName, int defaultValue=0, bool allowOtherDelimiters=true ) const;

/**
* Get unsigned integer argument. For example if argv contained `--foo=100`,
* then `GetUnsignedInt("foo")` would return `100`
*
* @param allowOtherDelimiters if true (default), the argName will be
* matched against occurances containing either `-` or `_`.
* For example, `--foo-bar` and `--foo_bar` would be the same.
*
* @returns `defaultValue` if the argument couldn't be found, or if the value
* was negative (`0` by default). Otherwise, returns the parsed value.
*/
uint32_t GetUnsignedInt( const char* argName, uint32_t defaultValue=0 ) const;
uint32_t GetUnsignedInt( const char* argName, uint32_t defaultValue=0, bool allowOtherDelimiters=true ) const;

/**
* Get string argument. For example if argv contained `--foo=bar`,
* then `GetString("foo")` would return `"bar"`
*
* @param allowOtherDelimiters if true (default), the argName will be
* matched against occurances containing either `-` or `_`.
* For example, `--foo-bar` and `--foo_bar` would be the same.
*
* @returns `defaultValue` if the argument couldn't be found (`NULL` by default).
* Otherwise, returns a pointer to the argument value string
* from the `argv` array.
*/
const char* GetString( const char* argName, const char* defaultValue=NULL ) const;
const char* GetString( const char* argName, const char* defaultValue=NULL, bool allowOtherDelimiters=true ) const;

/**
* Get positional string argument. Positional arguments aren't named, but rather
Expand Down

0 comments on commit c71f5a7

Please sign in to comment.