Skip to content

Commit ee86751

Browse files
committed
Add dump_ast --version option
1 parent 48f84d8 commit ee86751

1 file changed

Lines changed: 45 additions & 6 deletions

File tree

tool/dump_ast.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,57 @@ print_error(const pm_diagnostic_t *diagnostic, void *data)
2828
}
2929

3030
int
31-
main(int argc, const char *argv[]) {
32-
if (argc != 2) {
33-
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
31+
main(int argc, const char *argv[])
32+
{
33+
const char *filepath = 0;
34+
35+
for (int i = 1; i < argc; ++i) {
36+
const char *arg = argv[i];
37+
if (arg[0] != '-') {
38+
if (filepath) {
39+
fprintf(stderr, "too many filename\n");
40+
goto usage;
41+
}
42+
filepath = arg;
43+
}
44+
else if (arg[1] == '-') {
45+
if (!arg[2]) break;
46+
if (strcmp(arg + 2, "version") == 0) {
47+
version:
48+
printf("Prism %s\n", PRISM_VERSION);
49+
return EXIT_SUCCESS;
50+
}
51+
else {
52+
fprintf(stderr, "unknown option %s\n", arg);
53+
goto usage;
54+
}
55+
}
56+
else {
57+
while (*++arg) {
58+
switch (*arg) {
59+
case 'v':
60+
goto version;
61+
default:
62+
fprintf(stderr, "unknown option -%c\n", *arg);
63+
goto usage;
64+
}
65+
}
66+
}
67+
}
68+
69+
if (!filepath) {
70+
usage:
71+
fprintf(stderr, "Usage: %s [options]... <filename>\n"
72+
"Options:\n"
73+
" -v, --version: show Prism version\n"
74+
"", argv[0]);
3475
return EXIT_FAILURE;
3576
}
3677

37-
const char *filepath = argv[1];
3878
pm_source_init_result_t init_result;
3979
pm_source_t *source = pm_source_mapped_new(filepath, 0, &init_result);
4080

41-
if (init_result != PM_SOURCE_INIT_SUCCESS)
42-
{
81+
if (init_result != PM_SOURCE_INIT_SUCCESS) {
4382
fprintf(stderr, "unable to map file: %s\n", filepath);
4483
return EXIT_FAILURE;
4584
}

0 commit comments

Comments
 (0)