Skip to content

Commit

Permalink
fix zig run to accept executable args
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mikdusan authored and andrewrk committed Mar 31, 2019
1 parent a4afacd commit aa794eb
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1147,8 +1150,10 @@ int main(int argc, char **argv) {

if (cmd == CmdRun) {
ZigList<const char*> 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);
Expand Down

0 comments on commit aa794eb

Please sign in to comment.