zig-clap/example
2021-07-06 09:00:25 +00:00
..
help.zig Update to latest zig in preperation for 0.8.0 2021-05-26 21:06:10 +02:00
README.md.template Update to latest zig in preperation for 0.8.0 2021-05-26 21:06:10 +02:00
simple-error.zig Modernize codebase 2021-05-08 18:08:52 +02:00
simple-ex.zig Update to latest zig in preperation for 0.8.0 2021-05-26 21:06:10 +02:00
simple.zig Update to latest zig in preperation for 0.8.0 2021-05-26 21:06:10 +02:00
streaming-clap.zig Update to latest zig in preperation for 0.8.0 2021-05-26 21:06:10 +02:00
usage.zig Update example of usage (#45) 2021-07-06 09:00:25 +00:00

# zig-clap

A simple and easy to use command line argument parser library for Zig.

Looking for a version that works with `zig master`? The `zig-master` branch has
you covered. It is maintained by people who live at head (not me) and is merged
into master on every `zig` release.

## Features

* Short arguments `-a`
  * Chaining `-abc` where `a` and `b` does not take values.
* Long arguments `--long`
* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`)
  * Short args also support passing values with no spacing or `=` (`-a100`)
  * This all works with chaining (`-ba 100`, `-ba=100`, `-ba100`)
* Supports options that can be specified multiple times (`-e 1 -e 2 -e 3`)
* Print help message from parameter specification.
* Parse help message to parameter specification.

## Examples

### `clap.parse`

The simplest way to use this library is to just call the `clap.parse` function.

```zig
{s}
```

The data structure returned has lookup speed on par with array access (`arr[i]`) and validates
that the strings you pass to `option`, `options` and `flag` are actually parameters that the
program can take:

```zig
{s}
```

```
zig-clap/clap/comptime.zig:109:17: error: --helps is not a parameter.
                @compileError(name ++ " is not a parameter.");
                ^
zig-clap/clap/comptime.zig:77:45: note: called from here
            const param = comptime findParam(name);
                                            ^
zig-clap/clap.zig:238:31: note: called from here
            return a.clap.flag(name);
                              ^
zig-clap/example/simple-error.zig:16:18: note: called from here
    _ = args.flag("--helps");
```

There is also a `parseEx` variant that takes an argument iterator.

### `StreamingClap`

The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
`args.Iterator` to provide it with arguments lazily.

```zig
{s}
```

Currently, this parse is the only parser that allow an array of `Param` tha
is generated at runtime.

### `help`

The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the
program can take.

```zig
{s}
```

```
	-h, --help   	Display this help and exit.
	-v, --version	Output version information and exit.
```

The `help` functions are the simplest to call. It only takes an `OutStream` and a slice of
`Param(Help)`.

The `helpEx` is the generic version of `help`. It can print a help message for any
`Param` give that the caller provides functions for getting the help and value strings.

The `helpFull` is even more generic, allowing the functions that get the help and value strings
to return errors and take a context as a parameter.

### `usage`

The `usage`, `usageEx` and `usageFull` are functions for printing a small abbreviated version
of the help message.

```zig
{s}
```

```
[-hv] [--value <N>]
```