forked from cory/tildefriends
Merge branches/quickjs to trunk. This is the way.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3621 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2
deps/xopt/test/.gitignore
vendored
Normal file
2
deps/xopt/test/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/*-test
|
||||
/*.o
|
12
deps/xopt/test/autohelp-1.out
vendored
Normal file
12
deps/xopt/test/autohelp-1.out
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
args: «--help» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
usage: autohelp-test-case [opts...] [--] [extras...]
|
||||
|
||||
Tests the simple parser macro
|
||||
|
||||
-i, --some-int=n Some integer value. Can set to whatever number you like.
|
||||
-f Some float value.
|
||||
--some-double=n Some double value.
|
||||
-?, --help Shows this help message
|
||||
|
||||
[end of arguments]
|
100
deps/xopt/test/autohelp.c
vendored
Normal file
100
deps/xopt/test/autohelp.c
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
0,
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
"autohelp-test-case",
|
||||
0,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (!err) {
|
||||
err = config.help
|
||||
? "--help was passed but autohelp didn't fire"
|
||||
: "--help was not passed - it is required for this test case";
|
||||
}
|
||||
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 0;
|
||||
goto exit;
|
||||
}
|
11
deps/xopt/test/macro-1.out
vendored
Normal file
11
deps/xopt/test/macro-1.out
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
args: «--some-int=10» «--some-double=14.5» «foo» «bar» «--» «--some-other=20»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
help: 0
|
||||
|
||||
extra count: 3
|
||||
- foo
|
||||
- bar
|
||||
- --some-other=20
|
116
deps/xopt/test/macro.c
vendored
Normal file
116
deps/xopt/test/macro.c
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
0,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
3
deps/xopt/test/nocondense-1.out
vendored
Normal file
3
deps/xopt/test/nocondense-1.out
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
args: «-i» «10» «-d» «14.5» «-ssome string» «-m» «-mm» «-mmm» «foo» «bar» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
Error: short option parameters must be separated, not condensed: -ssome string
|
3
deps/xopt/test/nocondense-sloppy-1.out
vendored
Normal file
3
deps/xopt/test/nocondense-sloppy-1.out
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
args: «-i» «10» «-d» «14.5» «-s» «some string» «-m» «-mm» «-mmm» «foo» «bar» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
Error: short options cannot be combined: -mm
|
3
deps/xopt/test/nocondense-sloppy-2.out
vendored
Normal file
3
deps/xopt/test/nocondense-sloppy-2.out
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
args: «-i» «10» «-d» «14.5» «-ssome string» «-m» «-mm» «-mmm» «foo» «bar» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
Error: short options cannot be combined: -mm
|
14
deps/xopt/test/nocondense-sloppy-3.out
vendored
Normal file
14
deps/xopt/test/nocondense-sloppy-3.out
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
args: «-i» «10» «-d» «14.5» «-ssome string» «-m» «-m» «-m» «-m» «-m» «-m» «foo» «bar» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
someString: some string
|
||||
multiCount: 6
|
||||
help: 0
|
||||
|
||||
extra count: 4
|
||||
- foo
|
||||
- bar
|
||||
- --is-not-passed
|
||||
- ignoreme
|
149
deps/xopt/test/nocondense-sloppy.c
vendored
Normal file
149
deps/xopt/test/nocondense-sloppy.c
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
const char *someString;
|
||||
int multiCount;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
static void on_verbose(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
|
||||
(void) v;
|
||||
(void) longArg;
|
||||
(void) err;
|
||||
int *count = (int *)(((char *) data) + option->offset);
|
||||
++*count;
|
||||
}
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"some-string",
|
||||
's',
|
||||
offsetof(SimpleConfig, someString),
|
||||
0,
|
||||
XOPT_TYPE_STRING,
|
||||
"s",
|
||||
"Some string value."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'm',
|
||||
offsetof(SimpleConfig, multiCount),
|
||||
&on_verbose,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Specify multiple times to increase count"
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.someFloat = 0.0f;
|
||||
config.someString = 0;
|
||||
config.multiCount = 0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
XOPT_CTX_SLOPPYSHORTS | XOPT_CTX_NOCONDENSE,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(someString, s);
|
||||
P(multiCount, d);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
149
deps/xopt/test/nocondense.c
vendored
Normal file
149
deps/xopt/test/nocondense.c
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
const char *someString;
|
||||
int multiCount;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
static void on_verbose(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
|
||||
(void) v;
|
||||
(void) longArg;
|
||||
(void) err;
|
||||
int *count = (int *)(((char *) data) + option->offset);
|
||||
++*count;
|
||||
}
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"some-string",
|
||||
's',
|
||||
offsetof(SimpleConfig, someString),
|
||||
0,
|
||||
XOPT_TYPE_STRING,
|
||||
"s",
|
||||
"Some string value."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'm',
|
||||
offsetof(SimpleConfig, multiCount),
|
||||
&on_verbose,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Specify multiple times to increase count"
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.someFloat = 0.0f;
|
||||
config.someString = 0;
|
||||
config.multiCount = 0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
XOPT_CTX_NOCONDENSE,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(someString, s);
|
||||
P(multiCount, d);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
11
deps/xopt/test/optional-longarg-1.out
vendored
Normal file
11
deps/xopt/test/optional-longarg-1.out
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
args: «-i» «10» «-d» «14.5» «foo» «bar» «--» «--some-other=20»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
help: 0
|
||||
|
||||
extra count: 3
|
||||
- foo
|
||||
- bar
|
||||
- --some-other=20
|
116
deps/xopt/test/optional-longarg.c
vendored
Normal file
116
deps/xopt/test/optional-longarg.c
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
0,
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
0,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"macro-test [opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
12
deps/xopt/test/required-1.out
vendored
Normal file
12
deps/xopt/test/required-1.out
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
args: «--some-int=10» «--some-double=14.5» «--some-required=1337» «foo» «bar» «--» «--some-other=20»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
someRequired: 1337
|
||||
help: 0
|
||||
|
||||
extra count: 3
|
||||
- foo
|
||||
- bar
|
||||
- --some-other=20
|
128
deps/xopt/test/required.c
vendored
Normal file
128
deps/xopt/test/required.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
int someRequired;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"some-required",
|
||||
'r',
|
||||
offsetof(SimpleConfig, someRequired),
|
||||
0,
|
||||
XOPT_TYPE_INT | XOPT_REQUIRED,
|
||||
"n",
|
||||
"Some value"
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.someRequired = 0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
0,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(someRequired, d);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
11
deps/xopt/test/simple-1.out
vendored
Normal file
11
deps/xopt/test/simple-1.out
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
args: «--some-int=10» «--some-double=14.5» «foo» «bar» «--» «--some-other=20»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
help: 0
|
||||
|
||||
extra count: 3
|
||||
- foo
|
||||
- bar
|
||||
- --some-other=20
|
128
deps/xopt/test/simple.c
vendored
Normal file
128
deps/xopt/test/simple.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int result;
|
||||
const char *err;
|
||||
xoptContext *ctx;
|
||||
SimpleConfig config;
|
||||
const char **extras = 0;
|
||||
const char **extrasPtr = 0;
|
||||
int extraCount;
|
||||
|
||||
result = 0;
|
||||
err = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.help = 0;
|
||||
|
||||
/* create context */
|
||||
ctx = xopt_context("xopt-test", options,
|
||||
XOPT_CTX_POSIXMEHARDER | XOPT_CTX_STRICT, &err);
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
result = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* parse */
|
||||
extraCount = xopt_parse(ctx, argc, argv, &config, &extras, &err);
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
result = 2;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* help? */
|
||||
if (config.help) {
|
||||
xoptAutohelpOptions opts;
|
||||
opts.usage = "[options] [extras...]";
|
||||
opts.prefix = "A simple demonstration of the XOpt options parser library.";
|
||||
opts.suffix = "End argument list.";
|
||||
opts.spacer = 10;
|
||||
|
||||
xopt_autohelp(ctx, stderr, &opts, &err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit:
|
||||
if (extras) free(extras); /* DO NOT free individual strings */
|
||||
if (ctx) free(ctx); /* they point to argv strings */
|
||||
return result;
|
||||
}
|
14
deps/xopt/test/sloppyshorts-1.out
vendored
Normal file
14
deps/xopt/test/sloppyshorts-1.out
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
args: «-i10» «-d» «14.5» «-ssome string» «-m» «-mm» «-mmm» «foo» «bar» «--» «--is-not-passed» «ignoreme»
|
||||
|
||||
someInt: 10
|
||||
someFloat: 0.000000
|
||||
someDouble: 14.500000
|
||||
someString: some string
|
||||
multiCount: 6
|
||||
help: 0
|
||||
|
||||
extra count: 4
|
||||
- foo
|
||||
- bar
|
||||
- --is-not-passed
|
||||
- ignoreme
|
149
deps/xopt/test/sloppyshorts.c
vendored
Normal file
149
deps/xopt/test/sloppyshorts.c
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../xopt.h"
|
||||
|
||||
typedef struct {
|
||||
int someInt;
|
||||
float someFloat;
|
||||
double someDouble;
|
||||
const char *someString;
|
||||
int multiCount;
|
||||
bool help;
|
||||
} SimpleConfig;
|
||||
|
||||
static void on_verbose(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
|
||||
(void) v;
|
||||
(void) longArg;
|
||||
(void) err;
|
||||
int *count = (int *)(((char *) data) + option->offset);
|
||||
++*count;
|
||||
}
|
||||
|
||||
xoptOption options[] = {
|
||||
{
|
||||
"some-int",
|
||||
'i',
|
||||
offsetof(SimpleConfig, someInt),
|
||||
0,
|
||||
XOPT_TYPE_INT,
|
||||
"n",
|
||||
"Some integer value. Can set to whatever number you like."
|
||||
},
|
||||
{
|
||||
"some-float",
|
||||
'f',
|
||||
offsetof(SimpleConfig, someFloat),
|
||||
0,
|
||||
XOPT_TYPE_FLOAT,
|
||||
"n",
|
||||
"Some float value."
|
||||
},
|
||||
{
|
||||
"some-double",
|
||||
'd',
|
||||
offsetof(SimpleConfig, someDouble),
|
||||
0,
|
||||
XOPT_TYPE_DOUBLE,
|
||||
"n",
|
||||
"Some double value."
|
||||
},
|
||||
{
|
||||
"some-string",
|
||||
's',
|
||||
offsetof(SimpleConfig, someString),
|
||||
0,
|
||||
XOPT_TYPE_STRING,
|
||||
"s",
|
||||
"Some string value."
|
||||
},
|
||||
{
|
||||
0,
|
||||
'm',
|
||||
offsetof(SimpleConfig, multiCount),
|
||||
&on_verbose,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Specify multiple times to increase count"
|
||||
},
|
||||
{
|
||||
"help",
|
||||
'?',
|
||||
offsetof(SimpleConfig, help),
|
||||
0,
|
||||
XOPT_TYPE_BOOL,
|
||||
0,
|
||||
"Shows this help message"
|
||||
},
|
||||
XOPT_NULLOPTION
|
||||
};
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int exit_code = 1;
|
||||
const char *err = NULL;
|
||||
SimpleConfig config;
|
||||
const char **extras = NULL;
|
||||
const char **extrasPtr = NULL;
|
||||
int extraCount = 0;
|
||||
|
||||
/* show arguments */
|
||||
fputs("args:", stderr);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
fprintf(stderr, " «%s»", argv[i]);
|
||||
}
|
||||
fputs("\n\n", stderr);
|
||||
|
||||
/* setup defaults */
|
||||
config.someInt = 0;
|
||||
config.someDouble = 0.0;
|
||||
config.someFloat = 0.0f;
|
||||
config.someString = 0;
|
||||
config.multiCount = 0;
|
||||
config.help = 0;
|
||||
|
||||
XOPT_SIMPLE_PARSE(
|
||||
argv[0],
|
||||
XOPT_CTX_SLOPPYSHORTS,
|
||||
&options[0], &config,
|
||||
argc, argv,
|
||||
&extraCount, &extras,
|
||||
&err,
|
||||
stderr,
|
||||
"[opts...] [--] [extras...]",
|
||||
"Tests the simple parser macro",
|
||||
"[end of arguments]",
|
||||
15);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr, "Error: %s\n", err);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* print */
|
||||
#define P(field, delim) fprintf(stderr, #field ":\t%" #delim "\n", \
|
||||
config.field)
|
||||
|
||||
P(someInt, d);
|
||||
P(someFloat, f);
|
||||
P(someDouble, f);
|
||||
P(someString, s);
|
||||
P(multiCount, d);
|
||||
P(help, d);
|
||||
|
||||
fprintf(stderr, "\nextra count: %d\n", extraCount);
|
||||
extrasPtr = extras;
|
||||
while (extraCount--) {
|
||||
fprintf(stderr, "- %s\n", *extrasPtr++);
|
||||
}
|
||||
|
||||
#undef P
|
||||
|
||||
exit_code = 0;
|
||||
exit:
|
||||
free(extras); /* DO NOT free individual strings */
|
||||
return exit_code;
|
||||
xopt_help:
|
||||
exit_code = 2;
|
||||
goto exit;
|
||||
}
|
46
deps/xopt/test/test-case.sh
vendored
Executable file
46
deps/xopt/test/test-case.sh
vendored
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
#
|
||||
# To be used with CMake builds located in <xopt>/build/
|
||||
#
|
||||
|
||||
set -uo pipefail
|
||||
exec >&2
|
||||
|
||||
casebin="$1"
|
||||
caseout="$2"
|
||||
shift 2
|
||||
|
||||
function die {
|
||||
echo -e "error: $*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
if [ -z "$casebin" ]; then
|
||||
die 'no test case executable specified'
|
||||
fi
|
||||
|
||||
if [ -z "$caseout" ]; then
|
||||
die 'no test case output (some-case.out) specified'
|
||||
fi
|
||||
|
||||
if [ ! -x "$casebin" ]; then
|
||||
die "test case does not exist or is not executable: $casebin"
|
||||
fi
|
||||
|
||||
if [ ! -f "$caseout" ]; then
|
||||
die "test case expected output file does not exist: $caseout"
|
||||
fi
|
||||
|
||||
output="$("$casebin" "$@" 2>&1)"
|
||||
r=$?
|
||||
if [ $r -eq 139 ]; then
|
||||
die "xopt test case failed with SEGFAULT ($r)"
|
||||
fi
|
||||
|
||||
diff="$(diff -U0 -d -t "$caseout" - <<< "$output" 2>&1)"
|
||||
if [ ! -z "$diff" ]; then
|
||||
die "xopt test case didn't match expected output: '$caseout'\n$diff"
|
||||
fi
|
Reference in New Issue
Block a user