Can python support generating shell completion script for itself: `python -<TAB>`?

Can python support generating shell completion script for itself python -<TAB>? Just use the option name
and help to fill a shell completion script template, then python --print-completion XXsh or PYTHON_COMPLETE=zsh python to output it.

I notice https://github.com/python/cpython/blob/main/Python/initconfig.c#L34-L75

static const char usage_help[] = "\
Options (and corresponding environment variables):\n\
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
         and comparing bytes/bytearray with str. (-bb: issue errors)\n\
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
-c cmd : program passed in as string (terminates option list)\n\
-d     : turn on parser debugging output (for experts only, only works on\n\
         debug builds); also PYTHONDEBUG=x\n\

Can python provide a data structure like https://github.com/latex3/l3build/blob/main/l3build-arguments.lua#L37-L163,

Then generate help text and shell completion scripts from the data structure.

I have a python module that i use to write completion code for bash.

Sorry i have not completed docs. But there is an example in the code.

Oh, sorry, I means python --<TAB>, not any program written in python program --<TAB>.

No need to do this at runtime in the Python interpreter. You only need a shell script (actually different shell scripts for different shells, because they are completely incompatible). These script can be written manually or generated at the buid time. For now, it may be simpler to write them manually.

And on my computer I already have such scripts installed:

/usr/share/bash-completion/completions/python3.9
/usr/share/zsh/functions/Completion/Unix/_python

Oh, I thought python had a data structure to store option information, like the following pseudo code:

#include <stdio.h>
#include "third_parties/bash_completion.h"
#include "third_parties/zsh_completion.h"

struct Option
{
    char name[20];
    int nargs;
    char help[20];
    char completion[20];
    char stop_completion[20];
};

extern char* print_bash_completion(struct Option* options, int option_num)
extern char* print_zsh_completion(struct Option* options, int option_num)

char* print_help(struct Option* options, int option_num)
{
	...
}

int main(int argc, char *argv[])
{
	struct Option options[] = {
		{"-m", 1, "run library module as a script (terminates option list)", "modules", "option"},
		{"-E", 0, "ignore PYTHON* environment variables (such as PYTHONPATH)", "", ""},
		{"-h", 0, "print this help message and exit (also -? or --help)", "", "all"},
    ...
	};
	if (argv[1] == "--help") {
		print_help()
		return 0;
	}
	if (argv[1] == "--print-completion=bash") {
		print_bash_completion()
		return 0;
	}
	return 0;
}

However, after a glance of the cpython code, I found it is not similar with my
thought, which must write these text (help, completion) manually.

It was expected that Python will not have many options, and that they are rarely changed. Currently, when you add a new option, you have to edit multiple sites: the help output, the man page, the documentation, and of course the code that actually parses options and saves results. Options so non-uniform, that you need to do this all manually.

You can create a script which parses the help output and creates a completetion script, but it will be full of special cases and manually written code, because you want --check-hash-based-pycs a<TAB> and -X frozen_modules=<TAB> to work.