summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig5
-rw-r--r--examples/core.zig446
-rw-r--r--examples/extended.zig2
-rw-r--r--src/extended.zig54
-rw-r--r--tests/extended.zig36
5 files changed, 47 insertions, 496 deletions
diff --git a/build.zig b/build.zig
index a32c0bd..29792ee 100644
--- a/build.zig
+++ b/build.zig
@@ -5,10 +5,7 @@ pub fn build(b: *Builder) void {
5 5
6 { 6 {
7 const example_step = b.step("examples", "Build all examples"); 7 const example_step = b.step("examples", "Build all examples");
8 const examples = [][]const u8 { 8 const examples = [][]const u8 {};
9 "core",
10 "extended",
11 };
12 9
13 b.default_step.dependOn(example_step); 10 b.default_step.dependOn(example_step);
14 inline for (examples) |example| { 11 inline for (examples) |example| {
diff --git a/examples/core.zig b/examples/core.zig
deleted file mode 100644
index 4335ff2..0000000
--- a/examples/core.zig
+++ /dev/null
@@ -1,446 +0,0 @@
1const std = @import("std");
2const clap = @import("clap").core;
3
4const debug = std.debug;
5const mem = std.mem;
6
7const Names = clap.Names;
8const Param = clap.Param;
9
10const ArgError = clap.OsArgIterator.Error;
11
12// TODO: More specific error in this func type.
13const Command = fn(*mem.Allocator, *clap.ArgIterator(ArgError)) error!void;
14
15const params = []Param(Command){
16 Param(Command).init(help, false, Names.prefix("help")),
17 Param(Command).init(cmdBuild, false, Names.bare("build")),
18 Param(Command).init(cmdBuildExe, false, Names.bare("build-exe")),
19 Param(Command).init(cmdBuildLib, false, Names.bare("build-lib")),
20 Param(Command).init(cmdBuildObj, false, Names.bare("build-obj")),
21 Param(Command).init(cmdFmt, false, Names.bare("fmt")),
22 Param(Command).init(cmdRun, false, Names.bare("run")),
23 Param(Command).init(cmdTargets, false, Names.bare("targets")),
24 Param(Command).init(cmdTest, false, Names.bare("test")),
25 Param(Command).init(cmdVersion, false, Names.bare("version")),
26 Param(Command).init(cmdZen, false, Names.bare("zen")),
27};
28
29const usage =
30 \\usage: zig [command] [options]
31 \\
32 \\Commands:
33 \\
34 \\ build Build project from build.zig
35 \\ build-exe [source] Create executable from source or object files
36 \\ build-lib [source] Create library from source or object files
37 \\ build-obj [source] Create object from source or assembly
38 \\ fmt [source] Parse file and render in canonical zig format
39 \\ run [source] Create executable and run immediately
40 \\ targets List available compilation targets
41 \\ test [source] Create and run a test build
42 \\ translate-c [source] Convert c code to zig code
43 \\ version Print version number and exit
44 \\ zen Print zen of zig and exit
45 \\
46 \\
47;
48
49pub fn main() !void {
50 var direct_allocator = std.heap.DirectAllocator.init();
51 defer direct_allocator.deinit();
52 var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator);
53 defer arena.deinit();
54
55 const allocator = &arena.allocator;
56
57 var args = clap.OsArgIterator.init(allocator);
58 defer args.deinit();
59
60 const exe = try args.iter.next();
61 var parser = clap.Clap(Command, ArgError).init(params, &args.iter);
62
63 const maybe_arg = parser.next() catch |err| b: {
64 debug.warn("{}.\n", @errorName(err));
65 // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches
66 return err;
67 };
68 const arg = maybe_arg ?? {
69 debug.warn("No command found.\n");
70 // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches
71 return error.NoCommandFound;
72 };
73
74 try arg.param.id(allocator, parser.iter);
75}
76
77pub fn help(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
78 // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches
79}
80
81// cmd:build ///////////////////////////////////////////////////////////////////////////////////////
82
83const Build = enum {
84 Help,
85 Init,
86 BuildFile,
87 CacheDir,
88 Verbose,
89 Prefix,
90 VerboseTokenize,
91 VerboseAst,
92 VerboseLink,
93 VerboseIr,
94 VerboseLlvmIr,
95 VerboseCImport,
96};
97
98const build_params = []Param(Build){
99 Param(Build).init(Build.Help, false, Names.prefix("help")),
100 Param(Build).init(Build.Init, false, Names.long("init")),
101 Param(Build).init(Build.BuildFile, true, Names.long("build-file")),
102 Param(Build).init(Build.CacheDir, true, Names.long("cache-dir")),
103 Param(Build).init(Build.Verbose, false, Names.prefix("verbose")),
104 Param(Build).init(Build.Prefix, true, Names.long("prefix")),
105
106 Param(Build).init(Build.VerboseTokenize, false, Names.prefix("verbose-tokenize")),
107 Param(Build).init(Build.VerboseAst, false, Names.prefix("verbose-ast")),
108 Param(Build).init(Build.VerboseLink, false, Names.prefix("verbose-link")),
109 Param(Build).init(Build.VerboseIr, false, Names.prefix("verbose-ir")),
110 Param(Build).init(Build.VerboseLlvmIr, false, Names.prefix("verbose-llvm-ir")),
111 Param(Build).init(Build.VerboseCImport, false, Names.prefix("verbose-cimport")),
112};
113
114const build_usage =
115 \\usage: zig build <options>
116 \\
117 \\General Options:
118 \\ -h, --help Print this help and exit
119 \\ --init Generate a build.zig template
120 \\ --build-file [file] Override path to build.zig
121 \\ --cache-dir [path] Override path to cache directory
122 \\ -v, --verbose Print commands before executing them
123 \\ --prefix [path] Override default install prefix
124 \\
125 \\Project-Specific Options:
126 \\
127 \\ Project-specific options become available when the build file is found.
128 \\
129 \\Advanced Options:
130 \\ --verbose-tokenize Enable compiler debug output for tokenization
131 \\ --verbose-ast Enable compiler debug output for parsing into an AST
132 \\ --verbose-link Enable compiler debug output for linking
133 \\ --verbose-ir Enable compiler debug output for Zig IR
134 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
135 \\ --verbose-cimport Enable compiler debug output for C imports
136 \\
137 \\
138;
139
140const missing_build_file =
141 \\No 'build.zig' file found.
142 \\
143 \\Initialize a 'build.zig' template file with `zig build --init`,
144 \\or build an executable directly with `zig build-exe $FILENAME.zig`.
145 \\
146 \\See: `zig build --help` or `zig help` for more options.
147 \\
148;
149
150fn cmdBuild(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) !void {
151 var init = false;
152 var build_file: []const u8 = "build.zig";
153 var cache_dir: []const u8 = "zig-cache";
154 var verbose = false;
155 var prefix: []const u8 = "";
156 var verbose_tokenize = false;
157 var verbose_ast = false;
158 var verbose_link = false;
159 var verbose_ir = false;
160 var verbose_llvm_ir = false;
161 var verbose_cimport = false;
162
163 var parser = clap.Clap(Build, ArgError).init(build_params, args);
164
165 while (parser.next() catch |err| {
166 debug.warn("{}.\n", @errorName(err));
167 // debug.warn(build_usage); TODO: error: evaluation exceeded 1000 backwards branches
168 return err;
169 }) |arg| switch (arg.param.id) {
170 Build.Help => return, // debug.warn(build_usage) TODO: error: evaluation exceeded 1000 backwards branches,
171 Build.Init => init = true,
172 Build.BuildFile => build_file = ??arg.value,
173 Build.CacheDir => cache_dir = ??arg.value,
174 Build.Verbose => verbose = true,
175 Build.Prefix => prefix = ??arg.value,
176 Build.VerboseTokenize => verbose_tokenize = true,
177 Build.VerboseAst => verbose_ast = true,
178 Build.VerboseLink => verbose_link = true,
179 Build.VerboseIr => verbose_ir = true,
180 Build.VerboseLlvmIr => verbose_llvm_ir = true,
181 Build.VerboseCImport => verbose_cimport = true,
182 };
183
184 debug.warn("command: build\n");
185 debug.warn("init = {}\n", init);
186 debug.warn("build_file = {}\n", build_file);
187 debug.warn("cache_dir = {}\n", cache_dir);
188 debug.warn("verbose = {}\n", verbose);
189 debug.warn("prefix = {}\n", prefix);
190 debug.warn("verbose_tokenize = {}\n", verbose_tokenize);
191 debug.warn("verbose_ast = {}\n", verbose_ast);
192 debug.warn("verbose_link = {}\n", verbose_link);
193 debug.warn("verbose_ir = {}\n", verbose_ir);
194 debug.warn("verbose_llvm_ir = {}\n", verbose_llvm_ir);
195 debug.warn("verbose_cimport = {}\n", verbose_cimport);
196}
197
198// cmd:build-exe ///////////////////////////////////////////////////////////////////////////////////
199
200const BuildGeneric = enum {
201 File,
202 Help,
203 Color,
204
205 Assembly,
206 CacheDir,
207 Emit,
208 EnableTimingInfo,
209 LibCDir,
210 Name,
211 Output,
212 OutputH,
213 PkgBegin,
214 PkgEnd,
215 ReleaseFast,
216 ReleaseSafe,
217 Static,
218 Strip,
219 TargetArch,
220 TargetEnviron,
221 TargetOs,
222 VerboseTokenize,
223 VerboseAst,
224 VerboseLink,
225 VerboseIr,
226 VerboseLlvmIr,
227 VerboseCImport,
228 DirAfter,
229 ISystem,
230 MLlvm,
231
232 ArPath,
233 DynamicLinker,
234 EachLibRPath,
235 LibcLibDir,
236 LibcStaticLibDir,
237 MsvcLibDir,
238 Kernel32LibDir,
239 Library,
240 ForbidLibrary,
241 LibraryPath,
242 LinkerScript,
243 Object,
244 RDynamic,
245 RPath,
246 MConsole,
247 MWindows,
248 Framework,
249 MiOsVersionMin,
250 MMacOsXVersonMin,
251 VerMajor,
252 VerMinor,
253 VerPatch,
254};
255
256const build_generic_params = []Param(BuildGeneric){
257 Param(BuildGeneric).init(BuildGeneric.Help, false, Names.prefix("help")),
258};
259
260const build_generic_usage =
261 \\usage: zig build-exe <options> [file]
262 \\ zig build-lib <options> [file]
263 \\ zig build-obj <options> [file]
264 \\
265 \\General Options:
266 \\ -h, --help Print this help and exit
267 \\ -c, --color [auto|off|on] Enable or disable colored error messages
268 \\
269 \\Compile Options:
270 \\ --assembly [source] Add assembly file to build
271 \\ --cache-dir [path] Override the cache directory
272 \\ --emit [filetype] Emit a specific file format as compilation output
273 \\ --enable-timing-info Print timing diagnostics
274 \\ --libc-include-dir [path] Directory where libc stdlib.h resides
275 \\ --name [name] Override output name
276 \\ --output [file] Override destination path
277 \\ --output-h [file] Override generated header file path
278 \\ --pkg-begin [name] [path] Make package available to import and push current pkg
279 \\ --pkg-end Pop current pkg
280 \\ --release-fast Build with optimizations on and safety off
281 \\ --release-safe Build with optimizations on and safety on
282 \\ --static Output will be statically linked
283 \\ --strip Exclude debug symbols
284 \\ --target-arch [name] Specify target architecture
285 \\ --target-environ [name] Specify target environment
286 \\ --target-os [name] Specify target operating system
287 \\ --verbose-tokenize Turn on compiler debug output for tokenization
288 \\ --verbose-ast-tree Turn on compiler debug output for parsing into an AST (tree view)
289 \\ --verbose-ast-fmt Turn on compiler debug output for parsing into an AST (render source)
290 \\ --verbose-link Turn on compiler debug output for linking
291 \\ --verbose-ir Turn on compiler debug output for Zig IR
292 \\ --verbose-llvm-ir Turn on compiler debug output for LLVM IR
293 \\ --verbose-cimport Turn on compiler debug output for C imports
294 \\ --dirafter [dir] Same as --isystem but do it last
295 \\ --isystem [dir] Add additional search path for other .h files
296 \\ --mllvm [arg] Additional arguments to forward to LLVM's option processing
297 \\
298 \\Link Options:
299 \\ --ar-path [path] Set the path to ar
300 \\ --dynamic-linker [path] Set the path to ld.so
301 \\ --each-lib-rpath Add rpath for each used dynamic library
302 \\ --libc-lib-dir [path] Directory where libc crt1.o resides
303 \\ --libc-static-lib-dir [path] Directory where libc crtbegin.o resides
304 \\ --msvc-lib-dir [path] (windows) directory where vcruntime.lib resides
305 \\ --kernel32-lib-dir [path] (windows) directory where kernel32.lib resides
306 \\ --library [lib] Link against lib
307 \\ --forbid-library [lib] Make it an error to link against lib
308 \\ --library-path [dir] Add a directory to the library search path
309 \\ --linker-script [path] Use a custom linker script
310 \\ --object [obj] Add object file to build
311 \\ --rdynamic Add all symbols to the dynamic symbol table
312 \\ --rpath [path] Add directory to the runtime library search path
313 \\ --mconsole (windows) --subsystem console to the linker
314 \\ --mwindows (windows) --subsystem windows to the linker
315 \\ --framework [name] (darwin) link against framework
316 \\ --mios-version-min [ver] (darwin) set iOS deployment target
317 \\ --mmacosx-version-min [ver] (darwin) set Mac OS X deployment target
318 \\ --ver-major [ver] Dynamic library semver major version
319 \\ --ver-minor [ver] Dynamic library semver minor version
320 \\ --ver-patch [ver] Dynamic library semver patch version
321 \\
322 \\
323;
324
325
326fn cmdBuildExe(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
327}
328
329// cmd:build-lib ///////////////////////////////////////////////////////////////////////////////////
330
331fn cmdBuildLib(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
332}
333
334// cmd:build-obj ///////////////////////////////////////////////////////////////////////////////////
335
336fn cmdBuildObj(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
337}
338
339// cmd:fmt /////////////////////////////////////////////////////////////////////////////////////////
340
341const usage_fmt =
342 \\usage: zig fmt [file]...
343 \\
344 \\ Formats the input files and modifies them in-place.
345 \\
346 \\Options:
347 \\ --help Print this help and exit
348 \\ --color [auto|off|on] Enable or disable colored error messages
349 \\
350 \\
351;
352
353fn cmdFmt(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
354}
355
356// cmd:targets /////////////////////////////////////////////////////////////////////////////////////
357
358fn cmdTargets(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
359}
360
361// cmd:version /////////////////////////////////////////////////////////////////////////////////////
362
363fn cmdVersion(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
364}
365
366// cmd:test ////////////////////////////////////////////////////////////////////////////////////////
367
368const usage_test =
369 \\usage: zig test [file]...
370 \\
371 \\Options:
372 \\ --help Print this help and exit
373 \\
374 \\
375;
376
377fn cmdTest(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
378}
379
380// cmd:run /////////////////////////////////////////////////////////////////////////////////////////
381
382// Run should be simple and not expose the full set of arguments provided by build-exe. If specific
383// build requirements are need, the user should `build-exe` then `run` manually.
384const usage_run =
385 \\usage: zig run [file] -- <runtime args>
386 \\
387 \\Options:
388 \\ --help Print this help and exit
389 \\
390 \\
391;
392
393fn cmdRun(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
394}
395
396// cmd:translate-c /////////////////////////////////////////////////////////////////////////////////
397
398const usage_translate_c =
399 \\usage: zig translate-c [file]
400 \\
401 \\Options:
402 \\ --help Print this help and exit
403 \\ --enable-timing-info Print timing diagnostics
404 \\ --output [path] Output file to write generated zig file (default: stdout)
405 \\
406 \\
407;
408
409fn cmdTranslateC(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) {
410}
411
412// cmd:zen /////////////////////////////////////////////////////////////////////////////////////////
413
414const info_zen =
415 \\
416 \\ * Communicate intent precisely.
417 \\ * Edge cases matter.
418 \\ * Favor reading code over writing code.
419 \\ * Only one obvious way to do things.
420 \\ * Runtime crashes are better than bugs.
421 \\ * Compile errors are better than runtime crashes.
422 \\ * Incremental improvements.
423 \\ * Avoid local maximums.
424 \\ * Reduce the amount one must remember.
425 \\ * Minimize energy spent on coding style.
426 \\ * Together we serve end users.
427 \\
428 \\
429;
430
431fn cmdZen(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
432}
433
434// cmd:internal ////////////////////////////////////////////////////////////////////////////////////
435
436const usage_internal =
437 \\usage: zig internal [subcommand]
438 \\
439 \\Sub-Commands:
440 \\ build-info Print static compiler build-info
441 \\
442 \\
443;
444
445fn cmdInternal(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) {
446}
diff --git a/examples/extended.zig b/examples/extended.zig
deleted file mode 100644
index c21d254..0000000
--- a/examples/extended.zig
+++ /dev/null
@@ -1,2 +0,0 @@
1
2pub fn main() void {}
diff --git a/src/extended.zig b/src/extended.zig
index 5fd019d..ec8310e 100644
--- a/src/extended.zig
+++ b/src/extended.zig
@@ -16,40 +16,48 @@ pub const Param = struct {
16 settings: Settings, 16 settings: Settings,
17 kind: Kind, 17 kind: Kind,
18 18
19 pub fn flag(field: []const u8, names: *const core.Names, settings: *const Settings) Param { 19 required: bool,
20 return Param{ 20 position: ?usize,
21 .field = field, 21
22 .names = names.*, 22 pub fn flag(field: []const u8, names: *const core.Names) Param {
23 .settings = settings.*, 23 return init(
24 .kind = Kind.Flag, 24 field,
25 }; 25 names,
26 Kind.Flag,
27 );
26 } 28 }
27 29
28 pub fn option( 30 pub fn option(
29 field: []const u8, 31 field: []const u8,
30 names: *const core.Names, 32 names: *const core.Names,
31 settings: *const Settings,
32 comptime parser: *const Parser, 33 comptime parser: *const Parser,
33 ) Param { 34 ) Param {
34 return Param{ 35 return init(
35 .field = field, 36 field,
36 .names = names.*, 37 names,
37 .settings = settings.*, 38 Kind{ .Option = parser.* },
38 .kind = Kind{ .Option = parser.* }, 39 );
39 };
40 } 40 }
41 41
42 pub fn subcommand( 42 pub fn subcommand(
43 field: []const u8, 43 field: []const u8,
44 names: *const core.Names, 44 names: *const core.Names,
45 settings: *const Settings,
46 comptime command: *const Command, 45 comptime command: *const Command,
47 ) Param { 46 ) Param {
47 return init(
48 field,
49 names,
50 Kind{ .Subcommand = command.* },
51 );
52 }
53
54 pub fn init(field: []const u8, names: *const core.Names, kind: *const Kind) Param {
48 return Param{ 55 return Param{
49 .field = field, 56 .field = field,
50 .names = names.*, 57 .names = names.*,
51 .settings = settings.*, 58 .kind = kind.*,
52 .kind = Kind{ .Subcommand = Command.* }, 59 .required = false,
60 .position = null,
53 }; 61 };
54 } 62 }
55 63
@@ -58,18 +66,6 @@ pub const Param = struct {
58 Option: Parser, 66 Option: Parser,
59 Subcommand: Command, 67 Subcommand: Command,
60 }; 68 };
61
62 pub const Settings = struct {
63 required: bool,
64 position: ?usize,
65
66 pub fn default() Settings {
67 return Settings{
68 .required = false,
69 .position = null,
70 };
71 }
72 };
73}; 69};
74 70
75const Opaque = @OpaqueType(); 71const Opaque = @OpaqueType();
diff --git a/tests/extended.zig b/tests/extended.zig
index 97b817e..78c319e 100644
--- a/tests/extended.zig
+++ b/tests/extended.zig
@@ -79,11 +79,13 @@ test "clap.extended: short" {
79 .b = 0, 79 .b = 0,
80 }, 80 },
81 .params = []Param{ 81 .params = []Param{
82 Param.flag("a", Names.short('a'), Param.Settings{ 82 p: {
83 .required = true, 83 var res = Param.flag("a", Names.short('a'));
84 .position = 0, 84 res.required = true;
85 }), 85 res.position = 0;
86 Param.option("b", Names.short('b'), Param.Settings.default(), &Parser.int(u8, 10)), 86 break :p res;
87 },
88 Param.option("b", Names.short('b'), &Parser.int(u8, 10)),
87 } 89 }
88 }; 90 };
89 91
@@ -173,11 +175,13 @@ test "clap.extended: long" {
173 .b = 0, 175 .b = 0,
174 }, 176 },
175 .params = []Param{ 177 .params = []Param{
176 Param.flag("a", Names.long("a"), Param.Settings{ 178 p: {
177 .required = true, 179 var res = Param.long("a", Names.short('a'));
178 .position = 0, 180 res.required = true;
179 }), 181 res.position = 0;
180 Param.option("b", Names.long("b"), Param.Settings.default(), &Parser.int(u8, 10)), 182 break :p res;
183 },
184 Param.option("b", Names.long('b'), &Parser.int(u8, 10)),
181 } 185 }
182 }; 186 };
183 187
@@ -243,11 +247,13 @@ test "clap.extended: bare" {
243 .b = 0, 247 .b = 0,
244 }, 248 },
245 .params = []Param{ 249 .params = []Param{
246 Param.flag("a", Names.bare("a"), Param.Settings{ 250 p: {
247 .required = true, 251 var res = Param.bare("a", Names.short('a'));
248 .position = 0, 252 res.required = true;
249 }), 253 res.position = 0;
250 Param.option("b", Names.bare("b"), Param.Settings.default(), &Parser.int(u8, 10)), 254 break :p res;
255 },
256 Param.option("b", Names.bare('b'), &Parser.int(u8, 10)),
251 } 257 }
252 }; 258 };
253 259