Styling
Control the appearance and colors of your CLI help output.
ColorChoice
clap.styling.ColorChoice
Bases: Enum
Represents the color preferences for help output.
Attributes:
-
Always–Enables colored output regardless of whether or not the output is going to a terminal/TTY.
-
Auto–Enables colored output only when the output is going to a terminal or TTY.
-
Never–Disables colored output no matter if the output is going to a terminal/TTY, or not.
Always
class-attribute
instance-attribute
Always = auto()
Enables colored output regardless of whether or not the output is going to a terminal/TTY.
Example:
import clap
from clap import ColorChoice
@clap.command(color=ColorChoice.Always)
class Cli:
...
Auto
class-attribute
instance-attribute
Auto = auto()
Enables colored output only when the output is going to a terminal or TTY.
Example:
import clap
from clap import ColorChoice
@clap.command(color=ColorChoice.Auto)
class Cli:
...
Never
class-attribute
instance-attribute
Never = auto()
Disables colored output no matter if the output is going to a terminal/TTY, or not.
Example:
import clap
from clap import ColorChoice
@clap.command(color=ColorChoice.Never)
class Cli:
...
AnsiColor
clap.styling.AnsiColor
Bases: IntEnum
Available 4-bit ANSI color palette codes.
The user's terminal defines the meaning of the each palette code.
Attributes:
-
Black–Black: #0 (foreground code
30, background code40). -
Blue–Blue: #4 (foreground code
34, background code44). -
BrightBlack–Bright black: #0 (foreground code
90, background code100). -
BrightBlue–Bright blue: #4 (foreground code
94, background code104). -
BrightCyan–Bright cyan: #6 (foreground code
96, background code106). -
BrightGreen–Bright green: #2 (foreground code
92, background code102). -
BrightMagenta–Bright magenta: #5 (foreground code
95, background code105). -
BrightRed–Bright red: #1 (foreground code
91, background code101). -
BrightWhite–Bright white: #7 (foreground code
97, background code107). -
BrightYellow–Bright yellow: #3 (foreground code
93, background code103). -
Cyan–Cyan: #6 (foreground code
36, background code46). -
Green–Green: #2 (foreground code
32, background code42). -
Magenta–Magenta: #5 (foreground code
35, background code45). -
Red–Red: #1 (foreground code
31, background code41). -
White–White: #7 (foreground code
37, background code47). -
Yellow–Yellow: #3 (foreground code
33, background code43).
Black
class-attribute
instance-attribute
Black = auto()
Black: #0 (foreground code 30, background code 40).
Blue
class-attribute
instance-attribute
Blue = auto()
Blue: #4 (foreground code 34, background code 44).
BrightBlack
class-attribute
instance-attribute
BrightBlack = auto()
Bright black: #0 (foreground code 90, background code 100).
BrightBlue
class-attribute
instance-attribute
BrightBlue = auto()
Bright blue: #4 (foreground code 94, background code 104).
BrightCyan
class-attribute
instance-attribute
BrightCyan = auto()
Bright cyan: #6 (foreground code 96, background code 106).
BrightGreen
class-attribute
instance-attribute
BrightGreen = auto()
Bright green: #2 (foreground code 92, background code 102).
BrightMagenta
class-attribute
instance-attribute
BrightMagenta = auto()
Bright magenta: #5 (foreground code 95, background code 105).
BrightRed
class-attribute
instance-attribute
BrightRed = auto()
Bright red: #1 (foreground code 91, background code 101).
BrightWhite
class-attribute
instance-attribute
BrightWhite = auto()
Bright white: #7 (foreground code 97, background code 107).
BrightYellow
class-attribute
instance-attribute
BrightYellow = auto()
Bright yellow: #3 (foreground code 93, background code 103).
Cyan
class-attribute
instance-attribute
Cyan = auto()
Cyan: #6 (foreground code 36, background code 46).
Green
class-attribute
instance-attribute
Green = auto()
Green: #2 (foreground code 32, background code 42).
Magenta
class-attribute
instance-attribute
Magenta = auto()
Magenta: #5 (foreground code 35, background code 45).
Red
class-attribute
instance-attribute
Red = auto()
Red: #1 (foreground code 31, background code 41).
White
class-attribute
instance-attribute
White = auto()
White: #7 (foreground code 37, background code 47).
Yellow
class-attribute
instance-attribute
Yellow = auto()
Yellow: #3 (foreground code 33, background code 43).
Style
clap.styling.Style
dataclass
Style(
color_fg: Optional[AnsiColor] = None,
color_bg: Optional[AnsiColor] = None,
is_bold: bool = False,
is_dimmed: bool = False,
is_italic: bool = False,
is_underline: bool = False,
)
ANSI text styling.
You can print a Style to render the corresponding ANSI code. Using the
alternate flag # will render the ANSI reset code, if needed. Together,
this makes it convenient to render styles using inline format arguments.
Example:
style = Style().bold()
value = 42
print(f"{style}value{style:#}")
Methods:
-
bg_color–Set background color.
-
bold–Apply
boldeffect. -
dimmed–Apply
dimmedeffect. -
fg_color–Set foreground color.
-
italic–Apply
italiceffect. -
render_bg–Render the ANSI code for a background color.
-
render_fg–Render the ANSI code for a foreground color.
-
render_reset–Renders the ANSI reset code.
-
underline–Apply
underlineeffect.
bg_color
Set background color.
Source code in clap/styling.py
137 138 139 140 | |
bold
bold() -> Style
Apply bold effect.
Source code in clap/styling.py
112 113 114 115 | |
dimmed
dimmed() -> Style
Apply dimmed effect.
Source code in clap/styling.py
117 118 119 120 | |
fg_color
Set foreground color.
Source code in clap/styling.py
132 133 134 135 | |
italic
italic() -> Style
Apply italic effect.
Source code in clap/styling.py
122 123 124 125 | |
render_bg
render_bg() -> str
Render the ANSI code for a background color.
Source code in clap/styling.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
render_fg
render_fg() -> str
Render the ANSI code for a foreground color.
Source code in clap/styling.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
render_reset
render_reset() -> str
Renders the ANSI reset code.
Ellides the code if there is nothing to reset.
Source code in clap/styling.py
184 185 186 187 188 189 190 191 | |
underline
underline() -> Style
Apply underline effect.
Source code in clap/styling.py
127 128 129 130 | |
Styles
clap.styling.Styles
Styles()
Terminal styling definitions.
Example:
from clap import AnsiColor, Style, Styles
styles = (Styles().header(Style().bold().underline())
.literal(Style().fg_color(AnsiColor.Green).bold()))
Methods:
-
header–General heading style, e.g.,
Commands. -
literal–Literal command-line syntax, e.g.,
--help. -
placeholder–Descriptions within command-line syntax, e.g.,
value_name. -
styled–Default terminal styling.
-
usage–Usage heading.
Source code in clap/styling.py
230 231 232 233 234 | |
header
General heading style, e.g., Commands.
Source code in clap/styling.py
244 245 246 247 | |
literal
Literal command-line syntax, e.g., --help.
Source code in clap/styling.py
249 250 251 252 | |
placeholder
Descriptions within command-line syntax, e.g., value_name.
Source code in clap/styling.py
259 260 261 262 | |
styled
classmethod
styled() -> Styles
Default terminal styling.
Source code in clap/styling.py
236 237 238 239 240 241 242 | |
Functions
clap.styling.determine_color_usage
determine_color_usage(color_choice: ColorChoice) -> bool
Source code in clap/styling.py
265 266 267 268 269 270 271 272 | |