From aa794eb621d328f8e96e3458602b926d610598ec Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Sun, 31 Mar 2019 16:48:33 -0400 Subject: [PATCH] fix zig run to accept executable args The `--` double-hyphen is now used to end further `zig` processing of command line options. All arguments after `--` will be passed on to the executable. eg. `--help` will be passed on. `zig run foo.zig -- --help` closes #2148 --- src/main.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 775ce928cdbf919c64eaa7144cde3bba3f0c4374..76261d4d686ea1a5961e175101d4a341c9306732 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " init-exe initialize a `zig build` application in the cwd\n" " init-lib initialize a `zig build` library in the cwd\n" " libc [paths_file] Display native libc paths file or validate one\n" - " run [source] create executable and run immediately\n" + " run [source] [-- [args]] create executable and run immediately\n" " translate-c [source] convert c code to zig code\n" " targets list available compilation targets\n" " test [source] create and run a test build\n" @@ -617,7 +617,14 @@ int main(int argc, char **argv) { char *arg = argv[i]; if (arg[0] == '-') { - if (strcmp(arg, "--release-fast") == 0) { + if (strcmp(arg, "--") == 0) { + if (cmd == CmdRun) { + runtime_args_start = i + 1; + break; // rest of the args are for the program + } else { + fprintf(stderr, "Unexpected end-of-parameter mark: %s\n", arg); + } + } else if (strcmp(arg, "--release-fast") == 0) { build_mode = BuildModeFastRelease; } else if (strcmp(arg, "--release-safe") == 0) { build_mode = BuildModeSafeRelease; @@ -880,10 +887,6 @@ int main(int argc, char **argv) { case CmdLibC: if (!in_file) { in_file = arg; - if (cmd == CmdRun) { - runtime_args_start = i + 1; - break; // rest of the args are for the program - } } else { fprintf(stderr, "Unexpected extra parameter: %s\n", arg); return print_error_usage(arg0); @@ -1147,8 +1150,10 @@ int main(int argc, char **argv) { if (cmd == CmdRun) { ZigList args = {0}; - for (int i = runtime_args_start; i < argc; ++i) { - args.append(argv[i]); + if (runtime_args_start != -1) { + for (int i = runtime_args_start; i < argc; ++i) { + args.append(argv[i]); + } } const char *exec_path = buf_ptr(&g->output_file_path); -- 2.54.0