aboutsummaryrefslogtreecommitdiff
path: root/vendor/optparse-master/examples/subcommands.c
blob: 3d6238c5c0e9651157773f590a61b119679085b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
}