authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-03-31 16:48:33-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-31 17:01:11-04:00
logaa794eb621d328f8e96e3458602b926d610598ec
tree83a5469cd05396cb5a2a15cd1cdecf8a59a192dc
parenta4afacd1822cea75196b27a8a5fe30bd252eccae

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

1 files changed, 13 insertions(+), 8 deletions(-)

src/main.cpp+13-8
......@@ -38,7 +38,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
3838 " init-exe initialize a `zig build` application in the cwd\n"
3939 " init-lib initialize a `zig build` library in the cwd\n"
4040 " libc [paths_file] Display native libc paths file or validate one\n"
41 " run [source] create executable and run immediately\n"
41 " run [source] [-- [args]] create executable and run immediately\n"
4242 " translate-c [source] convert c code to zig code\n"
4343 " targets list available compilation targets\n"
4444 " test [source] create and run a test build\n"
......@@ -617,7 +617,14 @@ int main(int argc, char **argv) {
617617 char *arg = argv[i];
618618
619619 if (arg[0] == '-') {
620 if (strcmp(arg, "--release-fast") == 0) {
620 if (strcmp(arg, "--") == 0) {
621 if (cmd == CmdRun) {
622 runtime_args_start = i + 1;
623 break; // rest of the args are for the program
624 } else {
625 fprintf(stderr, "Unexpected end-of-parameter mark: %s\n", arg);
626 }
627 } else if (strcmp(arg, "--release-fast") == 0) {
621628 build_mode = BuildModeFastRelease;
622629 } else if (strcmp(arg, "--release-safe") == 0) {
623630 build_mode = BuildModeSafeRelease;
......@@ -880,10 +887,6 @@ int main(int argc, char **argv) {
880887 case CmdLibC:
881888 if (!in_file) {
882889 in_file = arg;
883 if (cmd == CmdRun) {
884 runtime_args_start = i + 1;
885 break; // rest of the args are for the program
886 }
887890 } else {
888891 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
889892 return print_error_usage(arg0);
......@@ -1147,8 +1150,10 @@ int main(int argc, char **argv) {
11471150
11481151 if (cmd == CmdRun) {
11491152 ZigList<const char*> args = {0};
1150 for (int i = runtime_args_start; i < argc; ++i) {
1151 args.append(argv[i]);
1153 if (runtime_args_start != -1) {
1154 for (int i = runtime_args_start; i < argc; ++i) {
1155 args.append(argv[i]);
1156 }
11521157 }
11531158
11541159 const char *exec_path = buf_ptr(&g->output_file_path);