Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add angle brackets for required args in argparse

60 views
Skip to first unread message

scruel tao

unread,
Feb 15, 2023, 12:17:00 PM2/15/23
to
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
```

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

Thanks.

Mark Bourne

unread,
Feb 20, 2023, 3:39:44 PM2/20/23
to
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.

scruel tao

unread,
Feb 22, 2023, 9:43:46 PM2/22/23
to
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.
0 new messages