diff options
-rw-r--r-- | docs/unix/quickref.rst | 6 | ||||
-rw-r--r-- | ports/unix/main.c | 21 |
2 files changed, 19 insertions, 8 deletions
diff --git a/docs/unix/quickref.rst b/docs/unix/quickref.rst index ea0e6865c..a5180fa6c 100644 --- a/docs/unix/quickref.rst +++ b/docs/unix/quickref.rst @@ -8,7 +8,7 @@ Command line options Usage:: - micropython [ -i ] [ -O<level> ] [ -v ] [ -X <option> ] [ -c <command> | -m <module> | <script> ] [ <args> ] + micropython [ -h ] [ -i ] [ -O<level> ] [ -v ] [ -X <option> ] [ -c <command> | -m <module> | <script> ] [ <args> ] Invocation options: @@ -38,6 +38,10 @@ interactive REPL mode. General options: +.. option:: -h + + Prints a help message containing the command line usage and exits. + .. option:: -i Enables inspection. When this flag is set, MicroPython will enter the diff --git a/ports/unix/main.c b/ports/unix/main.c index 14283321f..3f415285c 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -303,10 +303,11 @@ STATIC int do_str(const char *str) { return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false); } -STATIC int usage(char **argv) { +STATIC void print_help(char **argv) { printf( "usage: %s [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]\n" "Options:\n" +"-h : print this help message\n" "-i : enable inspection via REPL after running command/module/file\n" #if MICROPY_DEBUG_PRINTERS "-v : verbose (trace various operations); can be multiple\n" @@ -335,7 +336,10 @@ STATIC int usage(char **argv) { if (impl_opts_cnt == 0) { printf(" (none)\n"); } +} +STATIC int invalid_args(void) { + fprintf(stderr, "Invalid command line arguments. Use -h option for help.\n"); return 1; } @@ -343,9 +347,13 @@ STATIC int usage(char **argv) { STATIC void pre_process_options(int argc, char **argv) { for (int a = 1; a < argc; a++) { if (argv[a][0] == '-') { + if (strcmp(argv[a], "-h") == 0) { + print_help(argv); + exit(0); + } if (strcmp(argv[a], "-X") == 0) { if (a + 1 >= argc) { - exit(usage(argv)); + exit(invalid_args()); } if (0) { } else if (strcmp(argv[a + 1], "compile-only") == 0) { @@ -394,8 +402,7 @@ STATIC void pre_process_options(int argc, char **argv) { #endif } else { invalid_arg: - fprintf(stderr, "Invalid option\n"); - exit(usage(argv)); + exit(invalid_args()); } a++; } @@ -563,7 +570,7 @@ MP_NOINLINE int main_(int argc, char **argv) { inspect = true; } else if (strcmp(argv[a], "-c") == 0) { if (a + 1 >= argc) { - return usage(argv); + return invalid_args(); } ret = do_str(argv[a + 1]); if (ret & FORCED_EXIT) { @@ -572,7 +579,7 @@ MP_NOINLINE int main_(int argc, char **argv) { a += 1; } else if (strcmp(argv[a], "-m") == 0) { if (a + 1 >= argc) { - return usage(argv); + return invalid_args(); } mp_obj_t import_args[4]; import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1])); @@ -627,7 +634,7 @@ MP_NOINLINE int main_(int argc, char **argv) { for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++); } } else { - return usage(argv); + return invalid_args(); } } else { char *pathbuf = malloc(PATH_MAX); |