Skip to content

Commit

Permalink
Improve ADD_ARG
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipon committed Apr 28, 2022
1 parent 6d03705 commit 1a9cbe3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
33 changes: 25 additions & 8 deletions cTools/libs/args/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ For adding new agument just use: ADD_ARG.
```
/***
* hand - pointer to handler for argument
* Name - full argument name (--Name)
* Key - short argument name (-Key)
* Arg - extra hint for help. If Arg="VALUE"
* name - full argument name (--Name)
* key - short argument name (-Key)
* arg - extra hint for help. If Arg="VALUE"
* help will print --Name=VALUE
* Flags - argp option flags. See more:
* flags - argp option flags. See more:
* https://www.gnu.org/software/libc/manual/html_node/Argp-Option-Flags.html
* Doc - description of argument
* doc - description of argument
*/
ADD_ARG(hand, Name, Key, Arg, Flags, Doc);
ADD_ARG(hand, .name = Name
, .key = Key
, .arg = Arg
, .flags = Flags
, .doc = Doc
);
```

_Name, Key, Arg, Flags, Doc_ are the same as [struct argp\_option](https://www.gnu.org/software/libc/manual/html_node/Argp-Option-Vectors.html).
_name, key, arg, flags, doc_ are the same as [struct argp\_option](https://www.gnu.org/software/libc/manual/html_node/Argp-Option-Vectors.html).

### Add program description for --help
For adding extra description just use: ADD_DOC.
Expand All @@ -49,13 +54,20 @@ static char argsDoc[] = "ARG0 ARG1";
ADD_ARGS_DOC(argsDoc);
```

### Add program version for --version
```
const char progVersion[] = "0.0.1";
ADD_VERSION(progVersion);
```

### Full example
```
#include "args.h"
#include <stdbool.h>
static char doc[] = "Test args library";
static char argsDoc[] = "ARG0 ARG1";
const char progVersion[] = "0.0.1";
bool isVerbose = false;
static void argVerbose(char *arg)
Expand All @@ -67,7 +79,11 @@ int main(int argc, char **argv)
{
ADD_DOC(doc);
ADD_ARGS_DOC(argsDoc);
ADD_ARG(argVerbose, "verbose", 'v', 0 , 0, "Produce verbose output");
ADD_VERSION(progVersion);
ADD_ARG(argVerbose, .name = "verbose"
, .key = 'v'
, .doc = "Produce verbose output"
);
ARG_PARSE(argc, argv);
return 0;
Expand All @@ -81,6 +97,7 @@ int main(int argc, char **argv)
* -v, --verbose Produce verbose output
* -?, --help Give this help list
* --usage Give a short usage message
* -V, --version Print program version
*/
```

16 changes: 11 additions & 5 deletions cTools/libs/args/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static struct argp_option* opts = NULL;
static ARG_HAND *hands = NULL;
static struct argp argp = {0};

int addArg(ARG_HAND hand, const struct argp_option* opt)
int addArg(const Arg* arg)
{
if (NUM_OPTS == OPTS_LEN) {
struct argp_option* new_opts = Calloc(NUM_OPTS + 10, sizeof(struct argp_option));
Expand All @@ -52,8 +52,8 @@ int addArg(ARG_HAND hand, const struct argp_option* opt)
OPTS_LEN += 10;
}

memcpy(opts + NUM_OPTS, opt, sizeof(struct argp_option));
hands[NUM_OPTS] = hand;
memcpy(opts + NUM_OPTS, arg, sizeof(struct argp_option));
hands[NUM_OPTS] = arg->hand;
++NUM_OPTS;

return 0;
Expand All @@ -71,6 +71,12 @@ void addArgsDoc(const char* argsDoc)
argp.args_doc = argsDoc;
}

void addVersion(const char* version)
{
// version of program
argp_program_version = version;
}

static ARG_HAND getHandle(int key)
{
size_t i = 0;
Expand Down Expand Up @@ -103,7 +109,7 @@ parseOpt(int key, char *arg, struct argp_state *state)
error_t argParse(int argc, char** argv)
{
// Add last elem argp_option
ADD_ARG(NULL, 0, 0, 0, 0, 0);
ADD_ARG(NULL);
argp.parser = parseOpt;
return argp_parse( &argp
, argc
Expand All @@ -116,6 +122,6 @@ error_t argParse(int argc, char** argv)
, NULL
);

return 0;
//return 0;
}

53 changes: 33 additions & 20 deletions cTools/libs/args/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,47 @@

typedef void(*ARG_HAND)(char*);

/***
* hand - pointer to handler for argument
* Name - full argument name (--Name)
* Key - short argument name (-Key)
* Arg - extra hint for help. If Arg="VALUE"
* help will print --Name=VALUE
* Flags - argp option flags. See
* https://www.gnu.org/software/libc/manual/html_node/Argp-Option-Flags.html
* Doc - description of argument
*/
#define ADD_ARG(hand, Name, Key, Arg, Flags, Doc) { \
struct argp_option loc_opt = { \
.name = Name, \
.key = Key, \
.arg = Arg, \
.flags = Flags, \
.doc = Doc, \
}; \
addArg(hand, &loc_opt); \
typedef struct Arg {
// full argument name (--Name)
const char *name;

// short argument name (-Key)
int key;
// extra hint for help. If Arg="VALUE"
// help will print --Name=VALUE (type of arg)
const char *arg;

// argp option flags. See
// https://www.gnu.org/software/libc/manual/html_node/Argp-Option-Flags.html
int flags;

// description of argument
const char *doc;

int group;

// handler for argument
ARG_HAND hand;
} Arg;

#define ADD_ARG(h, ...) { \
Arg loc_arg = { \
.hand = h, \
__VA_ARGS__ \
}; \
addArg(&loc_arg); \
}
int addArg(ARG_HAND hand, const struct argp_option* opt);
int addArg(const Arg* arg);

#define ADD_DOC(doc) addDoc(doc);
void addDoc(const char* doc);

#define ADD_ARGS_DOC(argsDoc) addArgsDoc(argsDoc);
void addArgsDoc(const char* argsDoc);

#define ADD_VERSION(version) addVersion(version);
void addVersion(const char* version);

#define ARG_PARSE(argc, argv) argParse(argc, argv);
error_t argParse(int argc, char** argv);

Expand Down
8 changes: 7 additions & 1 deletion cTools/libs/args/test/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static char doc[] = "Test args library";
// A description of the arguments we accept.
static char argsDoc[] = "ARG0 ARG1";

const char progVersion[] = "0.0.1";

bool isVerbose = false;

static void argVerbose(char *arg)
Expand All @@ -46,7 +48,11 @@ int main(int argc, char **argv)
{
ADD_DOC(doc);
ADD_ARGS_DOC(argsDoc);
ADD_ARG(argVerbose, "verbose", 'v', 0 , 0, "Produce verbose output");
ADD_ARG(argVerbose, .name = "verbose"
, .key = 'v'
, .doc = "Produce verbose output"
);
ADD_VERSION(progVersion);
ARG_PARSE(argc, argv);

EXPECT_BOOL_EQ(isVerbose, true);
Expand Down

0 comments on commit 1a9cbe3

Please sign in to comment.