@@ -1021,12 +1021,11 @@ necessary type-checking and type conversions to be performed.
10211021If the type _ keyword is used with the default _ keyword, the type converter
10221022is only applied if the default is a string.
10231023
1024- The argument to ``type `` can be any callable that accepts a single string.
1024+ The argument to ``type `` can be a callable that accepts a single string or
1025+ the name of a registered type (see :meth: `~ArgumentParser.register `)
10251026If the function raises :exc: `ArgumentTypeError `, :exc: `TypeError `, or
10261027:exc: `ValueError `, the exception is caught and a nicely formatted error
1027- message is displayed. Other exception types are not handled. You can also
1028- customize how the type is displayed in error messages by registering it with
1029- the parser using :meth: `~ArgumentParser.register `.
1028+ message is displayed. Other exception types are not handled.
10301029
10311030Common built-in types and functions can be used as type converters:
10321031
@@ -2188,6 +2187,8 @@ Registering custom types or actions
21882187 >>> parser.register('type', 'hexadecimal integer', lambda s: int(s, 16))
21892188 >>> parser.add_argument('--foo', type='hexadecimal integer')
21902189 _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type='hexadecimal integer', choices=None, required=False, help=None, metavar=None, deprecated=False)
2190+ >>> parser.parse_args(['--foo', '0xFA'])
2191+ Namespace(foo=250)
21912192 >>> parser.parse_args(['--foo', '1.2'])
21922193 usage: PROG [-h] [--foo FOO]
21932194 PROG: error: argument --foo: invalid 'hexadecimal integer' value: '1.2'
0 commit comments