YAAP - Yet Another Argument Parser
A simple commandline argument parser for C
argparse.h
Go to the documentation of this file.
1 #ifndef __ARGPARSE_H__
2 #define __ARGPARSE_H__
3 /*
4 argparse functions
5 
6 Copyright (C) 2018 Dominik Meyer
7 
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
71 #define ARGPARSE_INITIAL_COMMAND_NR 5
72 
73 #define to_argbase(ptr) ((struct arg_base *) ptr)
74 #define to_cmd(ptr) ((struct arg_parse_cmd *)ptr)
75 #define to_str(ptr) ((struct arg_str *)ptr)
76 #define to_flag(ptr) ((struct arg_flag *) ptr)
77 #define to_int(ptr) ((struct arg_int *) ptr)
78 
79 
82 enum argtypes {
87 };
88 
92 struct arg_base
93 {
94  enum argtypes type;
95  int mandatory;
96  int set;
97 };
98 
103 {
104  struct arg_base base;
106  const char *command;
107  const char *description;
108  int (*cb)(int argc, char **argv);
109 };
110 
114 struct arg_str {
115  struct arg_base base;
116  char short_flag;
117  const char *long_flag;
118  char *value;
119  int maxchars;
120  const char *description;
121 };
122 
126 struct arg_flag {
127  struct arg_base base;
128  char short_flag;
129  const char *long_flag;
130  const char *description;
131  int (*cb)(void *ctx, void *userdata);
132 };
133 
137 struct arg_int {
138  struct arg_base base;
139  char short_flag;
140  const char *long_flag;
141  const char *description;
142  int *value;
143 };
144 
145 
146 
151 {
152  void **arguments;
154 };
155 
156 
157 struct arg_parse_ctx * argparse_init();
158 void argparse_free(struct arg_parse_ctx *ctx);
159 int argparse_add_command(struct arg_parse_ctx *ctx, struct arg_parse_cmd *cmd);
160 int argparse_add_string(struct arg_parse_ctx *ctx, struct arg_str *str);
161 int argparse_add_flag(struct arg_parse_ctx *ctx, struct arg_flag *flag);
162 int argparse_add_int(struct arg_parse_ctx *ctx, struct arg_int *integer);
163 int argparse_parse(struct arg_parse_ctx *ctx,int argc, char **argv);
164 
165 #endif
const char * description
short description of the command
Definition: argparse.h:107
const char * long_flag
multi char flag identifying the argument, ignore if NULL
Definition: argparse.h:117
int argparse_add_string(struct arg_parse_ctx *ctx, struct arg_str *str)
add a string argument to the context
Definition: argparse.c:154
char * value
memory already allocated for the string parameter
Definition: argparse.h:118
void argparse_free(struct arg_parse_ctx *ctx)
free the parsing context at program end
Definition: argparse.c:94
int mandatory
is the argument mandatory
Definition: argparse.h:95
structure representing a string command line argument
Definition: argparse.h:114
const char * description
short description of the argument
Definition: argparse.h:141
void ** arguments
array for all argparse elementsdd
Definition: argparse.h:152
an argument with a string parameter (-t hallo, –title=hallo)
Definition: argparse.h:85
char short_flag
one char flag identifying the argument, ignored if NULL
Definition: argparse.h:128
structure representing a command
Definition: argparse.h:102
int nr_arguments
how many arguments are currently registered
Definition: argparse.h:153
const char * command
the name of the command on the commandline, no spaces or special chars allowed, only ascii ...
Definition: argparse.h:106
const char * long_flag
multi char flag identifying the argument, ignore if NULL
Definition: argparse.h:140
struct arg_parse_ctx * argparse_init()
initialize parsing context
Definition: argparse.c:58
structure representing a simple flag command line argument
Definition: argparse.h:126
const char * description
short description of the argument
Definition: argparse.h:130
base structure for a command line argument
Definition: argparse.h:92
int argparse_add_flag(struct arg_parse_ctx *ctx, struct arg_flag *flag)
add a flag argument to the context
Definition: argparse.c:185
an argument with a integer parameter (-s 10, –size=10)
Definition: argparse.h:86
char short_flag
one char flag identifying the argument, ignored if NULL
Definition: argparse.h:116
int maxchars
the maximum number of chars for the string parameters
Definition: argparse.h:119
a simple flag like -v without additional parameter
Definition: argparse.h:84
int argparse_add_int(struct arg_parse_ctx *ctx, struct arg_int *integer)
add a integer argument to the context
Definition: argparse.c:216
char short_flag
one char flag identifying the argument, ignored if NULL
Definition: argparse.h:139
int argparse_parse(struct arg_parse_ctx *ctx, int argc, char **argv)
parse the given commandline in the context
Definition: argparse.c:322
structure representing a simple int command line argument
Definition: argparse.h:137
const char * long_flag
multi char flag identifying the argument, ignore if NULL
Definition: argparse.h:129
int * value
the value of the option
Definition: argparse.h:142
argtypes
supported argument types
Definition: argparse.h:82
int argparse_add_command(struct arg_parse_ctx *ctx, struct arg_parse_cmd *cmd)
add a commandline command to the context
Definition: argparse.c:124
const char * description
short description of the argument
Definition: argparse.h:120
structure for the argparse context, holding argparse specific data structures
Definition: argparse.h:150
a command argument like git uses it (git commit)
Definition: argparse.h:83
int no_command
if this is 1, the callback will be called if no command is given
Definition: argparse.h:105
enum argtypes type
type of the argument
Definition: argparse.h:94