Long options support

I've just closed a bug report wishing for long option support, pointing to a patch sitting in the patch tracker implementing this. Should we accept at least the very common options "--help" and "--version" in 2.5? Georg

Georg Brandl wrote:
Guido pronounced on this in May: "Guido van Rossum" <guido@python.org> wrote: > On 5/4/06, Fredrik Lundh <fredrik@pythonware.com> wrote: > > I'm +1 on adding --help and --version, +1 on adding -? and /? for windows only, > > -0=slightly sceptical if adding a generic long option machinery is worth it, and -1 > > on a guessing machinery. > > I fully support Fredrik's position on this issue. > > --Guido van Rossum (home page: http://www.python.org/~guido/) </F>

Fredrik Lundh wrote:
Late it comes, but here is a patch for getopt.c implementing this pronouncement. I think there's no need to wait for 2.6 with it, or is there? Cheers, Georg Index: Python/getopt.c =================================================================== --- Python/getopt.c (Revision 50599) +++ Python/getopt.c (Arbeitskopie) @@ -24,6 +24,9 @@ * davegottner@delphi.com. *---------------------------------------------------------------------------*/ +/* Modified to support --help and --version, as well as /? on Windows + * by Georg Brandl. */ + #include <stdio.h> #include <string.h> @@ -43,15 +46,35 @@ if (*opt_ptr == '\0') { - if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' || - argv[_PyOS_optind][1] == '\0' /* lone dash */ ) + if (_PyOS_optind >= argc) return -1; +#ifdef MS_WINDOWS + else if (strcmp(argv[_PyOS_optind], "/?") == 0) { + ++_PyOS_optind; + return 'h'; + } +#endif + else if (argv[_PyOS_optind][0] != '-' || + argv[_PyOS_optind][1] == '\0' /* lone dash */ ) + return -1; + else if (strcmp(argv[_PyOS_optind], "--") == 0) { ++_PyOS_optind; return -1; } + else if (strcmp(argv[_PyOS_optind], "--help") == 0) { + ++_PyOS_optind; + return 'h'; + } + + else if (strcmp(argv[_PyOS_optind], "--version") == 0) { + ++_PyOS_optind; + return 'V'; + } + + opt_ptr = &argv[_PyOS_optind++][1]; } @@ -62,7 +85,7 @@ if (_PyOS_opterr) fprintf(stderr, "Unknown option: -%c\n", option); - return '?'; + return '_'; } if (*(ptr + 1) == ':') { @@ -76,7 +99,7 @@ if (_PyOS_opterr) fprintf(stderr, "Argument expected for the -%c option\n", option); - return '?'; + return '_'; } _PyOS_optarg = argv[_PyOS_optind++]; Index: Modules/main.c =================================================================== --- Modules/main.c (Revision 50599) +++ Modules/main.c (Arbeitskopie) @@ -40,7 +40,7 @@ static int orig_argc; /* command line options */ -#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX" +#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX?" #ifndef RISCOS #define PROGRAM_OPTS BASE_OPTS @@ -62,7 +62,7 @@ -c cmd : program passed in as string (terminates option list)\n\ -d : debug output from parser (also PYTHONDEBUG=x)\n\ -E : ignore environment variables (such as PYTHONPATH)\n\ --h : print this help message and exit\n\ +-h : print this help message and exit (also --help)\n\ -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ and force prompts, even if stdin does not appear to be a terminal\n\ "; @@ -78,7 +78,7 @@ static char *usage_3 = "\ see man page for details on internal buffering relating to '-u'\n\ -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ --V : print the Python version number and exit\n\ +-V : print the Python version number and exit (also --version)\n\ -W arg : warning control (arg is action:message:category:module:lineno)\n\ -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ file : program read from script file\n\ @@ -313,6 +313,7 @@ Py_UnicodeFlag++; break; case 'h': + case '?': help++; break; case 'V':

Georg Brandl wrote:
Guido pronounced on this in May: "Guido van Rossum" <guido@python.org> wrote: > On 5/4/06, Fredrik Lundh <fredrik@pythonware.com> wrote: > > I'm +1 on adding --help and --version, +1 on adding -? and /? for windows only, > > -0=slightly sceptical if adding a generic long option machinery is worth it, and -1 > > on a guessing machinery. > > I fully support Fredrik's position on this issue. > > --Guido van Rossum (home page: http://www.python.org/~guido/) </F>

Fredrik Lundh wrote:
Late it comes, but here is a patch for getopt.c implementing this pronouncement. I think there's no need to wait for 2.6 with it, or is there? Cheers, Georg Index: Python/getopt.c =================================================================== --- Python/getopt.c (Revision 50599) +++ Python/getopt.c (Arbeitskopie) @@ -24,6 +24,9 @@ * davegottner@delphi.com. *---------------------------------------------------------------------------*/ +/* Modified to support --help and --version, as well as /? on Windows + * by Georg Brandl. */ + #include <stdio.h> #include <string.h> @@ -43,15 +46,35 @@ if (*opt_ptr == '\0') { - if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' || - argv[_PyOS_optind][1] == '\0' /* lone dash */ ) + if (_PyOS_optind >= argc) return -1; +#ifdef MS_WINDOWS + else if (strcmp(argv[_PyOS_optind], "/?") == 0) { + ++_PyOS_optind; + return 'h'; + } +#endif + else if (argv[_PyOS_optind][0] != '-' || + argv[_PyOS_optind][1] == '\0' /* lone dash */ ) + return -1; + else if (strcmp(argv[_PyOS_optind], "--") == 0) { ++_PyOS_optind; return -1; } + else if (strcmp(argv[_PyOS_optind], "--help") == 0) { + ++_PyOS_optind; + return 'h'; + } + + else if (strcmp(argv[_PyOS_optind], "--version") == 0) { + ++_PyOS_optind; + return 'V'; + } + + opt_ptr = &argv[_PyOS_optind++][1]; } @@ -62,7 +85,7 @@ if (_PyOS_opterr) fprintf(stderr, "Unknown option: -%c\n", option); - return '?'; + return '_'; } if (*(ptr + 1) == ':') { @@ -76,7 +99,7 @@ if (_PyOS_opterr) fprintf(stderr, "Argument expected for the -%c option\n", option); - return '?'; + return '_'; } _PyOS_optarg = argv[_PyOS_optind++]; Index: Modules/main.c =================================================================== --- Modules/main.c (Revision 50599) +++ Modules/main.c (Arbeitskopie) @@ -40,7 +40,7 @@ static int orig_argc; /* command line options */ -#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX" +#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX?" #ifndef RISCOS #define PROGRAM_OPTS BASE_OPTS @@ -62,7 +62,7 @@ -c cmd : program passed in as string (terminates option list)\n\ -d : debug output from parser (also PYTHONDEBUG=x)\n\ -E : ignore environment variables (such as PYTHONPATH)\n\ --h : print this help message and exit\n\ +-h : print this help message and exit (also --help)\n\ -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ and force prompts, even if stdin does not appear to be a terminal\n\ "; @@ -78,7 +78,7 @@ static char *usage_3 = "\ see man page for details on internal buffering relating to '-u'\n\ -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ --V : print the Python version number and exit\n\ +-V : print the Python version number and exit (also --version)\n\ -W arg : warning control (arg is action:message:category:module:lineno)\n\ -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ file : program read from script file\n\ @@ -313,6 +313,7 @@ Py_UnicodeFlag++; break; case 'h': + case '?': help++; break; case 'V':
participants (3)
-
Anthony Baxter
-
Fredrik Lundh
-
Georg Brandl