authorgravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2018-09-25 20:13:02-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-25 22:05:44-04:00
log7b204649e3af348a7831837d5a36219733b505ca
tree3bed18bc49e418054ab0f7d88c1df00532a04f66
parent2e562a5f366aabefd34a4191b2cd0b5e72d3d089

stage1: Added `zig help` to show usage on stdout

This will make it easier to do things like `zig help | grep something`. Invalid arguments will now display a short notice for `zig help` instead of showing the full usage information. This will make it easier to see the actual error.

1 files changed, 46 insertions(+), 28 deletions(-)

src/main.cpp+46-28
...@@ -16,14 +16,22 @@...@@ -16,14 +16,22 @@
1616
17#include <stdio.h>17#include <stdio.h>
1818
19static int usage(const char *arg0) {19static int print_error_usage(const char *arg0) {
20 fprintf(stderr, "Usage: %s [command] [options]\n"20 fprintf(stderr, "See `%s help` for detailed usage information\n", arg0);
21 return EXIT_FAILURE;
22}
23
24static int print_full_usage(const char *arg0) {
25 fprintf(stdout,
26 "Usage: %s [command] [options]\n"
27 "\n"
21 "Commands:\n"28 "Commands:\n"
22 " build build project from build.zig\n"29 " build build project from build.zig\n"
23 " build-exe [source] create executable from source or object files\n"30 " build-exe [source] create executable from source or object files\n"
24 " build-lib [source] create library from source or object files\n"31 " build-lib [source] create library from source or object files\n"
25 " build-obj [source] create object from source or assembly\n"32 " build-obj [source] create object from source or assembly\n"
26 " builtin show the source code of that @import(\"builtin\")\n"33 " builtin show the source code of that @import(\"builtin\")\n"
34 " help show this usage information\n"
27 " id print the base64-encoded compiler id\n"35 " id print the base64-encoded compiler id\n"
28 " init-exe initialize a `zig build` application in the cwd\n"36 " init-exe initialize a `zig build` application in the cwd\n"
29 " init-lib initialize a `zig build` library in the cwd\n"37 " init-lib initialize a `zig build` library in the cwd\n"
...@@ -33,6 +41,7 @@ static int usage(const char *arg0) {...@@ -33,6 +41,7 @@ static int usage(const char *arg0) {
33 " test [source] create and run a test build\n"41 " test [source] create and run a test build\n"
34 " version print version number and exit\n"42 " version print version number and exit\n"
35 " zen print zen of zig and exit\n"43 " zen print zen of zig and exit\n"
44 "\n"
36 "Compile Options:\n"45 "Compile Options:\n"
37 " --assembly [source] add assembly file to build\n"46 " --assembly [source] add assembly file to build\n"
38 " --cache-dir [path] override the cache directory\n"47 " --cache-dir [path] override the cache directory\n"
...@@ -63,6 +72,7 @@ static int usage(const char *arg0) {...@@ -63,6 +72,7 @@ static int usage(const char *arg0) {
63 " -dirafter [dir] same as -isystem but do it last\n"72 " -dirafter [dir] same as -isystem but do it last\n"
64 " -isystem [dir] add additional search path for other .h files\n"73 " -isystem [dir] add additional search path for other .h files\n"
65 " -mllvm [arg] forward an arg to LLVM's option processing\n"74 " -mllvm [arg] forward an arg to LLVM's option processing\n"
75 "\n"
66 "Link Options:\n"76 "Link Options:\n"
67 " --dynamic-linker [path] set the path to ld.so\n"77 " --dynamic-linker [path] set the path to ld.so\n"
68 " --each-lib-rpath add rpath for each used dynamic library\n"78 " --each-lib-rpath add rpath for each used dynamic library\n"
...@@ -87,13 +97,14 @@ static int usage(const char *arg0) {...@@ -87,13 +97,14 @@ static int usage(const char *arg0) {
87 " --ver-major [ver] dynamic library semver major version\n"97 " --ver-major [ver] dynamic library semver major version\n"
88 " --ver-minor [ver] dynamic library semver minor version\n"98 " --ver-minor [ver] dynamic library semver minor version\n"
89 " --ver-patch [ver] dynamic library semver patch version\n"99 " --ver-patch [ver] dynamic library semver patch version\n"
100 "\n"
90 "Test Options:\n"101 "Test Options:\n"
91 " --test-filter [text] skip tests that do not match filter\n"102 " --test-filter [text] skip tests that do not match filter\n"
92 " --test-name-prefix [text] add prefix to all tests\n"103 " --test-name-prefix [text] add prefix to all tests\n"
93 " --test-cmd [arg] specify test execution command one arg at a time\n"104 " --test-cmd [arg] specify test execution command one arg at a time\n"
94 " --test-cmd-bin appends test binary path to test cmd args\n"105 " --test-cmd-bin appends test binary path to test cmd args\n"
95 , arg0);106 , arg0);
96 return EXIT_FAILURE;107 return EXIT_SUCCESS;
97}108}
98109
99static const char *ZIG_ZEN = "\n"110static const char *ZIG_ZEN = "\n"
...@@ -144,15 +155,16 @@ static int print_target_list(FILE *f) {...@@ -144,15 +155,16 @@ static int print_target_list(FILE *f) {
144}155}
145156
146enum Cmd {157enum Cmd {
147 CmdInvalid,158 CmdNone,
148 CmdBuild,159 CmdBuild,
149 CmdBuiltin,160 CmdBuiltin,
161 CmdHelp,
150 CmdRun,162 CmdRun,
163 CmdTargets,
151 CmdTest,164 CmdTest,
165 CmdTranslateC,
152 CmdVersion,166 CmdVersion,
153 CmdZen,167 CmdZen,
154 CmdTranslateC,
155 CmdTargets,
156};168};
157169
158static const char *default_zig_cache_name = "zig-cache";170static const char *default_zig_cache_name = "zig-cache";
...@@ -251,7 +263,7 @@ int main(int argc, char **argv) {...@@ -251,7 +263,7 @@ int main(int argc, char **argv) {
251 if (init_kind != InitKindNone) {263 if (init_kind != InitKindNone) {
252 if (argc >= 3) {264 if (argc >= 3) {
253 fprintf(stderr, "Unexpected extra argument: %s\n", argv[2]);265 fprintf(stderr, "Unexpected extra argument: %s\n", argv[2]);
254 return usage(arg0);266 return print_error_usage(arg0);
255 }267 }
256 Buf *cmd_template_path = buf_alloc();268 Buf *cmd_template_path = buf_alloc();
257 os_path_join(get_zig_special_dir(), buf_create_from_str(init_cmd), cmd_template_path);269 os_path_join(get_zig_special_dir(), buf_create_from_str(init_cmd), cmd_template_path);
...@@ -326,7 +338,7 @@ int main(int argc, char **argv) {...@@ -326,7 +338,7 @@ int main(int argc, char **argv) {
326 }338 }
327 }339 }
328340
329 Cmd cmd = CmdInvalid;341 Cmd cmd = CmdNone;
330 EmitFileType emit_file_type = EmitFileTypeBinary;342 EmitFileType emit_file_type = EmitFileTypeBinary;
331 const char *in_file = nullptr;343 const char *in_file = nullptr;
332 const char *out_file = nullptr;344 const char *out_file = nullptr;
...@@ -547,7 +559,7 @@ int main(int argc, char **argv) {...@@ -547,7 +559,7 @@ int main(int argc, char **argv) {
547 } else if (strcmp(arg, "--pkg-begin") == 0) {559 } else if (strcmp(arg, "--pkg-begin") == 0) {
548 if (i + 2 >= argc) {560 if (i + 2 >= argc) {
549 fprintf(stderr, "Expected 2 arguments after --pkg-begin\n");561 fprintf(stderr, "Expected 2 arguments after --pkg-begin\n");
550 return usage(arg0);562 return print_error_usage(arg0);
551 }563 }
552 CliPkg *new_cur_pkg = allocate<CliPkg>(1);564 CliPkg *new_cur_pkg = allocate<CliPkg>(1);
553 i += 1;565 i += 1;
...@@ -565,7 +577,7 @@ int main(int argc, char **argv) {...@@ -565,7 +577,7 @@ int main(int argc, char **argv) {
565 cur_pkg = cur_pkg->parent;577 cur_pkg = cur_pkg->parent;
566 } else if (i + 1 >= argc) {578 } else if (i + 1 >= argc) {
567 fprintf(stderr, "Expected another argument after %s\n", arg);579 fprintf(stderr, "Expected another argument after %s\n", arg);
568 return usage(arg0);580 return print_error_usage(arg0);
569 } else {581 } else {
570 i += 1;582 i += 1;
571 if (strcmp(arg, "--output") == 0) {583 if (strcmp(arg, "--output") == 0) {
...@@ -581,7 +593,7 @@ int main(int argc, char **argv) {...@@ -581,7 +593,7 @@ int main(int argc, char **argv) {
581 color = ErrColorOff;593 color = ErrColorOff;
582 } else {594 } else {
583 fprintf(stderr, "--color options are 'auto', 'on', or 'off'\n");595 fprintf(stderr, "--color options are 'auto', 'on', or 'off'\n");
584 return usage(arg0);596 return print_error_usage(arg0);
585 }597 }
586 } else if (strcmp(arg, "--cache") == 0) {598 } else if (strcmp(arg, "--cache") == 0) {
587 if (strcmp(argv[i], "auto") == 0) {599 if (strcmp(argv[i], "auto") == 0) {
...@@ -592,7 +604,7 @@ int main(int argc, char **argv) {...@@ -592,7 +604,7 @@ int main(int argc, char **argv) {
592 enable_cache = CacheOptOff;604 enable_cache = CacheOptOff;
593 } else {605 } else {
594 fprintf(stderr, "--cache options are 'auto', 'on', or 'off'\n");606 fprintf(stderr, "--cache options are 'auto', 'on', or 'off'\n");
595 return usage(arg0);607 return print_error_usage(arg0);
596 }608 }
597 } else if (strcmp(arg, "--emit") == 0) {609 } else if (strcmp(arg, "--emit") == 0) {
598 if (strcmp(argv[i], "asm") == 0) {610 if (strcmp(argv[i], "asm") == 0) {
...@@ -603,7 +615,7 @@ int main(int argc, char **argv) {...@@ -603,7 +615,7 @@ int main(int argc, char **argv) {
603 emit_file_type = EmitFileTypeLLVMIr;615 emit_file_type = EmitFileTypeLLVMIr;
604 } else {616 } else {
605 fprintf(stderr, "--emit options are 'asm', 'bin', or 'llvm-ir'\n");617 fprintf(stderr, "--emit options are 'asm', 'bin', or 'llvm-ir'\n");
606 return usage(arg0);618 return print_error_usage(arg0);
607 }619 }
608 } else if (strcmp(arg, "--name") == 0) {620 } else if (strcmp(arg, "--name") == 0) {
609 out_name = argv[i];621 out_name = argv[i];
...@@ -672,10 +684,10 @@ int main(int argc, char **argv) {...@@ -672,10 +684,10 @@ int main(int argc, char **argv) {
672 test_exec_args.append(argv[i]);684 test_exec_args.append(argv[i]);
673 } else {685 } else {
674 fprintf(stderr, "Invalid argument: %s\n", arg);686 fprintf(stderr, "Invalid argument: %s\n", arg);
675 return usage(arg0);687 return print_error_usage(arg0);
676 }688 }
677 }689 }
678 } else if (cmd == CmdInvalid) {690 } else if (cmd == CmdNone) {
679 if (strcmp(arg, "build-exe") == 0) {691 if (strcmp(arg, "build-exe") == 0) {
680 cmd = CmdBuild;692 cmd = CmdBuild;
681 out_type = OutTypeExe;693 out_type = OutTypeExe;
...@@ -685,6 +697,8 @@ int main(int argc, char **argv) {...@@ -685,6 +697,8 @@ int main(int argc, char **argv) {
685 } else if (strcmp(arg, "build-lib") == 0) {697 } else if (strcmp(arg, "build-lib") == 0) {
686 cmd = CmdBuild;698 cmd = CmdBuild;
687 out_type = OutTypeLib;699 out_type = OutTypeLib;
700 } else if (strcmp(arg, "help") == 0) {
701 cmd = CmdHelp;
688 } else if (strcmp(arg, "run") == 0) {702 } else if (strcmp(arg, "run") == 0) {
689 cmd = CmdRun;703 cmd = CmdRun;
690 out_type = OutTypeExe;704 out_type = OutTypeExe;
...@@ -703,7 +717,7 @@ int main(int argc, char **argv) {...@@ -703,7 +717,7 @@ int main(int argc, char **argv) {
703 cmd = CmdBuiltin;717 cmd = CmdBuiltin;
704 } else {718 } else {
705 fprintf(stderr, "Unrecognized command: %s\n", arg);719 fprintf(stderr, "Unrecognized command: %s\n", arg);
706 return usage(arg0);720 return print_error_usage(arg0);
707 }721 }
708 } else {722 } else {
709 switch (cmd) {723 switch (cmd) {
...@@ -719,16 +733,17 @@ int main(int argc, char **argv) {...@@ -719,16 +733,17 @@ int main(int argc, char **argv) {
719 }733 }
720 } else {734 } else {
721 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);735 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
722 return usage(arg0);736 return print_error_usage(arg0);
723 }737 }
724 break;738 break;
725 case CmdBuiltin:739 case CmdBuiltin:
740 case CmdHelp:
726 case CmdVersion:741 case CmdVersion:
727 case CmdZen:742 case CmdZen:
728 case CmdTargets:743 case CmdTargets:
729 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);744 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
730 return usage(arg0);745 return print_error_usage(arg0);
731 case CmdInvalid:746 case CmdNone:
732 zig_unreachable();747 zig_unreachable();
733 }748 }
734 }749 }
...@@ -751,19 +766,19 @@ int main(int argc, char **argv) {...@@ -751,19 +766,19 @@ int main(int argc, char **argv) {
751 if (target_arch) {766 if (target_arch) {
752 if (parse_target_arch(target_arch, &target->arch)) {767 if (parse_target_arch(target_arch, &target->arch)) {
753 fprintf(stderr, "invalid --target-arch argument\n");768 fprintf(stderr, "invalid --target-arch argument\n");
754 return usage(arg0);769 return print_error_usage(arg0);
755 }770 }
756 }771 }
757 if (target_os) {772 if (target_os) {
758 if (parse_target_os(target_os, &target->os)) {773 if (parse_target_os(target_os, &target->os)) {
759 fprintf(stderr, "invalid --target-os argument\n");774 fprintf(stderr, "invalid --target-os argument\n");
760 return usage(arg0);775 return print_error_usage(arg0);
761 }776 }
762 }777 }
763 if (target_environ) {778 if (target_environ) {
764 if (parse_target_environ(target_environ, &target->env_type)) {779 if (parse_target_environ(target_environ, &target->env_type)) {
765 fprintf(stderr, "invalid --target-environ argument\n");780 fprintf(stderr, "invalid --target-environ argument\n");
766 return usage(arg0);781 return print_error_usage(arg0);
767 }782 }
768 }783 }
769 }784 }
...@@ -785,13 +800,13 @@ int main(int argc, char **argv) {...@@ -785,13 +800,13 @@ int main(int argc, char **argv) {
785 {800 {
786 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0) {801 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0) {
787 fprintf(stderr, "Expected source file argument or at least one --object or --assembly argument.\n");802 fprintf(stderr, "Expected source file argument or at least one --object or --assembly argument.\n");
788 return usage(arg0);803 return print_error_usage(arg0);
789 } else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) {804 } else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) {
790 fprintf(stderr, "Expected source file argument.\n");805 fprintf(stderr, "Expected source file argument.\n");
791 return usage(arg0);806 return print_error_usage(arg0);
792 } else if (cmd == CmdBuild && out_type == OutTypeObj && objects.length != 0) {807 } else if (cmd == CmdBuild && out_type == OutTypeObj && objects.length != 0) {
793 fprintf(stderr, "When building an object file, --object arguments are invalid.\n");808 fprintf(stderr, "When building an object file, --object arguments are invalid.\n");
794 return usage(arg0);809 return print_error_usage(arg0);
795 }810 }
796811
797 assert(cmd != CmdBuild || out_type != OutTypeUnknown);812 assert(cmd != CmdBuild || out_type != OutTypeUnknown);
...@@ -820,7 +835,7 @@ int main(int argc, char **argv) {...@@ -820,7 +835,7 @@ int main(int argc, char **argv) {
820835
821 if (need_name && buf_out_name == nullptr) {836 if (need_name && buf_out_name == nullptr) {
822 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");837 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");
823 return usage(arg0);838 return print_error_usage(arg0);
824 }839 }
825840
826 Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;841 Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;
...@@ -1012,6 +1027,8 @@ int main(int argc, char **argv) {...@@ -1012,6 +1027,8 @@ int main(int argc, char **argv) {
1012 zig_unreachable();1027 zig_unreachable();
1013 }1028 }
1014 }1029 }
1030 case CmdHelp:
1031 return print_full_usage(arg0);
1015 case CmdVersion:1032 case CmdVersion:
1016 printf("%s\n", ZIG_VERSION_STRING);1033 printf("%s\n", ZIG_VERSION_STRING);
1017 return EXIT_SUCCESS;1034 return EXIT_SUCCESS;
...@@ -1020,7 +1037,8 @@ int main(int argc, char **argv) {...@@ -1020,7 +1037,8 @@ int main(int argc, char **argv) {
1020 return EXIT_SUCCESS;1037 return EXIT_SUCCESS;
1021 case CmdTargets:1038 case CmdTargets:
1022 return print_target_list(stdout);1039 return print_target_list(stdout);
1023 case CmdInvalid:1040 case CmdNone:
1024 return usage(arg0);1041 fprintf(stderr, "Zig programming language\n");
1042 return print_error_usage(arg0);
1025 }1043 }
1026}1044}