diff options
| author | 2018-11-15 12:53:46 +0100 | |
|---|---|---|
| committer | 2018-11-15 12:53:46 +0100 | |
| commit | 1a219fc680faa79f1236912b14fd58754313df87 (patch) | |
| tree | 94ee1aee0dd89f0a40d1ab04156dd3dcb7965115 /src/index.zig | |
| 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 'src/index.zig')
| -rw-r--r-- | src/index.zig | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/index.zig b/src/index.zig index 225eb9c..0914176 100644 --- a/src/index.zig +++ b/src/index.zig | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | 2 | ||
| 3 | const debug = std.debug; | 3 | const debug = std.debug; |
| 4 | const io = std.io; | ||
| 5 | const mem = std.mem; | ||
| 4 | 6 | ||
| 5 | pub const @"comptime" = @import("comptime.zig"); | 7 | pub const @"comptime" = @import("comptime.zig"); |
| 6 | pub const args = @import("args.zig"); | 8 | pub const args = @import("args.zig"); |
| @@ -106,3 +108,135 @@ pub fn Param(comptime Id: type) type { | |||
| 106 | } | 108 | } |
| 107 | }; | 109 | }; |
| 108 | } | 110 | } |
| 111 | |||
| 112 | /// Will print a help message in the following format: | ||
| 113 | /// -s, --long=value_text help_text | ||
| 114 | /// -s, help_text | ||
| 115 | /// --long help_text | ||
| 116 | pub fn helpEx( | ||
| 117 | stream: var, | ||
| 118 | comptime Id: type, | ||
| 119 | params: []const Param(Id), | ||
| 120 | comptime Error: type, | ||
| 121 | context: var, | ||
| 122 | help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | ||
| 123 | value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | ||
| 124 | ) !void { | ||
| 125 | const max_spacing = blk: { | ||
| 126 | var null_stream = io.NullOutStream.init(); | ||
| 127 | var res: usize = 0; | ||
| 128 | for (params) |param| { | ||
| 129 | var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); | ||
| 130 | try printParam(&counting_stream.stream, Id, param, Error, context, value_text); | ||
| 131 | if (res < counting_stream.bytes_written) | ||
| 132 | res = counting_stream.bytes_written; | ||
| 133 | } | ||
| 134 | |||
| 135 | break :blk res; | ||
| 136 | }; | ||
| 137 | |||
| 138 | for (params) |param| { | ||
| 139 | if (param.names.short == null and param.names.long == null) | ||
| 140 | continue; | ||
| 141 | |||
| 142 | var counting_stream = io.CountingOutStream(@typeOf(stream.*).Error).init(stream); | ||
| 143 | try stream.print("\t"); | ||
| 144 | try printParam(&counting_stream.stream, Id, param, Error, context, value_text); | ||
| 145 | try stream.writeByteNTimes(' ', max_spacing - counting_stream.bytes_written); | ||
| 146 | try stream.print("\t{}\n", try help_text(context, param)); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | fn printParam( | ||
| 151 | stream: var, | ||
| 152 | comptime Id: type, | ||
| 153 | param: Param(Id), | ||
| 154 | comptime Error: type, | ||
| 155 | context: var, | ||
| 156 | value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, | ||
| 157 | ) @typeOf(stream.*).Error!void { | ||
| 158 | if (param.names.short) |s| { | ||
| 159 | try stream.print("-{c}", s); | ||
| 160 | } else { | ||
| 161 | try stream.print(" "); | ||
| 162 | } | ||
| 163 | if (param.names.long) |l| { | ||
| 164 | if (param.names.short) |_| { | ||
| 165 | try stream.print(", "); | ||
| 166 | } else { | ||
| 167 | try stream.print(" "); | ||
| 168 | } | ||
| 169 | |||
| 170 | try stream.print("--{}", l); | ||
| 171 | } | ||
| 172 | if (param.takes_value) | ||
| 173 | try stream.print("={}", value_text(context, param)); | ||
| 174 | } | ||
| 175 | |||
| 176 | /// A wrapper around helpEx that takes a Param([]const u8) and uses the string id | ||
| 177 | /// as the help text for each paramter. | ||
| 178 | pub fn help(stream: var, params: []const Param([]const u8)) !void { | ||
| 179 | try helpEx(stream, []const u8, params, error{}, {}, getHelpSimple, getValueSimple); | ||
| 180 | } | ||
| 181 | |||
| 182 | fn getHelpSimple(context: void, param: Param([]const u8)) error{}![]const u8 { | ||
| 183 | return param.id; | ||
| 184 | } | ||
| 185 | |||
| 186 | fn getValueSimple(context: void, param: Param([]const u8)) error{}![]const u8 { | ||
| 187 | return "VALUE"; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | test "clap.help" { | ||
| 192 | var buf: [1024]u8 = undefined; | ||
| 193 | var slice_stream = io.SliceOutStream.init(buf[0..]); | ||
| 194 | try help( | ||
| 195 | &slice_stream.stream, | ||
| 196 | []Param([]const u8){ | ||
| 197 | Param([]const u8).flag( | ||
| 198 | "Short flag.", | ||
| 199 | Names.short('a'), | ||
| 200 | ), | ||
| 201 | Param([]const u8).option( | ||
| 202 | "Short option.", | ||
| 203 | Names.short('b'), | ||
| 204 | ), | ||
| 205 | Param([]const u8).flag( | ||
| 206 | "Long flag.", | ||
| 207 | Names.long("aa"), | ||
| 208 | ), | ||
| 209 | Param([]const u8).option( | ||
| 210 | "Long option.", | ||
| 211 | Names.long("bb"), | ||
| 212 | ), | ||
| 213 | Param([]const u8).flag( | ||
| 214 | "Both flag.", | ||
| 215 | Names.prefix("cc"), | ||
| 216 | ), | ||
| 217 | Param([]const u8).option( | ||
| 218 | "Both option.", | ||
| 219 | Names.prefix("dd"), | ||
| 220 | ), | ||
| 221 | Param([]const u8).positional( | ||
| 222 | "Positional. This should not appear in the help message." | ||
| 223 | ), | ||
| 224 | }, | ||
| 225 | ); | ||
| 226 | |||
| 227 | const expected = | ||
| 228 | "\t-a \tShort flag.\n" ++ | ||
| 229 | "\t-b=VALUE \tShort option.\n" ++ | ||
| 230 | "\t --aa \tLong flag.\n" ++ | ||
| 231 | "\t --bb=VALUE\tLong option.\n" ++ | ||
| 232 | "\t-c, --cc \tBoth flag.\n" ++ | ||
| 233 | "\t-d, --dd=VALUE\tBoth option.\n"; | ||
| 234 | |||
| 235 | if (!mem.eql(u8, slice_stream.getWritten(), expected)) { | ||
| 236 | debug.warn("============ Expected ============\n"); | ||
| 237 | debug.warn("{}", expected); | ||
| 238 | debug.warn("============= Actual =============\n"); | ||
| 239 | debug.warn("{}", slice_stream.getWritten()); | ||
| 240 | return error.NoMatch; | ||
| 241 | } | ||
| 242 | } | ||