diff options
| author | 2018-11-15 12:53:46 +0100 | |
|---|---|---|
| committer | 2018-11-15 12:53:46 +0100 | |
| commit | 1a219fc680faa79f1236912b14fd58754313df87 (patch) | |
| tree | 94ee1aee0dd89f0a40d1ab04156dd3dcb7965115 /README.md | |
| parent | Zig fmt (diff) | |
| download | zig-clap-1a219fc680faa79f1236912b14fd58754313df87.tar.gz zig-clap-1a219fc680faa79f1236912b14fd58754313df87.tar.xz zig-clap-1a219fc680faa79f1236912b14fd58754313df87.zip | |
Added help function
Diffstat (limited to '')
| -rw-r--r-- | README.md | 73 |
1 files changed, 73 insertions, 0 deletions
| @@ -107,3 +107,76 @@ zig-clap/example/comptime-clap.zig:41:18: note: called from here | |||
| 107 | ``` | 107 | ``` |
| 108 | 108 | ||
| 109 | Ofc, this limits you to use only parameters that are comptime known. | 109 | Ofc, this limits you to use only parameters that are comptime known. |
| 110 | |||
| 111 | ### `help` | ||
| 112 | |||
| 113 | The `help` and `helpEx` are functions for printing a simple list of all parameters the | ||
| 114 | program can take. | ||
| 115 | |||
| 116 | ```rust | ||
| 117 | const stderr_file = try std.io.getStdErr(); | ||
| 118 | var stderr_out_stream = stderr_file.outStream(); | ||
| 119 | const stderr = &stderr_out_stream.stream; | ||
| 120 | |||
| 121 | try clap.help( | ||
| 122 | stderr, | ||
| 123 | []clap.Param([]const u8){ | ||
| 124 | clap.Param([]const u8).flag( | ||
| 125 | "Display this help and exit.", | ||
| 126 | clap.Names.prefix("help"), | ||
| 127 | ), | ||
| 128 | clap.Param([]const u8).flag( | ||
| 129 | "Output version information and exit.", | ||
| 130 | clap.Names.prefix("version"), | ||
| 131 | ), | ||
| 132 | }, | ||
| 133 | ); | ||
| 134 | ``` | ||
| 135 | |||
| 136 | ``` | ||
| 137 | -h, --help Display this help and exit. | ||
| 138 | -v, --version Output version information and exit. | ||
| 139 | ``` | ||
| 140 | |||
| 141 | The `help` function is the simplest to call. It only takes an `OutStream` and a slice of | ||
| 142 | `Param([]const u8)`. This function assumes that the id of each parameter is the help message. | ||
| 143 | |||
| 144 | The `clap.helpEx` is the generic version of `help`. It can print a help message for any | ||
| 145 | `Param`, but requires some extra arguments for it to work. | ||
| 146 | |||
| 147 | ```rust | ||
| 148 | fn getHelp(_: void, param: clap.Param(u8)) error{}![]const u8 { | ||
| 149 | return switch (param.id) { | ||
| 150 | 'h' => "Display this help and exit.", | ||
| 151 | 'v' => "Output version information and exit.", | ||
| 152 | else => unreachable, | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | |||
| 156 | fn getValue(_: void, param: clap.Param(u8)) error{}![]const u8 { | ||
| 157 | return ""; | ||
| 158 | } | ||
| 159 | |||
| 160 | const stderr_file = try std.io.getStdErr(); | ||
| 161 | var stderr_out_stream = stderr_file.outStream(); | ||
| 162 | const stderr = &stderr_out_stream.stream; | ||
| 163 | |||
| 164 | try stderr.print("\n"); | ||
| 165 | try clap.helpEx( | ||
| 166 | stderr, | ||
| 167 | u8, | ||
| 168 | []clap.Param(u8){ | ||
| 169 | clap.Param(u8).flag('h', clap.Names.prefix("help")), | ||
| 170 | clap.Param(u8).flag('v', clap.Names.prefix("version")), | ||
| 171 | }, | ||
| 172 | error{}, | ||
| 173 | {}, | ||
| 174 | getHelp, | ||
| 175 | getValue, | ||
| 176 | ); | ||
| 177 | ``` | ||
| 178 | |||
| 179 | ``` | ||
| 180 | -h, --help Display this help and exit. | ||
| 181 | -v, --version Output version information and exit. | ||
| 182 | ``` | ||