You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to document my Typer cli app. For my cli, I have a configuration with multiple values.
In my help, I want to show an example of a config file and its values. Typer uses string with markup as its way to generate the help message. Since, I would like to reuse one of my config file used for testing as the example in the help, I was looking for a way to insert markup in the text of the config automatically.
I found a way using a highlighter and a manual string replace to obtain a markup string accepted by Typer. But, I was wondering if there was a better way I missed.
Here is an example code of my solution:
fromtextwrapimportdedentimporttyperfromrich.consoleimportConsolefromrich.highlighterimportRegexHighlighterfromrich.styleimportStylefromrich.themeimportThemeapp=typer.Typer(rich_markup_mode="rich")
classConfigTOMLlHighlighter(RegexHighlighter):
"""Apply style to toml file."""base_style="toml."highlights= [
r"(?P<variable>\w+)\=",
r"(?P<string>[\'\"].*?[\"\'])",
]
defhighlight():
# config represent the config file I want to colorizeconfig="#Config used for testing and docs\nvariable='/complex/path'"theme_dict= {
"toml.variable": "white",
"toml.string": "green",
}
highlighter=ConfigTOMLlHighlighter()
highlight_text=highlighter(config)
raw_string=highlight_text.markupfork, vintheme_dict.items():
raw_string=raw_string.replace(k, v)
returnraw_stringdefhelp_string():
help_string=dedent(
""" This [blue]help string[/blue] supports [yellow]rich[/yellow] """
)
returnf"{help_string}\n{highlight()}"@app.command(help=help_string())defmain():
passif__name__=="__main__":
app()
P.S.: I looked at using console, theme and highlighter together, but they all returned markup converted to ANSI code or Style object.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am trying to document my Typer cli app. For my cli, I have a configuration with multiple values.
In my help, I want to show an example of a config file and its values. Typer uses string with markup as its way to generate the help message. Since, I would like to reuse one of my config file used for testing as the example in the help, I was looking for a way to insert markup in the text of the config automatically.
I found a way using a highlighter and a manual string replace to obtain a markup string accepted by Typer. But, I was wondering if there was a better way I missed.
Here is an example code of my solution:
P.S.: I looked at using console, theme and highlighter together, but they all returned markup converted to ANSI code or Style object.
Beta Was this translation helpful? Give feedback.
All reactions