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) {...@@ -38,7 +38,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
38 " init-exe initialize a `zig build` application in the cwd\n"38 " init-exe initialize a `zig build` application in the cwd\n"
39 " init-lib initialize a `zig build` library in the cwd\n"39 " init-lib initialize a `zig build` library in the cwd\n"
40 " libc [paths_file] Display native libc paths file or validate one\n"40 " 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"
42 " translate-c [source] convert c code to zig code\n"42 " translate-c [source] convert c code to zig code\n"
43 " targets list available compilation targets\n"43 " targets list available compilation targets\n"
44 " test [source] create and run a test build\n"44 " test [source] create and run a test build\n"
...@@ -617,7 +617,14 @@ int main(int argc, char **argv) {...@@ -617,7 +617,14 @@ int main(int argc, char **argv) {
617 char *arg = argv[i];617 char *arg = argv[i];
618618
619 if (arg[0] == '-') {619 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) {
621 build_mode = BuildModeFastRelease;628 build_mode = BuildModeFastRelease;
622 } else if (strcmp(arg, "--release-safe") == 0) {629 } else if (strcmp(arg, "--release-safe") == 0) {
623 build_mode = BuildModeSafeRelease;630 build_mode = BuildModeSafeRelease;
...@@ -880,10 +887,6 @@ int main(int argc, char **argv) {...@@ -880,10 +887,6 @@ int main(int argc, char **argv) {
880 case CmdLibC:887 case CmdLibC:
881 if (!in_file) {888 if (!in_file) {
882 in_file = arg;889 in_file = arg;
883 if (cmd == CmdRun) {
884 runtime_args_start = i + 1;
885 break; // rest of the args are for the program
886 }
887 } else {890 } else {
888 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);891 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
889 return print_error_usage(arg0);892 return print_error_usage(arg0);
...@@ -1147,8 +1150,10 @@ int main(int argc, char **argv) {...@@ -1147,8 +1150,10 @@ int main(int argc, char **argv) {
11471150
1148 if (cmd == CmdRun) {1151 if (cmd == CmdRun) {
1149 ZigList<const char*> args = {0};1152 ZigList<const char*> args = {0};
1150 for (int i = runtime_args_start; i < argc; ++i) {1153 if (runtime_args_start != -1) {
1151 args.append(argv[i]);1154 for (int i = runtime_args_start; i < argc; ++i) {
1155 args.append(argv[i]);
1156 }
1152 }1157 }
11531158
1154 const char *exec_path = buf_ptr(&g->output_file_path);1159 const char *exec_path = buf_ptr(&g->output_file_path);