• argsparse: allowing --version without mandatory options

    From Loris Bennett@[email protected] to comp.lang.python on Thu Oct 30 11:12:28 2025
    From Newsgroup: comp.lang.python

    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?

    Cheers,

    Loris
    --
    This signature is currently under constuction.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Loris Bennett@[email protected] to comp.lang.python on Thu Oct 30 13:50:42 2025
    From Newsgroup: comp.lang.python

    "Loris Bennett" <[email protected]> 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.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@[email protected] to comp.lang.python on Thu Oct 30 21:57:52 2025
    From Newsgroup: comp.lang.python

    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.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Jonathan N. Little@[email protected] to comp.lang.python on Mon Nov 3 22:19:45 2025
    From Newsgroup: comp.lang.python

    Loris Bennett wrote:
    "Loris Bennett" <[email protected]> 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
    --- Synchronet 3.21a-Linux NewsLink 1.2