Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197576

Re: argsparse: allowing --version without mandatory options

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Loris Bennett" <loris.bennett@fu-berlin.de>
Newsgroups comp.lang.python
Subject Re: argsparse: allowing --version without mandatory options
Date Thu, 30 Oct 2025 13:50:42 +0100
Organization FUB-IT, Freie Universität Berlin
Lines 53
Message-ID <87pla4bljx.fsf@zedat.fu-berlin.de> (permalink)
References <87bjlod7g3.fsf@zedat.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain
X-Trace news.uni-berlin.de K64A2zenp9hzD0hNxa5iuAyXhHijmzE0fUBKU7j4K+LZFm
Cancel-Lock sha1:ADXZT8WYG78FcTLtnq1t4hBnECg= sha1:/WyWXdL9MpMbNLvusmIATqk/twE= sha256:P6Eu/8aRanvCGFLGqmV8ekkhd9ppOD5T7r6qydK5UJQ=
User-Agent Gnus/5.13 (Gnus v5.13)
Xref csiph.com comp.lang.python:197576

Show key headers only | View raw


"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

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

I see that instead of setting required to 'True' I can use an expression
which checks whether '--version' has been given:

    parser.add_argument(
        "uid", nargs="+",
        help="UIDs to send mail to"
    )
    parser.add_argument(
        '-s', '--subject', dest='subject', required=not '--version' in sys.argv,
        help="subject of email"
    )
    parser.add_argument(
        '-c', '--content_file', dest='content_file', required=not '--version' in sys.argv,
        help="file containing mail contents (without salutation or signature)"
    )
    parser.add_argument(
        '--version', action='store_true', dest='version',
        help="print version"

However, with the above, I still need to specify the mandatory argument,
even if I only want to see the version.

I guess I'll just have to change 

  nargs="+" 

to 

  nargs="*"

and check explicitly whether any arguments have been given.

Seems odd to me though that there is not a standard way of implementing
'--version' which works like '--help' does.

Cheers,

Loris

-- 
This signature is currently under constuction.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

argsparse: allowing --version without mandatory options "Loris Bennett" <loris.bennett@fu-berlin.de> - 2025-10-30 11:12 +0100
  Re: argsparse: allowing --version without mandatory options "Loris Bennett" <loris.bennett@fu-berlin.de> - 2025-10-30 13:50 +0100
    Re: argsparse: allowing --version without mandatory options "Jonathan N. Little" <lws4art@gmail.com> - 2025-11-03 22:19 -0500
  Re: argsparse: allowing --version without mandatory options Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-30 21:57 +0000

csiph-web