kopia lustrzana https://github.com/micropython/micropython
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.pull/5606/head
rodzic
5a63bc5a44
commit
83439e38fc
|
@ -8,7 +8,7 @@ Command line options
|
||||||
|
|
||||||
Usage::
|
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:
|
Invocation options:
|
||||||
|
@ -38,6 +38,10 @@ interactive REPL mode.
|
||||||
|
|
||||||
General options:
|
General options:
|
||||||
|
|
||||||
|
.. option:: -h
|
||||||
|
|
||||||
|
Prints a help message containing the command line usage and exits.
|
||||||
|
|
||||||
.. option:: -i
|
.. option:: -i
|
||||||
|
|
||||||
Enables inspection. When this flag is set, MicroPython will enter the
|
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);
|
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(
|
printf(
|
||||||
"usage: %s [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]\n"
|
"usage: %s [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
|
"-h : print this help message\n"
|
||||||
"-i : enable inspection via REPL after running command/module/file\n"
|
"-i : enable inspection via REPL after running command/module/file\n"
|
||||||
#if MICROPY_DEBUG_PRINTERS
|
#if MICROPY_DEBUG_PRINTERS
|
||||||
"-v : verbose (trace various operations); can be multiple\n"
|
"-v : verbose (trace various operations); can be multiple\n"
|
||||||
|
@ -335,7 +336,10 @@ STATIC int usage(char **argv) {
|
||||||
if (impl_opts_cnt == 0) {
|
if (impl_opts_cnt == 0) {
|
||||||
printf(" (none)\n");
|
printf(" (none)\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC int invalid_args(void) {
|
||||||
|
fprintf(stderr, "Invalid command line arguments. Use -h option for help.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,9 +347,13 @@ STATIC int usage(char **argv) {
|
||||||
STATIC void pre_process_options(int argc, char **argv) {
|
STATIC void pre_process_options(int argc, char **argv) {
|
||||||
for (int a = 1; a < argc; a++) {
|
for (int a = 1; a < argc; a++) {
|
||||||
if (argv[a][0] == '-') {
|
if (argv[a][0] == '-') {
|
||||||
|
if (strcmp(argv[a], "-h") == 0) {
|
||||||
|
print_help(argv);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
if (strcmp(argv[a], "-X") == 0) {
|
if (strcmp(argv[a], "-X") == 0) {
|
||||||
if (a + 1 >= argc) {
|
if (a + 1 >= argc) {
|
||||||
exit(usage(argv));
|
exit(invalid_args());
|
||||||
}
|
}
|
||||||
if (0) {
|
if (0) {
|
||||||
} else if (strcmp(argv[a + 1], "compile-only") == 0) {
|
} else if (strcmp(argv[a + 1], "compile-only") == 0) {
|
||||||
|
@ -394,8 +402,7 @@ STATIC void pre_process_options(int argc, char **argv) {
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
invalid_arg:
|
invalid_arg:
|
||||||
fprintf(stderr, "Invalid option\n");
|
exit(invalid_args());
|
||||||
exit(usage(argv));
|
|
||||||
}
|
}
|
||||||
a++;
|
a++;
|
||||||
}
|
}
|
||||||
|
@ -563,7 +570,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
|
||||||
inspect = true;
|
inspect = true;
|
||||||
} else if (strcmp(argv[a], "-c") == 0) {
|
} else if (strcmp(argv[a], "-c") == 0) {
|
||||||
if (a + 1 >= argc) {
|
if (a + 1 >= argc) {
|
||||||
return usage(argv);
|
return invalid_args();
|
||||||
}
|
}
|
||||||
ret = do_str(argv[a + 1]);
|
ret = do_str(argv[a + 1]);
|
||||||
if (ret & FORCED_EXIT) {
|
if (ret & FORCED_EXIT) {
|
||||||
|
@ -572,7 +579,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
|
||||||
a += 1;
|
a += 1;
|
||||||
} else if (strcmp(argv[a], "-m") == 0) {
|
} else if (strcmp(argv[a], "-m") == 0) {
|
||||||
if (a + 1 >= argc) {
|
if (a + 1 >= argc) {
|
||||||
return usage(argv);
|
return invalid_args();
|
||||||
}
|
}
|
||||||
mp_obj_t import_args[4];
|
mp_obj_t import_args[4];
|
||||||
import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]));
|
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)++);
|
for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return usage(argv);
|
return invalid_args();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
char *pathbuf = malloc(PATH_MAX);
|
char *pathbuf = malloc(PATH_MAX);
|
||||||
|
|
Ładowanie…
Reference in New Issue