aboutsummaryrefslogtreecommitdiff
path: root/vendor/optparse-master/examples
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-06-25 15:50:13 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-06-25 15:50:21 +0200
commitff415f69a93812418cc60a33dd14452fc6b2901a (patch)
tree1578ef579b958927e64e0835c81339a00fce65fb /vendor/optparse-master/examples
parent0d28ae797b89f05b08b1051f0d2dd72d2416d409 (diff)
Support generation of asm/obj files
Diffstat (limited to 'vendor/optparse-master/examples')
-rw-r--r--vendor/optparse-master/examples/Makefile18
-rw-r--r--vendor/optparse-master/examples/long.c56
-rw-r--r--vendor/optparse-master/examples/short.c47
-rw-r--r--vendor/optparse-master/examples/subcommands.c120
4 files changed, 241 insertions, 0 deletions
diff --git a/vendor/optparse-master/examples/Makefile b/vendor/optparse-master/examples/Makefile
new file mode 100644
index 0000000..11ae2d0
--- /dev/null
+++ b/vendor/optparse-master/examples/Makefile
@@ -0,0 +1,18 @@
+CC = cc
+CFLAGS = -ansi -pedantic -Wall -Wextra -Wno-unused-function -Wno-unused-but-set-variable -g3
+LDFLAGS =
+LDLIBS =
+
+all: short$(EXE) long$(EXE) subcommands$(EXE)
+
+short$(EXE): short.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ short.c ../optparse.h $(LDLIBS)
+
+long$(EXE): long.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ long.c ../optparse.h $(LDLIBS)
+
+subcommands$(EXE): subcommands.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ subcommands.c ../optparse.h $(LDLIBS)
+
+clean:
+ rm -f short$(EXE) long$(EXE) subcommands$(EXE)
diff --git a/vendor/optparse-master/examples/long.c b/vendor/optparse-master/examples/long.c
new file mode 100644
index 0000000..00993fa
--- /dev/null
+++ b/vendor/optparse-master/examples/long.c
@@ -0,0 +1,56 @@
+/* This is free and unencumbered software released into the public domain. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#define OPTPARSE_IMPLEMENTATION
+#define OPTPARSE_API static
+#include "../optparse.h"
+
+int main(int argc, char **argv)
+{
+ struct optparse_long longopts[] = {
+ {"amend", 'a', OPTPARSE_NONE},
+ {"brief", 'b', OPTPARSE_NONE},
+ {"color", 'c', OPTPARSE_REQUIRED},
+ {"delay", 'd', OPTPARSE_OPTIONAL},
+ {0}
+ };
+
+ bool amend = false;
+ bool brief = false;
+ const char *color = "white";
+ int delay = 0;
+
+ char *arg;
+ int option;
+ struct optparse options;
+
+ (void)argc;
+ optparse_init(&options, argv);
+ while ((option = optparse_long(&options, longopts, NULL)) != -1) {
+ switch (option) {
+ case 'a':
+ amend = true;
+ break;
+ case 'b':
+ brief = true;
+ break;
+ case 'c':
+ color = options.optarg;
+ break;
+ case 'd':
+ delay = options.optarg ? atoi(options.optarg) : 1;
+ break;
+ case '?':
+ fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /* Print remaining arguments. */
+ while ((arg = optparse_arg(&options)))
+ printf("%s\n", arg);
+
+ return 0;
+}
diff --git a/vendor/optparse-master/examples/short.c b/vendor/optparse-master/examples/short.c
new file mode 100644
index 0000000..781ea8a
--- /dev/null
+++ b/vendor/optparse-master/examples/short.c
@@ -0,0 +1,47 @@
+/* This is free and unencumbered software released into the public domain. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#define OPTPARSE_IMPLEMENTATION
+#define OPTPARSE_API static
+#include "../optparse.h"
+
+int main(int argc, char **argv)
+{
+ bool amend = false;
+ bool brief = false;
+ const char *color = "white";
+ int delay = 0;
+
+ char *arg;
+ int option;
+ struct optparse options;
+
+ (void)argc;
+ optparse_init(&options, argv);
+ while ((option = optparse(&options, "abc:d::")) != -1) {
+ switch (option) {
+ case 'a':
+ amend = true;
+ break;
+ case 'b':
+ brief = true;
+ break;
+ case 'c':
+ color = options.optarg;
+ break;
+ case 'd':
+ delay = options.optarg ? atoi(options.optarg) : 1;
+ break;
+ case '?':
+ fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /* Print remaining arguments. */
+ while ((arg = optparse_arg(&options)))
+ printf("%s\n", arg);
+ return 0;
+}
diff --git a/vendor/optparse-master/examples/subcommands.c b/vendor/optparse-master/examples/subcommands.c
new file mode 100644
index 0000000..3d6238c
--- /dev/null
+++ b/vendor/optparse-master/examples/subcommands.c
@@ -0,0 +1,120 @@
+/* This is free and unencumbered software released into the public domain. */
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define OPTPARSE_IMPLEMENTATION
+#include "../optparse.h"
+
+static int cmd_echo(char **argv)
+{
+ int i, option;
+ bool newline = true;
+ struct optparse options;
+
+ optparse_init(&options, argv);
+ options.permute = 0;
+ while ((option = optparse(&options, "hn")) != -1) {
+ switch (option) {
+ case 'h':
+ puts("usage: echo [-hn] [ARG]...");
+ return 0;
+ case 'n':
+ newline = false;
+ break;
+ case '?':
+ fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
+ return 1;
+ }
+ }
+ argv += options.optind;
+
+ for (i = 0; argv[i]; i++) {
+ printf("%s%s", i ? " " : "", argv[i]);
+ }
+ if (newline) {
+ putchar('\n');
+ }
+
+ fflush(stdout);
+ return !!ferror(stdout);
+}
+
+static int cmd_sleep(char **argv)
+{
+ int i, option;
+ struct optparse options;
+
+ optparse_init(&options, argv);
+ while ((option = optparse(&options, "h")) != -1) {
+ switch (option) {
+ case 'h':
+ puts("usage: sleep [-h] [NUMBER]...");
+ return 0;
+ case '?':
+ fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
+ return 1;
+ }
+ }
+
+ for (i = 0; argv[i]; i++) {
+ if (sleep(atoi(argv[i]))) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static void
+usage(FILE *f)
+{
+ fprintf(f, "usage: example [-h] <echo|sleep> [OPTION]...\n");
+}
+
+int main(int argc, char **argv)
+{
+ int i, option;
+ char **subargv;
+ struct optparse options;
+
+ static const struct {
+ char name[8];
+ int (*cmd)(char **);
+ } cmds[] = {
+ {"echo", cmd_echo },
+ {"sleep", cmd_sleep},
+ };
+ int ncmds = sizeof(cmds) / sizeof(*cmds);
+
+ (void)argc;
+ optparse_init(&options, argv);
+ options.permute = 0;
+ while ((option = optparse(&options, "h")) != -1) {
+ switch (option) {
+ case 'h':
+ usage(stdout);
+ return 0;
+ case '?':
+ usage(stderr);
+ fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
+ return 1;
+ }
+ }
+
+ subargv = argv + options.optind;
+ if (!subargv[0]) {
+ fprintf(stderr, "%s: missing subcommand\n", argv[0]);
+ usage(stderr);
+ return 1;
+ }
+
+ for (i = 0; i < ncmds; i++) {
+ if (!strcmp(cmds[i].name, subargv[0])) {
+ return cmds[i].cmd(subargv);
+ }
+ }
+ fprintf(stderr, "%s: invalid subcommand: %s\n", argv[0], subargv[0]);
+ return 1;
+}