Commit 44fc72c5 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Format using the SWIFT standard

parent 35b825a8
This diff is collapsed.
......@@ -18,36 +18,36 @@ extern "C" {
struct argparse;
struct argparse_option;
typedef int argparse_callback (struct argparse *self,
const struct argparse_option *option);
typedef int argparse_callback(struct argparse *self,
const struct argparse_option *option);
enum argparse_flag {
ARGPARSE_STOP_AT_NON_OPTION = 1,
ARGPARSE_STOP_AT_NON_OPTION = 1,
};
enum argparse_option_type {
/* special */
ARGPARSE_OPT_END,
ARGPARSE_OPT_GROUP,
/* options with no arguments */
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_BIT,
/* options with arguments (optional or required) */
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_FLOAT,
ARGPARSE_OPT_STRING,
/* special */
ARGPARSE_OPT_END,
ARGPARSE_OPT_GROUP,
/* options with no arguments */
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_BIT,
/* options with arguments (optional or required) */
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_FLOAT,
ARGPARSE_OPT_STRING,
};
enum argparse_option_flags {
OPT_NONEG = 1, /* disable negation */
OPT_NONEG = 1, /* disable negation */
};
/**
* argparse option
*
* `type`:
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
* array.
* holds the type of the option, you must have an ARGPARSE_OPT_END last in
* your array.
*
* `short_name`:
* the character to use as a short option name, '\0' if none.
......@@ -72,32 +72,32 @@ enum argparse_option_flags {
* option flags.
*/
struct argparse_option {
enum argparse_option_type type;
const char short_name;
const char *long_name;
void *value;
const char *help;
argparse_callback *callback;
intptr_t data;
int flags;
enum argparse_option_type type;
const char short_name;
const char *long_name;
void *value;
const char *help;
argparse_callback *callback;
intptr_t data;
int flags;
};
/**
* argpparse
*/
struct argparse {
// user supplied
const struct argparse_option *options;
const char *const *usages;
int flags;
const char *description; // a description after usage
const char *epilog; // a description at the end
// internal context
int argc;
const char **argv;
const char **out;
int cpidx;
const char *optvalue; // current option value
// user supplied
const struct argparse_option *options;
const char *const *usages;
int flags;
const char *description; // a description after usage
const char *epilog; // a description at the end
// internal context
int argc;
const char **argv;
const char **out;
int cpidx;
const char *optvalue; // current option value
};
// built-in callbacks
......@@ -105,16 +105,23 @@ int argparse_help_cb(struct argparse *self,
const struct argparse_option *option);
// built-in option macros
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ }
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 }
#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \
"show this help message and exit", \
argparse_help_cb, 0, 0)
#define OPT_END() \
{ ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
#define OPT_BOOLEAN(...) \
{ ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_BIT(...) \
{ ARGPARSE_OPT_BIT, __VA_ARGS__ }
#define OPT_INTEGER(...) \
{ ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_FLOAT(...) \
{ ARGPARSE_OPT_FLOAT, __VA_ARGS__ }
#define OPT_STRING(...) \
{ ARGPARSE_OPT_STRING, __VA_ARGS__ }
#define OPT_GROUP(h) \
{ ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 }
#define OPT_HELP() \
OPT_BOOLEAN('h', "help", NULL, "show this help message and exit", \
argparse_help_cb, 0, 0)
int argparse_init(struct argparse *self, struct argparse_option *options,
const char *const *usages, int flags);
......
......@@ -9,76 +9,72 @@ static const char *const usages[] = {
NULL,
};
#define PERM_READ (1<<0)
#define PERM_WRITE (1<<1)
#define PERM_EXEC (1<<2)
#define PERM_READ (1 << 0)
#define PERM_WRITE (1 << 1)
#define PERM_EXEC (1 << 2)
struct stuff {
const char *path[10];
int npath;
const char *path[10];
int npath;
};
static int callback(struct argparse *self, const struct argparse_option *opt)
{
printf("Called back... %s\n", *(char **)opt->value);
struct stuff *data = (struct stuff *)opt->data;
data->path[data->npath] = *(char **)opt->value;
data->npath++;
return 1;
static int callback(struct argparse *self, const struct argparse_option *opt) {
printf("Called back... %s\n", *(char **)opt->value);
struct stuff *data = (struct stuff *)opt->data;
data->path[data->npath] = *(char **)opt->value;
data->npath++;
return 1;
}
int
main(int argc, const char **argv)
{
int force = 0;
int self_gravity = 0;
int int_num = 0;
float flt_num = 0.f;
struct stuff data;
data.npath = 0;
data.path[0] = NULL;
const char *buffer;
int perms = 0;
int npath;
int main(int argc, const char **argv) {
int force = 0;
int self_gravity = 0;
int int_num = 0;
float flt_num = 0.f;
struct stuff data;
data.npath = 0;
data.path[0] = NULL;
const char *buffer;
int perms = 0;
int npath;
struct argparse_option options[] = {
OPT_HELP(),
OPT_GROUP("Basic options"),
OPT_BOOLEAN('f', "force", &force, "force to do", NULL, 0, 0),
OPT_BOOLEAN(0, "self-gravity", &self_gravity, "use self gravity",
NULL, 0, 0),
OPT_STRING('P', "path", &buffer, "path to read", &callback,
(intptr_t)&data , 0),
OPT_INTEGER('i', "int", &int_num, "selected integer", NULL, 0, 0),
OPT_FLOAT('s', "float", &flt_num, "selected float", NULL, 0, 0),
OPT_END(),
};
struct argparse_option options[] = {
OPT_HELP(),
OPT_GROUP("Basic options"),
OPT_BOOLEAN('f', "force", &force, "force to do", NULL, 0, 0),
OPT_BOOLEAN(0, "self-gravity", &self_gravity, "use self gravity", NULL, 0,
0),
OPT_STRING('P', "path", &buffer, "path to read", &callback,
(intptr_t)&data, 0),
OPT_INTEGER('i', "int", &int_num, "selected integer", NULL, 0, 0),
OPT_FLOAT('s', "float", &flt_num, "selected float", NULL, 0, 0),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, usages, 0);
argparse_describe(&argparse, "\nA brief description of what the program does and how it works.", "\nAdditional description of the program after the description of the arguments.");
argc = argparse_parse(&argparse, argc, argv);
if (force != 0)
printf("force: %d\n", force);
if (self_gravity != 0)
printf("self_gravity: %d\n", self_gravity);
if (data.npath > 0) {
for (int i = 0 ; i < data.npath; i++)
printf("path: %s\n", data.path[i]);
struct argparse argparse;
argparse_init(&argparse, options, usages, 0);
argparse_describe(
&argparse,
"\nA brief description of what the program does and how it works.",
"\nAdditional description of the program after the description of the "
"arguments.");
argc = argparse_parse(&argparse, argc, argv);
if (force != 0) printf("force: %d\n", force);
if (self_gravity != 0) printf("self_gravity: %d\n", self_gravity);
if (data.npath > 0) {
for (int i = 0; i < data.npath; i++) printf("path: %s\n", data.path[i]);
}
if (int_num != 0) printf("int_num: %d\n", int_num);
if (flt_num != 0) printf("flt_num: %g\n", flt_num);
if (argc != 0) {
printf("argc: %d\n", argc);
int i;
for (i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, *(argv + i));
}
if (int_num != 0)
printf("int_num: %d\n", int_num);
if (flt_num != 0)
printf("flt_num: %g\n", flt_num);
if (argc != 0) {
printf("argc: %d\n", argc);
int i;
for (i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, *(argv + i));
}
}
if (perms) {
printf("perms: %d\n", perms);
}
return 0;
}
if (perms) {
printf("perms: %d\n", perms);
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment