• =?UTF-8?Q?Re=3A_argparse_=E2=80=94_adding_a_=2D=2Dversion_flag_in_the_f

    From Skip Montanaro@21:1/5 to All on Sun Nov 27 18:24:31 2022
    class VersionAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string):
    print(VERSION)
    exit()
    ...
    parser.add_argument("-v", "--version", nargs=0, action=VersionAction)

    Thanks. An action class didn't occur to me. I looked briefly at the
    code for argparse to see how it handled --help. The added argument
    seemed normal, so gave up, figuring there was some special handling of
    that option.

    Skip

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Skip Montanaro@21:1/5 to All on Sun Nov 27 21:33:43 2022

    ummm, hate to say this, but have you checked the documentation? this
    case is supported using an action named 'version' without doing very much.


    Thanks, Mats.

    I actually searched all over the argparse docs. (There's a lot to digest. Honestly, if I wasn't attempting to be sort of up-to-date I'd just continue using getopt.) It never occurred to me that "version" would be special, so
    I didn't specifically search for it, or realize there would be a specific action devoted to it. I knew (the default) "help" was special, so I focused
    my search for it. Obviously, "--help" is a pretty bad search term.

    Skip



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Skip Montanaro@21:1/5 to All on Mon Nov 28 10:36:17 2022
    Thanks. It occurs to me that instead of providing two special actions
    ("help" and "version"), it might be worthwhile to provide a standard way of saying, "if present, process this option and exit before considering other details of the command line." Matt's example action works well enough for
    my needs, but it would be nice if more than one such option could be given.
    For example:

    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("url")
    parser.add_argument("--version", version="777", action="version")

    args = parser.parse_args(["--help", "--version"])

    Which option is processed depends on their order on the command line. I
    don't believe it's possible to run the script and see them both processed. That's probably a secondary consideration though. My script is working well enough in this regard now.

    Skip

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Loris Bennett on Tue Nov 29 13:14:44 2022
    On Tue, 29 Nov 2022 at 12:37, Loris Bennett <loris.bennett@fu-berlin.de> wrote:

    Mats Wichmann <mats@wichmann.us> writes:

    On 11/27/22 16:40, Skip Montanaro wrote:
    I have a script to which I'd like to add a --version flag. It should print >> the version number then exit, much in the same way --help prints the help >> text then exits. I haven't been able to figure that out. I always get a
    complaint about the required positional argument.
    I think I could use something like nargs='*', but that would push
    off
    detection of the presence of the positional arg to the application.
    Shouldn't I be able to tell argparse I'm going to process --verbose, then >> exit?

    ummm, hate to say this, but have you checked the documentation? this
    case is supported using an action named 'version' without doing very
    much.

    I hadn't noticed the action 'version'. I just use

    parser.add_argument(
    "-v", "--version", action="store_true", dest="version",
    help="print version"
    )


    That's still going to validate the rest of the args though - notably,
    you can't omit any mandatory arguments (like a subcommand).

    The version and help actions are implemented pretty simply, actually.
    They're just little action classes that do their action immediately on
    getting triggered. It should be easy enough to make any action you
    want that way.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)