ADD: added support for integer argument

This commit is contained in:
Dominik Meyer 2018-04-01 15:46:38 +02:00
parent afc822563c
commit 35bcf399b7
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
2 changed files with 82 additions and 1 deletions

View File

@ -205,6 +205,37 @@ int argparse_add_flag(struct arg_parse_ctx *ctx, struct arg_flag *flag)
return 0;
}
/**
* @brief add an arg_int to the context for later parsing
*
* @param ctx - the context the command should be added to
* @param integer - the arg_int to add
*
* @return 0 = everything is fine, -1 error occured
*/
int argparse_add_int(struct arg_parse_ctx *ctx, struct arg_int *integer)
{
if (ctx == NULL || integer == NULL)
{
return -1;
}
integer->base.type=ARG_INT;
// check if the array size has to be increased
if (ctx->nr_arguments % ARGPARSE_INITIAL_COMMAND_NR == 0)
{
ctx->arguments= (void **) realloc(ctx->arguments, sizeof(void *)*(ctx->nr_arguments+ARGPARSE_INITIAL_COMMAND_NR));
}
// append the command to the array
ctx->arguments[ctx->nr_arguments]=integer;
ctx->nr_arguments++;
return 0;
}
/**
* @brief internal function to print usage information, not exported to user
*
@ -249,6 +280,11 @@ void argparse_usage(struct arg_parse_ctx *ctx, char *program)
to_flag(ctx->arguments[i])->long_flag,
to_flag(ctx->arguments[i])->description);
break;
case ARG_INT:
printf("\t-%c <integer> | --%s=<integer>\t\t%s\n", to_flag(ctx->arguments[i])->short_flag,
to_flag(ctx->arguments[i])->long_flag,
to_flag(ctx->arguments[i])->description);
break;
default:
break;
};
@ -378,6 +414,37 @@ int argparse_parse(struct arg_parse_ctx *ctx, int argc, char **argv)
}
}
}
else if (argv[i][0]=='-' && to_argbase(ctx->arguments[r])->type == ARG_INT)
{
// check for long argument format or short
if(argv[i][1]=='-')
{
char *tok=strtok(argv[i],"=");
if (strcmp(tok+2,to_int(ctx->arguments[r])->long_flag)==0)
{
to_int(ctx->arguments[r])->base.set=1;
tok=strtok(NULL,"=");
found=1;
if (to_int(ctx->arguments[r])->value != NULL)
{
*(to_int(ctx->arguments[r])->value)=(int) strtol(tok,NULL,10);
}
}
}
else
{
if(argv[i][1] == to_int(ctx->arguments[r])->short_flag)
{
found=1;
to_int(ctx->arguments[r])->base.set=1;
if (to_int(ctx->arguments[r])->value != NULL)
{
*(to_int(ctx->arguments[r])->value)=(int) strtol(argv[i+1],NULL,10);
}
i++;
}
}
}
}
if (found == 0)

View File

@ -73,6 +73,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define to_cmd(ptr) ((struct arg_parse_cmd *)ptr) ///< convert a pointer to an arg_parse_cmd structure pointer
#define to_str(ptr) ((struct arg_str *)ptr) ///< convert a pointer to an arg_str structure pointer
#define to_flag(ptr) ((struct arg_flag *) ptr) ///< convert a pointer to an arg_flag structure pointer
#define to_int(ptr) ((struct arg_int *) ptr) ///< convert a pointer to an arg_int structure pointer
/**
* @brief supported argument types
@ -119,7 +120,7 @@ struct arg_str {
};
/**
* qbrief structure repsenting a simple flag command line argument
* @brief structure representing a simple flag command line argument
*/
struct arg_flag {
struct arg_base base; ///< base of the command line argument
@ -129,6 +130,18 @@ struct arg_flag {
int (*cb)(void *ctx, void *userdata); ///< callback called if argument is found
};
/**
* @brief structure representing a simple int command line argument
*/
struct arg_int {
struct arg_base base; ///< base of the command line argument
char short_flag; ///< one char flag identifying the argument, ignored if NULL
const char *long_flag; ///< multi char flag identifying the argument, ignore if NULL
const char *description; ///< short description of the argument
int *value; ///< the value of the option
};
/**
* @brief structure for the argparse context, holding argparse specific data structures
@ -145,6 +158,7 @@ void argparse_free(struct arg_parse_ctx *ctx);
int argparse_add_command(struct arg_parse_ctx *ctx, struct arg_parse_cmd *cmd); ///< add a commandline command to the context
int argparse_add_string(struct arg_parse_ctx *ctx, struct arg_str *str); ///< add a string argument to the context
int argparse_add_flag(struct arg_parse_ctx *ctx, struct arg_flag *flag); ///< add a flag argument to the context
int argparse_add_int(struct arg_parse_ctx *ctx, struct arg_int *integer); ///< add a integer argument to the context
int argparse_parse(struct arg_parse_ctx *ctx,int argc, char **argv); ///< parse the given commandline in the context
#endif