• Custom help format for a choice argparse argument

    From Ivan "Rambius" Ivanov@21:1/5 to All on Fri Jan 27 15:31:22 2023
    Hello,

    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    It works, but when I run it with the -h option it dumps all entries in pytz.all_timezones. I would like to modify the help format for just
    -z|--zone option. I read the docs about HelpFormatter and argparse.py
    and I ended up with

    class CustomHelpFormatter(argparse.HelpFormatter):
    def _metavar_formatter(self, action, default_metavar):
    if action.dest == 'zone':
    result = 'zone from pytz.all_timezones'
    def format(tuple_size):
    if isinstance(result, tuple):
    return result
    else:
    return (result, ) * tuple_size
    return format
    else:
    return super(CustomHelpFormatter,
    self)._metavar_formatter(action, default_metavar)


    def main():
    parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    This works, but is there a more elegant way to achieve it?

    Regards
    rambius

    --
    Tangra Mega Rock: http://www.radiotangra.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Ivan "Rambius" Ivanov on Sat Jan 28 08:06:39 2023
    On 27Jan2023 15:31, Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com> wrote: >I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    [...]

    It works, but when I run it with the -h option it dumps all entries in >pytz.all_timezones.

    What happens if you just presupply a `help=` parameter in
    `add_argument`?

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Weatherby,Gerard@21:1/5 to All on Fri Jan 27 21:22:47 2023
    Why not something like:

    parser.add_argument("-z", "--zone")
    args = parser.parse_args()
    if args.zone and args.zone not in ptyz.all_timezones:
    print(“Invalid timezone”,file=sys.stderr)




    From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com>
    Date: Friday, January 27, 2023 at 3:33 PM
    To: Python <python-list@python.org>
    Subject: Custom help format for a choice argparse argument
    *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

    Hello,

    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    It works, but when I run it with the -h option it dumps all entries in pytz.all_timezones. I would like to modify the help format for just
    -z|--zone option. I read the docs about HelpFormatter and argparse.py
    and I ended up with

    class CustomHelpFormatter(argparse.HelpFormatter):
    def _metavar_formatter(self, action, default_metavar):
    if action.dest == 'zone':
    result = 'zone from pytz.all_timezones'
    def format(tuple_size):
    if isinstance(result, tuple):
    return result
    else:
    return (result, ) * tuple_size
    return format
    else:
    return super(CustomHelpFormatter,
    self)._metavar_formatter(action, default_metavar)


    def main():
    parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    This works, but is there a more elegant way to achieve it?

    Regards
    rambius

    --
    Tangra Mega Rock: https://urldefense.com/v3/__http://www.radiotangra.com__;!!Cn_UX_p3!kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vTKFsFvaQ$<https://urldefense.com/v3/__http:/www.radiotangra.com__;!!Cn_UX_p3!
    kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vTKFsFvaQ$>
    -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vRXq-JKLg$<https://urldefense.com/v3/__https:/mail.python.org/mailman/
    listinfo/python-list__;!!Cn_UX_p3!kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vRXq-JKLg$>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ivan "Rambius" Ivanov@21:1/5 to Gerard on Fri Jan 27 16:27:17 2023
    Hello,

    On Fri, Jan 27, 2023 at 4:22 PM Weatherby,Gerard <gweatherby@uchc.edu> wrote:

    Why not something like:


    parser.add_argument("-z", "--zone")

    args = parser.parse_args()
    if args.zone and args.zone not in ptyz.all_timezones:

    print(“Invalid timezone”,file=sys.stderr)

    This is what I use now. I still wonder if I can mold HelpFormatter to
    do what I want it to do.

    …




    From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com>
    Date: Friday, January 27, 2023 at 3:33 PM
    To: Python <python-list@python.org>
    Subject: Custom help format for a choice argparse argument

    *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

    Hello,

    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    It works, but when I run it with the -h option it dumps all entries in pytz.all_timezones. I would like to modify the help format for just
    -z|--zone option. I read the docs about HelpFormatter and argparse.py
    and I ended up with

    class CustomHelpFormatter(argparse.HelpFormatter):
    def _metavar_formatter(self, action, default_metavar):
    if action.dest == 'zone':
    result = 'zone from pytz.all_timezones'
    def format(tuple_size):
    if isinstance(result, tuple):
    return result
    else:
    return (result, ) * tuple_size
    return format
    else:
    return super(CustomHelpFormatter, self)._metavar_formatter(action, default_metavar)


    def main():
    parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    This works, but is there a more elegant way to achieve it?

    Regards
    rambius

    --
    Tangra Mega Rock: https://urldefense.com/v3/__http://www.radiotangra.com__;!!Cn_UX_p3!kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vTKFsFvaQ$
    -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kiJusdm5pCptP3sOBX85KXqUJkqr2jSa4C_-WAqND7WkL-aw3BYbW50td_AcuzJ1XUPYIVO3JiLMc4gRWS885vRXq-JKLg$



    --
    Tangra Mega Rock: http://www.radiotangra.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ivan "Rambius" Ivanov@21:1/5 to cs@cskk.id.au on Fri Jan 27 16:53:02 2023
    Hello Cameron,

    On Fri, Jan 27, 2023 at 4:45 PM Cameron Simpson <cs@cskk.id.au> wrote:

    On 27Jan2023 15:31, Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com> wrote:
    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    [...]

    It works, but when I run it with the -h option it dumps all entries in >pytz.all_timezones.

    What happens if you just presupply a `help=` parameter in
    `add_argument`?

    I tried with def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones,
    help="a timezone from pytz.all_timezones")
    args = parser.parse_args()
    print(args)

    -h still shows all the contents of pytz.all_timezones.

    Regards
    rambius


    Cheers,
    Cameron Simpson <cs@cskk.id.au>
    --
    https://mail.python.org/mailman/listinfo/python-list



    --
    Tangra Mega Rock: http://www.radiotangra.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Ivan "Rambius" Ivanov on Fri Jan 27 18:21:47 2023
    On 1/27/2023 4:53 PM, Ivan "Rambius" Ivanov wrote:
    Hello Cameron,

    On Fri, Jan 27, 2023 at 4:45 PM Cameron Simpson <cs@cskk.id.au> wrote:

    On 27Jan2023 15:31, Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com> wrote:
    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    [...]

    It works, but when I run it with the -h option it dumps all entries in
    pytz.all_timezones.

    What happens if you just presupply a `help=` parameter in
    `add_argument`?

    I tried with def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones,
    help="a timezone from pytz.all_timezones")
    args = parser.parse_args()
    print(args)

    -h still shows all the contents of pytz.all_timezones.


    Adding a few arguments makes it work (with the help of the argparse doc
    page and Stack Overflow:

    https://stackoverflow.com/questions/14950964/overriding-default-argparse-h-behaviour):

    import argparse
    import pytz

    HELP ="""\nThis is the help message.
    This is the second line of the help message."""

    def main():
    parser = argparse.ArgumentParser(add_help=False, usage = HELP)
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones,
    help = argparse.SUPPRESS)
    parser.add_argument('-h', '--h', action = 'help',
    help = argparse.SUPPRESS)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")


    main()

    Your help message will display after "usage", like this -

    usage:
    This is the help message.
    This is the second line of the help message.

    You would give up have argparse automatically list all the options, but
    you can add add them manually to the HELP string.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Otten@21:1/5 to Ivan "Rambius" Ivanov on Mon Jan 30 23:20:26 2023
    On 27/01/2023 21:31, Ivan "Rambius" Ivanov wrote:
    Hello,

    I am developing a script that accepts a time zone as an option. The
    time zone can be any from pytz.all_timezones. I have

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    It works, but when I run it with the -h option it dumps all entries in pytz.all_timezones. I would like to modify the help format for just
    -z|--zone option. I read the docs about HelpFormatter and argparse.py
    and I ended up with

    class CustomHelpFormatter(argparse.HelpFormatter):
    def _metavar_formatter(self, action, default_metavar):
    if action.dest == 'zone':
    result = 'zone from pytz.all_timezones'
    def format(tuple_size):
    if isinstance(result, tuple):
    return result
    else:
    return (result, ) * tuple_size
    return format
    else:
    return super(CustomHelpFormatter, self)._metavar_formatter(action, default_metavar)


    def main():
    parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
    parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
    args = parser.parse_args()
    print(args)
    print(f"Specified timezone: {args.zone}")

    This works, but is there a more elegant way to achieve it?

    It may be sufficient to specify a metavar:

    import argparse
    p = argparse.ArgumentParser()
    p.add_argument("--foo", choices="alpha beta gamma".split(),
    metavar="<any greek letter>")
    [...]
    p.parse_args(["-h"])
    usage: [-h] [--foo <any greek letter>]

    optional arguments:
    -h, --help show this help message and exit
    --foo <any greek letter>

    While that helps with --help it doesn't always prevent the choices list
    from being spelt out:

    p.parse_args(["--foo", "whatever"])
    usage: [-h] [--foo <any greek letter>]
    : error: argument --foo: invalid choice: 'whatever' (choose from
    'alpha', 'beta', 'gamma')

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