library/src/blockclass/subclasses/BP_CommandLineOptionParser.cc
// adds a command line option to the parser (this is the fallback routine used by all the
// specific argument addition methods and implements all options available.  It's obtuse
// but only as a mechanism to provide functionality to the more specific argument addition
// methods)
adds a command line option to the parser (this is the fallback routine used by all the
specific argument addition methods and implements all options available.  It's obtuse
but only as a mechanism to provide functionality to the more specific argument addition
methods)
adds a command line option to the parser ( this is the fallback routine used by all the specific argument addition methods and implements all options available . It ' s obtuse but only as a mechanism to provide functionality to the more specific argument addition methods )
BP_ERROR_T BP_CommandLineOptionParser::addOption
(
	char *token,
	bool token_is_long_form,
	size_t token_required,
	BP_COMMAND_LINE_ARGUMENT_TYPE value_type,
	char *token_description,
	char *token_usage_example,
	void * default_value,
	size_t max_length_for_string,
	size_t min_length_for_string,
	size_t max_value_as_size_t,
	size_t min_value_as_size_t,
	int max_value_as_integer,
	int min_value_as_integer,
	void * (*argument_activation_callback)(BP_CommandLineOptionParser * parser, P_BP_COMMAND_LINE_PARSER_TOKENS token),
	char ** preg_custom_filters
)
{
	if(!this->tq)
		return ERR_FAILURE;
	if(!token)
		return ERR_FAILURE;
	this->tokens_n++;
	this->tokens = (P_BP_COMMAND_LINE_PARSER_TOKENS) tq_realloc(this->tq, this->tokens, sizeof(BP_COMMAND_LINE_PARSER_TOKENS) * this->tokens_n, BPLN);
	if(!this->tokens)
		return ERR_FAILURE;
	memset(&this->tokens[this->tokens_n-1], 0x00, sizeof(BP_COMMAND_LINE_PARSER_TOKENS));
	P_BP_COMMAND_LINE_PARSER_TOKENS new_token = (P_BP_COMMAND_LINE_PARSER_TOKENS) &this->tokens[this->tokens_n-1];
	new_token->token = (char *) tq_strndup(this->tq, token, 4096, BPLN);
	new_token->token_length = bpstrlen(new_token->token);
	new_token->token_is_long_form = token_is_long_form;
	 requirement
	new_token->token_required = token_required;
	new_token->value_type = value_type;
	if(token_description)
		new_token->token_description = (char *) tq_strdup(this->tq, token_description, BPLN);
	if(token_usage_example)
		new_token->token_usage_example = (char *) tq_strdup(this->tq, token_usage_example, BPLN);
	switch(value_type)
	{
		case BP_COMMAND_LINE_ARGUMENT_UNSET:
			break;
		case BP_COMMAND_LINE_ARGUMENT_STAND_ALONE_TOGGLE:
			new_token->default_value_as_toggle = default_value ? BP_TRUE : BP_FALSE;
			break;
		case BP_COMMAND_LINE_ARGUMENT_CHAR_STRING:
			if(default_value)
				new_token->default_value_as_string = (char *) tq_strdup(this->tq, (char *) default_value, BPLN);
			new_token->max_length_for_string = max_length_for_string;
			new_token->min_length_for_string = min_length_for_string;
			break;
		case BP_COMMAND_LINE_ARGUMENT_SIZE_T:
			new_token->default_value_as_size_t = (size_t) default_value;
			new_token->max_value_as_size_t = max_value_as_size_t;
			new_token->min_value_as_size_t = min_value_as_size_t;
			break;
		case BP_COMMAND_LINE_ARGUMENT_SIGNED_INT:
			memcpy(&new_token->default_value_as_int, &default_value, sizeof(new_token->default_value_as_int));
			new_token->max_value_as_integer = max_value_as_integer;
			new_token->min_value_as_integer = min_value_as_integer;
			break;
		case BP_COMMAND_LINE_ARGUMENT_URL:
			if
			(
					BP_StringIsURL((char *) default_value, BP_TRUE) == ERR_SUCCESS ||
					BP_StringIsURL((char *) default_value, BP_FALSE) == ERR_SUCCESS
			)
			{
				new_token->default_value_as_string = (char *) tq_strdup(this->tq, (char *) default_value, BPLN);
			}
			new_token->max_length_for_string = max_length_for_string;
			new_token->min_length_for_string = min_length_for_string;
			break;
		default:
			break;
	}
	if(argument_activation_callback)
	{
		new_token->argument_callback_enabled = BP_TRUE;
		new_token->argument_activation_callback = argument_activation_callback;
	}
	return ERR_SUCCESS;
}
BP_ERROR_T BP_CommandLineOptionParser :: addOption ( char * token , bool token_is_long_form , size_t token_required , BP_COMMAND_LINE_ARGUMENT_TYPE value_type , char * token_description , char * token_usage_example , void * default_value , size_t max_length_for_string , size_t min_length_for_string , size_t max_value_as_size_t , size_t min_value_as_size_t , int max_value_as_integer , int min_value_as_integer , void * ( * argument_activation_callback ) ( BP_CommandLineOptionParser * parser , P_BP_COMMAND_LINE_PARSER_TOKENS token ) , char * * preg_custom_filters ) { if ( ! this -> tq ) return ERR_FAILURE ; if ( ! token ) return ERR_FAILURE ; this -> tokens_n ++ ; this -> tokens = ( P_BP_COMMAND_LINE_PARSER_TOKENS ) tq_realloc ( this -> tq , this -> tokens , sizeof ( BP_COMMAND_LINE_PARSER_TOKENS ) * this -> tokens_n , BPLN ) ; if ( ! this -> tokens ) return ERR_FAILURE ; memset ( & this -> tokens [ this -> tokens_n - 1 ] , 0x00 , sizeof ( BP_COMMAND_LINE_PARSER_TOKENS ) ) ; P_BP_COMMAND_LINE_PARSER_TOKENS new_token = ( P_BP_COMMAND_LINE_PARSER_TOKENS ) & this -> tokens [ this -> tokens_n - 1 ] ; new_token -> token = ( char * ) tq_strndup ( this -> tq , token , 4096 , BPLN ) ; new_token -> token_length = bpstrlen ( new_token -> token ) ; new_token -> token_is_long_form = token_is_long_form ; new_token -> token_required = token_required ; new_token -> value_type = value_type ; if ( token_description ) new_token -> token_description = ( char * ) tq_strdup ( this -> tq , token_description , BPLN ) ; if ( token_usage_example ) new_token -> token_usage_example = ( char * ) tq_strdup ( this -> tq , token_usage_example , BPLN ) ; switch ( value_type ) { case BP_COMMAND_LINE_ARGUMENT_UNSET : break ; case BP_COMMAND_LINE_ARGUMENT_STAND_ALONE_TOGGLE : new_token -> default_value_as_toggle = default_value ? BP_TRUE : BP_FALSE ; break ; case BP_COMMAND_LINE_ARGUMENT_CHAR_STRING : if ( default_value ) new_token -> default_value_as_string = ( char * ) tq_strdup ( this -> tq , ( char * ) default_value , BPLN ) ; new_token -> max_length_for_string = max_length_for_string ; new_token -> min_length_for_string = min_length_for_string ; break ; case BP_COMMAND_LINE_ARGUMENT_SIZE_T : new_token -> default_value_as_size_t = ( size_t ) default_value ; new_token -> max_value_as_size_t = max_value_as_size_t ; new_token -> min_value_as_size_t = min_value_as_size_t ; break ; case BP_COMMAND_LINE_ARGUMENT_SIGNED_INT : memcpy ( & new_token -> default_value_as_int , & default_value , sizeof ( new_token -> default_value_as_int ) ) ; new_token -> max_value_as_integer = max_value_as_integer ; new_token -> min_value_as_integer = min_value_as_integer ; break ; case BP_COMMAND_LINE_ARGUMENT_URL : if ( BP_StringIsURL ( ( char * ) default_value , BP_TRUE ) == ERR_SUCCESS || BP_StringIsURL ( ( char * ) default_value , BP_FALSE ) == ERR_SUCCESS ) { new_token -> default_value_as_string = ( char * ) tq_strdup ( this -> tq , ( char * ) default_value , BPLN ) ; } new_token -> max_length_for_string = max_length_for_string ; new_token -> min_length_for_string = min_length_for_string ; break ; default : break ; } if ( argument_activation_callback ) { new_token -> argument_callback_enabled = BP_TRUE ; new_token -> argument_activation_callback = argument_activation_callback ; } return ERR_SUCCESS ; }
adds a command line option to the parser (this is the fallback routine used by all the
specific argument addition methods and implements all options available.
adds a command line option to the parser ( this is the fallback routine used by all the specific argument addition methods and implements all options available .
// ensure a tailqueue allocator is set for us to work with // ensure we have a token to add // increment the argument count // allocate tokens // if the token was unabled to be created, we have to exit as we're likely out of memory and about to experience a critical // application failure) // zero out the allocated structure // set a reference for readability/use // set token // set the token length // set long form indicator // set token requirement // set the type of value expected for the argument // set the token description // set the token usage // set the default value here // if it's unset, do nothing // default value as toggle // if the value is a string, process string values here // check for default value and add if necessary // now fill in max/min length for string // if the value is a size_t type, process here // set the default value for size_t type here // set the minimum and maximum values for size_t types // if the value is a signed integer type, process here // copy in signed integer directly // set the minimum and maximum values for signed integers // if the argument is a url type, process here // attempt to validate value as a url // since urls are essentially string types, we use string values here // default switch exit if no type can be matched // if we have a callback to activate for this token, set it here // return indicating success