|
6 | 6 | #include <stdarg.h> |
7 | 7 | #include <sys/stat.h> |
8 | 8 | #include <sys/types.h> |
| 9 | +#include <errno.h> |
9 | 10 |
|
10 | 11 | #include "nlr.h" |
11 | 12 | #include "misc.h" |
@@ -212,13 +213,24 @@ mp_obj_t test_obj_new(int value) { |
212 | 213 | return o; |
213 | 214 | } |
214 | 215 |
|
215 | | -int usage(void) { |
| 216 | +int usage(char **argv) { |
216 | 217 | printf( |
217 | | -"usage: py [-X <opt>] [-c <command>] [<filename>]\n" |
| 218 | +"usage: %s [-X <opt>] [-c <command>] [<filename>]\n" |
218 | 219 | "\n" |
219 | | -"Implementation specific options:\n" |
| 220 | +"Implementation specific options:\n", argv[0] |
| 221 | +); |
| 222 | + int impl_opts_cnt = 0; |
| 223 | +#if MICROPY_ENABLE_GC |
| 224 | + printf( |
220 | 225 | " heapsize=<n> -- set the heap size for the GC\n" |
221 | 226 | ); |
| 227 | + impl_opts_cnt++; |
| 228 | +#endif |
| 229 | + |
| 230 | + if (impl_opts_cnt == 0) { |
| 231 | + printf(" (none)\n"); |
| 232 | + } |
| 233 | + |
222 | 234 | return 1; |
223 | 235 | } |
224 | 236 |
|
@@ -249,15 +261,15 @@ void pre_process_options(int argc, char **argv) { |
249 | 261 | if (argv[a][0] == '-') { |
250 | 262 | if (strcmp(argv[a], "-X") == 0) { |
251 | 263 | if (a + 1 >= argc) { |
252 | | - exit(usage()); |
| 264 | + exit(usage(argv)); |
253 | 265 | } |
254 | 266 | if (0) { |
255 | 267 | #if MICROPY_ENABLE_GC |
256 | 268 | } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) { |
257 | 269 | heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0); |
258 | 270 | #endif |
259 | 271 | } else { |
260 | | - exit(usage()); |
| 272 | + exit(usage(argv)); |
261 | 273 | } |
262 | 274 | a++; |
263 | 275 | } |
@@ -358,24 +370,30 @@ int main(int argc, char **argv) { |
358 | 370 | if (argv[a][0] == '-') { |
359 | 371 | if (strcmp(argv[a], "-c") == 0) { |
360 | 372 | if (a + 1 >= argc) { |
361 | | - return usage(); |
| 373 | + return usage(argv); |
362 | 374 | } |
363 | 375 | do_str(argv[a + 1]); |
364 | 376 | executed = true; |
365 | 377 | a += 1; |
366 | 378 | } else if (strcmp(argv[a], "-X") == 0) { |
367 | 379 | a += 1; |
368 | 380 | } else { |
369 | | - return usage(); |
| 381 | + return usage(argv); |
370 | 382 | } |
371 | 383 | } else { |
372 | | - // Set base dir of the script as first entry in sys.path |
373 | 384 | char *basedir = realpath(argv[a], NULL); |
374 | | - if (basedir != NULL) { |
375 | | - char *p = strrchr(basedir, '/'); |
376 | | - path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); |
377 | | - free(basedir); |
| 385 | + if (basedir == NULL) { |
| 386 | + fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno); |
| 387 | + perror(""); |
| 388 | + // CPython exits with 2 in such case |
| 389 | + exit(2); |
378 | 390 | } |
| 391 | + |
| 392 | + // Set base dir of the script as first entry in sys.path |
| 393 | + char *p = strrchr(basedir, '/'); |
| 394 | + path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); |
| 395 | + free(basedir); |
| 396 | + |
379 | 397 | for (int i = a; i < argc; i++) { |
380 | 398 | mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); |
381 | 399 | } |
|
0 commit comments