Re: argsparse: allowing --version without mandatory options

Lawrence D’Oliveiro <[email protected]> Thu, 30 Oct 2025 21:57:52 -0000 (UTC)
Newsgroups comp.lang.python
Organization A noiseless patient Spider
Message-ID <[email protected]>
On Thu, 30 Oct 2025 11:12:28 +0100, Loris Bennett wrote:

> I am writing a program for the command-line which uses 'argsparse'.  I
> want to make some options mandatory by setting 'required=True', but
> still allow the program to run with the option '--version' (which just
> shows the version and then exits) even if the mandatory options are
> missing.
>
> Is there a standard way of doing this?

Crusty curmudgeon here, still sticking to good old getopt. It may be
simple, crude even, but on the other hand it doesn’t try to do too
much. It may be “soft-deprecated”, but at least that means it will
keep on doing what it does for the foreseeable future.

It doesn’t offer built-in help, but then I also like to write man
pages (yes, proper documentation) for my more complex scripts.