mirror of
https://github.com/python/cpython.git
synced 2025-10-18 04:38:07 +00:00
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
message and Python version number and exit immediately. Closes patch #101496.
This commit is contained in:
parent
645693783c
commit
3b2aedbdc5
1 changed files with 32 additions and 8 deletions
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
/* Python interpreter main program */
|
/* Python interpreter main program */
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
@ -51,6 +50,8 @@ static char *usage_mid = "\
|
||||||
-U : Unicode literals: treats '...' literals like u'...'\n\
|
-U : Unicode literals: treats '...' literals like u'...'\n\
|
||||||
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
|
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
|
||||||
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
|
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
|
||||||
|
-h : print this help message and exit\n\
|
||||||
|
-V : print the Python version number and exit\n\
|
||||||
-c cmd : program passed in as string (terminates option list)\n\
|
-c cmd : program passed in as string (terminates option list)\n\
|
||||||
file : program read from script file\n\
|
file : program read from script file\n\
|
||||||
- : program read from stdin (default; interactive mode if a tty)\n\
|
- : program read from stdin (default; interactive mode if a tty)\n\
|
||||||
|
@ -66,6 +67,18 @@ PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
|
||||||
";
|
";
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(int exitcode, char* program)
|
||||||
|
{
|
||||||
|
fprintf(stderr, usage_line, program);
|
||||||
|
fprintf(stderr, usage_top);
|
||||||
|
fprintf(stderr, usage_mid);
|
||||||
|
fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP);
|
||||||
|
exit(exitcode);
|
||||||
|
/*NOTREACHED*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Main program */
|
/* Main program */
|
||||||
|
|
||||||
DL_EXPORT(int)
|
DL_EXPORT(int)
|
||||||
|
@ -81,6 +94,8 @@ Py_Main(int argc, char **argv)
|
||||||
int unbuffered = 0;
|
int unbuffered = 0;
|
||||||
int skipfirstline = 0;
|
int skipfirstline = 0;
|
||||||
int stdin_is_interactive = 0;
|
int stdin_is_interactive = 0;
|
||||||
|
int help = 0;
|
||||||
|
int version = 0;
|
||||||
|
|
||||||
orig_argc = argc; /* For Py_GetArgcArgv() */
|
orig_argc = argc; /* For Py_GetArgcArgv() */
|
||||||
orig_argv = argv;
|
orig_argv = argv;
|
||||||
|
@ -90,7 +105,7 @@ Py_Main(int argc, char **argv)
|
||||||
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
|
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
|
||||||
unbuffered = 1;
|
unbuffered = 1;
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "c:diOStuUvxX")) != EOF) {
|
while ((c = getopt(argc, argv, "c:diOStuUvxXhV")) != EOF) {
|
||||||
if (c == 'c') {
|
if (c == 'c') {
|
||||||
/* -c is the last option; following arguments
|
/* -c is the last option; following arguments
|
||||||
that look like options are left for the
|
that look like options are left for the
|
||||||
|
@ -142,21 +157,30 @@ Py_Main(int argc, char **argv)
|
||||||
case 'U':
|
case 'U':
|
||||||
Py_UnicodeFlag++;
|
Py_UnicodeFlag++;
|
||||||
break;
|
break;
|
||||||
|
case 'h':
|
||||||
|
help++;
|
||||||
|
break;
|
||||||
|
case 'V':
|
||||||
|
version++;
|
||||||
|
break;
|
||||||
|
|
||||||
/* This space reserved for other options */
|
/* This space reserved for other options */
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, usage_line, argv[0]);
|
usage(2, argv[0]);
|
||||||
fprintf(stderr, usage_top);
|
|
||||||
fprintf(stderr, usage_mid);
|
|
||||||
fprintf(stderr, usage_bot,
|
|
||||||
DELIM, DELIM, PYTHONHOMEHELP);
|
|
||||||
exit(2);
|
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (help)
|
||||||
|
usage(0, argv[0]);
|
||||||
|
|
||||||
|
if (version) {
|
||||||
|
fprintf(stderr, "Python %s\n", PY_VERSION);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (command == NULL && optind < argc &&
|
if (command == NULL && optind < argc &&
|
||||||
strcmp(argv[optind], "-") != 0)
|
strcmp(argv[optind], "-") != 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue