aboutsummaryrefslogtreecommitdiff
path: root/examples/basic_example.c
blob: 5c6c3bd59655d833c7d71c7cdd64f1495a267e7a (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
#define LS_ARGS_IMPLEMENTATION
#include "ls_args.h"

int main(int argc, char** argv) {
    ls_args args;
    int help = 0;
    const char* outfile = "out.txt";
    const char* infile;
    const char* testfile = NULL;

    ls_args_init(&args);
    args.help_description = "An example program to show how arguments work. "
                            "Provide an input file and optionally an output "
                            "file and test file and see what happens!";
    ls_args_bool(&args, &help, "h", "help", "Prints help", 0);
    ls_args_string(&args, &outfile, "o", "out",
        "Specify the outfile, default 'out.txt'", 0);
    ls_args_pos_string(&args, &infile, "input file", LS_ARGS_REQUIRED);
    ls_args_pos_string(&args, &testfile, "test file", 0);
    if (!ls_args_parse(&args, argc, argv)) {
        if (help) {
            puts(ls_args_help(&args));
        } else {
            printf("Error: %s\n", args.last_error);
        }
        ls_args_free(&args);
        return 1;
    }

    printf("Got input file: %s\n", infile);
    printf("Got output file: %s\n", outfile);
    printf("Got test file: %s\n", testfile ? testfile : "(null)");

    ls_args_free(&args);
    return 0;
}