diff options
| author | 2018-11-15 15:41:19 +0100 | |
|---|---|---|
| committer | 2018-11-15 15:41:19 +0100 | |
| commit | e34633492fc702c2f32b1ad46edbbc69e6ec4034 (patch) | |
| tree | ab1d3d4d1d8d59f7b4b3a995848f6fd4816a08e6 | |
| parent | Update README.md (diff) | |
| download | zig-clap-e34633492fc702c2f32b1ad46edbbc69e6ec4034.tar.gz zig-clap-e34633492fc702c2f32b1ad46edbbc69e6ec4034.tar.xz zig-clap-e34633492fc702c2f32b1ad46edbbc69e6ec4034.zip | |
Renamed helpEx to helpFull and added a new helpEx that wraps helpFull
| -rw-r--r-- | README.md | 42 | ||||
| -rw-r--r-- | src/index.zig | 44 |
2 files changed, 44 insertions, 42 deletions
| @@ -110,7 +110,7 @@ Ofc, this limits you to use only parameters that are comptime known. | |||
| 110 | 110 | ||
| 111 | ### `help` | 111 | ### `help` |
| 112 | 112 | ||
| 113 | The `help` and `helpEx` are functions for printing a simple list of all parameters the | 113 | The `help`, `helpEx` and `helpFull` are functions for printing a simple list of all parameters the |
| 114 | program can take. | 114 | program can take. |
| 115 | 115 | ||
| 116 | ```rust | 116 | ```rust |
| @@ -142,41 +142,7 @@ The `help` function is the simplest to call. It only takes an `OutStream` and a | |||
| 142 | `Param([]const u8)`. This function assumes that the id of each parameter is the help message. | 142 | `Param([]const u8)`. This function assumes that the id of each parameter is the help message. |
| 143 | 143 | ||
| 144 | The `helpEx` is the generic version of `help`. It can print a help message for any | 144 | The `helpEx` is the generic version of `help`. It can print a help message for any |
| 145 | `Param`, but requires some extra arguments to work. | 145 | `Param` give that the caller provides functions for getting the help and value strings. |
| 146 | 146 | ||
| 147 | ```rust | 147 | The `helpFull` is even more generic, allowing the functions that get the help and value strings |
| 148 | fn getHelp(_: void, param: clap.Param(u8)) error{}![]const u8 { | 148 | to return errors and take a context as a parameter. |
| 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 | ``` | ||
diff --git a/src/index.zig b/src/index.zig index 0914176..e85470c 100644 --- a/src/index.zig +++ b/src/index.zig | |||
| @@ -113,7 +113,7 @@ pub fn Param(comptime Id: type) type { | |||
| 113 | /// -s, --long=value_text help_text | 113 | /// -s, --long=value_text help_text |
| 114 | /// -s, help_text | 114 | /// -s, help_text |
| 115 | /// --long help_text | 115 | /// --long help_text |
| 116 | pub fn helpEx( | 116 | pub fn helpFull( |
| 117 | stream: var, | 117 | stream: var, |
| 118 | comptime Id: type, | 118 | comptime Id: type, |
| 119 | params: []const Param(Id), | 119 | params: []const Param(Id), |
| @@ -173,17 +173,53 @@ fn printParam( | |||
| 173 | try stream.print("={}", value_text(context, param)); | 173 | try stream.print("={}", value_text(context, param)); |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | /// A wrapper around helpFull for simple help_text and value_text functions that | ||
| 177 | /// cant return an error or take a context. | ||
| 178 | pub fn helpEx( | ||
| 179 | stream: var, | ||
| 180 | comptime Id: type, | ||
| 181 | params: []const Param(Id), | ||
| 182 | help_text: fn(Param(Id)) []const u8, | ||
| 183 | value_text: fn(Param(Id)) []const u8, | ||
| 184 | ) !void { | ||
| 185 | const Context = struct { | ||
| 186 | help_text: fn(Param(Id)) []const u8, | ||
| 187 | value_text: fn(Param(Id)) []const u8, | ||
| 188 | |||
| 189 | pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { | ||
| 190 | return c.help_text(p); | ||
| 191 | } | ||
| 192 | |||
| 193 | pub fn value(c: @This(), p: Param(Id)) error{}![]const u8 { | ||
| 194 | return c.value_text(p); | ||
| 195 | } | ||
| 196 | }; | ||
| 197 | |||
| 198 | return helpFull( | ||
| 199 | stream, | ||
| 200 | Id, | ||
| 201 | params, | ||
| 202 | error{}, | ||
| 203 | Context{ | ||
| 204 | .help_text = help_text, | ||
| 205 | .value_text = value_text, | ||
| 206 | }, | ||
| 207 | Context.help, | ||
| 208 | Context.value, | ||
| 209 | ); | ||
| 210 | } | ||
| 211 | |||
| 176 | /// A wrapper around helpEx that takes a Param([]const u8) and uses the string id | 212 | /// A wrapper around helpEx that takes a Param([]const u8) and uses the string id |
| 177 | /// as the help text for each paramter. | 213 | /// as the help text for each paramter. |
| 178 | pub fn help(stream: var, params: []const Param([]const u8)) !void { | 214 | pub fn help(stream: var, params: []const Param([]const u8)) !void { |
| 179 | try helpEx(stream, []const u8, params, error{}, {}, getHelpSimple, getValueSimple); | 215 | try helpEx(stream, []const u8, params, getHelpSimple, getValueSimple); |
| 180 | } | 216 | } |
| 181 | 217 | ||
| 182 | fn getHelpSimple(context: void, param: Param([]const u8)) error{}![]const u8 { | 218 | fn getHelpSimple(param: Param([]const u8)) []const u8 { |
| 183 | return param.id; | 219 | return param.id; |
| 184 | } | 220 | } |
| 185 | 221 | ||
| 186 | fn getValueSimple(context: void, param: Param([]const u8)) error{}![]const u8 { | 222 | fn getValueSimple(param: Param([]const u8)) []const u8 { |
| 187 | return "VALUE"; | 223 | return "VALUE"; |
| 188 | } | 224 | } |
| 189 | 225 | ||