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


Groups > comp.lang.python > #197578

Re: argsparse: allowing --version without mandatory options

Path csiph.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From "Jonathan N. Little" <lws4art@gmail.com>
Newsgroups comp.lang.python
Subject Re: argsparse: allowing --version without mandatory options
Date Mon, 3 Nov 2025 22:19:45 -0500
Organization LITTLE WORKS STUDIO
Lines 97
Message-ID <10ebrci$3c3cn$1@dont-email.me> (permalink)
References <87bjlod7g3.fsf@zedat.fu-berlin.de> <87pla4bljx.fsf@zedat.fu-berlin.de>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
Injection-Date Tue, 04 Nov 2025 03:19:47 +0000 (UTC)
Injection-Info dont-email.me; posting-host="a5a290ed95bdeb6fca5aaefe60160049"; logging-data="3542423"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vxGq3HN+LvWuowxT1e9+mk2S7VbTf8Xc="
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0 SeaMonkey/2.53.21
Cancel-Lock sha1:CQnQBBURyDf6K23+VBzWYMJwI84=
In-Reply-To <87pla4bljx.fsf@zedat.fu-berlin.de>
X-Face o[H8T0h*NGH`K`P)s+4PmYlcy|GNl`~+L6Fi.m:%15m[c%{C7V-ump|WiCYPkQ+hFJhq;XW5^1Rg_El'"fE$~AcYW$Pq\yeh9K_-dJqlQ5\y2\;[yw5DYCtOtsf_.TUy}0U\oL^>[3Y#{AP2^o'bG`bwj`]]UNpCxY\(~xK9b+uZKxrb*4-rkD+
X-Dan Yes Dan this is a Winbox
Xref csiph.com comp.lang.python:197578

Show key headers only | View raw


Loris Bennett wrote:
> "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:


What I do is separate it into two functions. First function that sets up
the parser object and returns the parser object:

def make_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser( ... )
    ...
    return parser

The code is similar to yours below, but for version my action references
an f string variable:

parser.add_argument(
    "-v", "--version",  action="version",
    version=f"{APP_NAME} v{__version__}"
)


I typically have the optional args set to a default value, null string
or None. Then the second function which my main() calls this function
which returns a tuple of arg values here is one example:

def get_program_args() -> tuple:
    parser = make_parser()
    args = parser.parse_args()
    return args.action, args.profile, args.log_level

def main():
    # parser args
    action, profile_name, log_level = get_program_args()
    ...

Works for me.



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


-- 
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com

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