aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: bff5cc6c760a74780b65c7d53286e694fa6a7386 (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
# LS Args

Minimal, single-header command-line argument parser for C.

- ANSI C / C89
- Header-only
- No macros or code generation
- Extensively unit-tested (90%+ line- and branch coverage)
- Supports short/long options, booleans, strings, and positional arguments
- Supports short options as `-abc` equivalent to `-a -b -c`
- Optional/required argument modes
- Auto-generated help text
- Supports `--` to indicate that all following arguments should be treated as positional, even if they start with `-`

## Quick Start
1. Copy `ls_args.h` to your project.
2. Define and initialize an argument parser:
    ```c
    ls_args args;
    ls_args_init(&args);
    args.help_description = "My program description.";
    ```
3. Register arguments:
    ```c
    int verbose = 0;
    ls_args_bool(&args, &verbose, "v", "verbose", "Enable verbose output", 0);

    const char* output = NULL;
    ls_args_string(&args, &output, "o", "output", "Output file", 0);

    const char* input;
    ls_args_pos_string(&args, &input, "Input file", LS_ARGS_REQUIRED);
    ```
4. Parse arguments:
    ```c
    if (!ls_args_parse(&args, argc, argv)) {
        fprintf(stderr, "%s\n", args.last_error);
        exit(1);
    }
    ```
5. Use parsed values. Free resources when done:
    ```c
    ls_args_free(&args);
    ```
6. Use `ls_args_help()` to generate a help string from provided arguments:
    ```c
    puts(ls_args_help(&args));
    ```
    Example output:
    ```
    Usage: ./my_example [OPTION] <Input file>
    
    Options:
      -v 	--verbose   Enable verbose output
      -o 	--output    Output file
    ```

See [`ls_args.h`](ls_args.h) for detailed documentation and usage patterns.

## License

MIT.