unix/main: Add command-line -h option for printing help text.
This adds a -h option to print the usage help text and adds a new, shorter error message that is printed when invalid arguments are given. This behaviour follows CPython (and other tools) more closely.
This commit is contained in:
parent
5a63bc5a44
commit
83439e38fc
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user