• Add angle brackets for required args in argparse

    From scruel tao@21:1/5 to All on Wed Feb 15 07:33:37 2023
    SWYgd2UgaGF2ZSB0aGUgZm9sbG93aW5nIGNvZGU6DQpgYGANCnBhcnNlciA9IGFyZ3BhcnNlLkFy Z3VtZW50UGFyc2VyKGRlc2NyaXB0aW9uPSJ0ZXN0IikNCnBhcnNlci5hZGRfYXJndW1lbnQoJ3Bh dGgnKQ0KYGBgDQoNClJ1biBpdCB3aXRob3V0IGFyZ3MsIHdpbGwgZ2V0IGVycm9yIG1lc3NhZ2U6 DQpgYGANCnVzYWdlOiB0ZXN0LnB5IFstaF0gcGF0aA0KdGVzdC5weTogZXJyb3I6IHRoZSBmb2xs b3dpbmcgYXJndW1lbnRzIGFyZSByZXF1aXJlZDogcGF0aA0KYGBgDQoNCkhvd2V2ZXIsIEkgaG9w ZSB0aGUgbWVzc2FnZSBjYW4gYmUgYXMgdGhlIGZvbGxvd2luZzoNCmBgYA0KdXNhZ2U6IHRlc3Qu cHkgWy1oXSA8cGF0aD4NCnRlc3QucHk6IGVycm9yOiB0aGUgZm9sbG93aW5nIGFyZ3VtZW50cyBh cmUgcmVxdWlyZWQ6IHBhdGgNCmBgYA0KDQpPciBtaWdodCBjYW4gY29uc2lkZXIgdG8gcHJvdmlk ZSBhIHdheSB0byBsZXQgdXNlciBoYXZlIHRoZXJlIG93biBzdHlsZSwgbGlrZToNCmBgYA0KdXNh Z2U6IHRlc3QucHkgWy1oXSBwYXRoDQpgYGANCg0KVGhhbmtzLg0KDQo=

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Bourne@21:1/5 to scruel tao on Mon Feb 20 20:39:22 2023
    scruel tao wrote:
    If we have the following code:
    ```
    parser = argparse.ArgumentParser(description="test") parser.add_argument('path')
    ```

    Run it without args, will get error message:
    ```
    usage: test.py [-h] path
    test.py: error: the following arguments are required: path
    ```

    However, I hope the message can be as the following:
    ```
    usage: test.py [-h] <path>
    test.py: error: the following arguments are required: path
    ```

    The `metavar` argument to `add_argument` can be used to control how an
    argument is represented in the usage text:
    ```
    import argparse
    parser = argparse.ArgumentParser(description='test') parser.add_argument('path', metavar='<path>')
    parser.parse_args()
    ```

    Which results in:
    ```
    usage: test.py [-h] <path>
    test.py: error: the following arguments are required: <path>
    ```

    Or might can consider to provide a way to let user have there own style, like:
    ```
    usage: test.py [-h] path
    ```

    It's also possible to create a custom help formatter, overriding
    appropriate methods to control the formatting. For example:
    ```
    import argparse

    class CustomHelpFormatter(argparse.HelpFormatter):
    def _get_default_metavar_for_positional(self, action):
    default = super()._get_default_metavar_for_positional(action)
    return f'<{default}>'

    parser = argparse.ArgumentParser(
    description='test',
    formatter_class=CustomHelpFormatter)
    parser.add_argument('path')
    parser.parse_args()
    ```

    Which results in:
    ```
    usage: test.py [-h] <path>
    test.py: error: the following arguments are required: path
    ```

    That's a bit closer to what you asked for, since the required argument
    shown in the error message doesn't include the angle brackets. It also
    avoids needing to specify a `metavar` for every positional argument.
    However, it is overriding a non-public method of the `HelpFormatter`
    class, so might not work across all Python versions if the name or
    signature of that method changes (even if it does work with all current versions, it might break in future).

    --
    Mark.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From scruel tao@21:1/5 to All on Thu Feb 23 02:43:23 2023
    Thank you for your workarounds, Mark Bourne.
    `metavar` argument should be sufficient for infrequent use scenarios, and I will consider to use the custom help formatter if necessary.

    That's a bit closer to what you asked for, since the required argument
    shown in the error message doesn't include the angle brackets. It also
    avoids needing to specify a `metavar` for every positional argument.
    However, it is overriding a non-public method of the `HelpFormatter`
    class, so might not work across all Python versions if the name or
    signature of that method changes (even if it does work with all current
    versions, it might break in future).

    Your are right to be concerned, that’s why I still think, might the `argparse` can provide a more stable way which can set such format strategy globally, your workarounds are fine to work now, just I have to write the same code to parse either `metaver`
    or `formatter` every times I use argparse.
    Might can have different argparse subclasses, or make some HelpFormatter builtin, so that users won’t need to write them by themselves.

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