Re: argsparse: allowing --version without mandatory options

[email protected]
Newsgroups gmane.comp.python.general
Message-ID <aQOkpO_Xocj_C57F@anomaly>
On 2025-10-30 at 13:50:42 +0100,
Loris Bennett <[email protected]> wrote:

> "Loris Bennett" <[email protected]> writes:

> > 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?

Don't take this the wrong way, but what, exactly, is a mandatory option?
Mandatory and optional are quite opposite.  If you have mandatory
arguments, then make the arguments mandatory.  If you have optional
options, then make the options optional.  Otherwise, as a user, I just
have extra typing (i.e., the "--whatever" before the mandatory argument)
to type (redundancy intended).

Elsewhere, I've seen --version and --help options handled as special
cases that just do their thing in their "action" logic and exit the
program right then and there.  I'm not necessarily in favor of such a
design, but it's not as uncommon as I would prefer, and it does meet
your requirement.

HTH.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.