Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Functions to help with argparse.

class cardinal_pythonlib.argparse_func.MapType(map_separator: str = ':', pair_separator: str = ', ', strip: bool = True, from_type: ~typing.Type = <class 'str'>, to_type: ~typing.Type = <class 'str'>)[source]

argparse type maker that maps strings to a dictionary (map).

  • map_separator – string that separates the “from” and “to” members of a pair

  • pair_separator – string that separates different pairs

  • strip – strip whitespace after splitting?

  • from_type – type to coerce “from” values to; e.g. str, int

  • to_type – type to coerce “to” values to; e.g. str, int

class cardinal_pythonlib.argparse_func.RawDescriptionArgumentDefaultsHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Combines the features of

  • RawDescriptionHelpFormatter – don’t mangle the description

  • ArgumentDefaultsHelpFormatter – print argument defaults

class cardinal_pythonlib.argparse_func.RawDescriptionArgumentDefaultsRichHelpFormatter(prog: str, indent_increment: int = 2, max_help_position: int = 24, width: int | None = None, console: r.Console | None = None)[source]

Combines the features of

  • RawDescriptionRichHelpFormatter – don’t mangle the description

  • ArgumentDefaultsRichHelpFormatter – print argument defaults

class cardinal_pythonlib.argparse_func.ShowAllSubparserHelpAction(option_strings, dest='==SUPPRESS==', default='==SUPPRESS==', help=None)[source]

Class to serve as the action for an argparse top-level parser that shows help for all subparsers. As per

cardinal_pythonlib.argparse_func.nonnegative_int(value: str) int[source]

argparse argument type that checks that its value is a non-negative integer.

cardinal_pythonlib.argparse_func.percentage(value: str) float[source]

argparse argument type that checks that its value is a percentage (in the sense of a float in the range [0, 100]).

cardinal_pythonlib.argparse_func.positive_int(value: str) int[source]

argparse argument type that checks that its value is a positive integer.

cardinal_pythonlib.argparse_func.str2bool(v: str) bool[source]

argparse type that maps strings in case-insensitive fashion like this:

argument strings                value
------------------------------- -----
'yes', 'true', 't', 'y', '1'    True
'no', 'false', 'f', 'n', '0'    False


Specimen usage:

    "--nice", type=str2bool, nargs='?',
    const=True,  # if --nice is present with no parameter
    default=NICE,  # if the argument is entirely absent
    help="Activate nice mode.")