Skip to content
Myr

add currentmodule back in myst form and remove click. additions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by denuoweb · committed by denuoweb · parent 29eb946 · Browse files · 2 files+25 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−19 removed

  • other:+25 added−19 removed

2 files+25 added−19 removed

modified:docs/unicode-support.md+3 added
modified:docs/utils.md+22 added-19 removed
1```{currentmodule} click2```34# Utilities56Besides the functionality that Click provides to interface with argument parsing and handling, it also provides a bunch7of addon functionality that is useful for writing command line utilities.89## Printing to Stdout108The most obvious helper is the {func}`click.echo` function, which in many ways works like the Python `print` statement or11The most obvious helper is the {func}`echo` function, which in many ways works like the Python `print` statement or12function. The main difference is that it works the same in many different terminal environments.1314Example:15
9 unmodified lines
25```python26click.echo(b'\xe2\x98\x83', nl=False)27```2826Last but not least {func}`click.echo` uses click's intelligent internal output streams to stdout and stderr which support29Last but not least {func}`echo` uses click's intelligent internal output streams to stdout and stderr which support30unicode output on the Windows console. This means for as long as you are using `click.echo` you can output unicode31characters (there are some limitations on the default font with regards to which characters can be displayed).3233```{versionadded} 6.0
17 unmodified lines
5152```{versionadded} 2.053```5452The {func}`click.echo` function supports ANSI colors and styles. On Windows this uses [colorama].55The {func}`echo` function supports ANSI colors and styles. On Windows this uses [colorama].5657Primarily this means that:5856- Click's {func}`click.echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.57- the {func}`click.echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal59- Click's {func}`echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.60- the {func}`echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal61 API calls. This means that colors will work on Windows the same way they do on other operating systems.6263On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not64required for Click.6563For styling a string, the {func}`click.style` function can be used:66For styling a string, the {func}`style` function can be used:6768```python69import click70
1 unmodified line
72click.echo(click.style('Some more text', bg='blue', fg='white'))73click.echo(click.style('ATTENTION', blink=True, bold=True))74```7573The combination of {func}`click.echo` and {func}`click.style` is also available in a single function called {func}`click.secho`:76The combination of {func}`echo` and {func}`style` is also available in a single function called {func}`secho`:7778```python79click.secho('Hello World!', fg='green')80click.secho('Some more text', bg='blue', fg='white')
2 unmodified lines
8384## Pager Support8586In some situations, you might want to show long texts on the terminal and let a user scroll through it. This can be84achieved by using the {func}`click.echo_via_pager` function which works similarly to the {func}`click.echo` function, but always87achieved by using the {func}`echo_via_pager` function which works similarly to the {func}`echo` function, but always88writes to stdout and, if possible, through a pager.8990Example:91
20 unmodified lines
112113```{versionadded} 2.0114```115113To clear the terminal screen, you can use the {func}`click.clear` function that is provided starting with Click 2.0. It does116To clear the terminal screen, you can use the {func}`clear` function that is provided starting with Click 2.0. It does117what the name suggests: it clears the entire visible screen in a platform-agnostic way:118119```120import click
8 unmodified lines
129Normally, when reading input from the terminal, you would read from standard input. However, this is buffered input and130will not show up until the line has been terminated. In certain circumstances, you might not want to do that and instead131read individual characters as they are being written.132130For this, Click provides the {func}`click.getchar` function which reads a single character from the terminal buffer and133For this, Click provides the {func}`getchar` function which reads a single character from the terminal buffer and134returns it as a Unicode character.135136Note that this function will always read from the terminal, even if stdin is instead a pipe.137
25 unmodified lines
163164Sometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows165where `cmd.exe` will close the window at the end of the command execution by default, instead of waiting.166164In click, this can be accomplished with the {func}`click.pause` function. This function will print a quick message to the167In click, this can be accomplished with the {func}`pause` function. This function will print a quick message to the168terminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP169(no operation instruction) if the script is not run interactively.170171Example:
7 unmodified lines
179180```{versionadded} 2.0181```182180Click supports launching editors automatically through {func}`click.edit`. This is very useful for asking users for multi-line183Click supports launching editors automatically through {func}`edit`. This is very useful for asking users for multi-line184input. It will automatically open the user's defined editor or fall back to a sensible default. If the user closes the185editor without saving, the return value will be `None`, otherwise the entered text.186187Example usage:
22 unmodified lines
210211```{versionadded} 2.0212```213211Click supports launching applications through {func}`click.launch`. This can be used to open the default application214Click supports launching applications through {func}`launch`. This can be used to open the default application215associated with a URL or filetype. This can be used to launch web browsers or picture viewers, for instance. In addition216to this, it can also launch the file manager and automatically select the provided file.217218Example usage:
6 unmodified lines
225## Printing Filenames226227Because filenames might not be Unicode, formatting them can be a bit tricky.228226The way this works with click is through the {func}`click.format_filename` function. It does a best-effort conversion of the229The way this works with click is through the {func}`format_filename` function. It does a best-effort conversion of the230filename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode231string.232233Example:
7 unmodified lines
241For command line utilities, it's very important to get access to input and output streams reliably. Python generally242provides access to these streams through `sys.stdout` and friends, but unfortunately, there are API differences between2432.x and 3.x, especially with regards to how these streams respond to Unicode and binary data.244242Because of this, click provides the {func}`click.get_binary_stream` and {func}`click.get_text_stream` functions, which produce245Because of this, click provides the {func}`get_binary_stream` and {func}`get_text_stream` functions, which produce246consistent results with different Python versions and for a wide variety of terminal configurations.247248The end result is that these functions will always return a functional stream object (except in very odd cases; see249{doc}`/unicode-support`).
17 unmodified lines
267268```{versionadded} 3.0269```270268Starting with Click 3.0 the logic for opening files from the {func}`click.File` type is exposed through the {func}`click.open_file`271Starting with Click 3.0 the logic for opening files from the {func}`File` type is exposed through the {func}`open_file`272function. It can intelligently open stdin/stdout as well as any other file.273274Example:275
18 unmodified lines
294```295296Very often, you want to open a configuration file that belongs to your application. However, different operating systems297store these configuration files in different locations depending on their standards. Click provides a295{func}`click.get_app_dir` function which returns the most appropriate location for per-user config files for your application298{func}`get_app_dir` function which returns the most appropriate location for per-user config files for your application299depending on the OS.300301Example usage:302
17 unmodified lines
320## Showing Progress Bars321322Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some323progress about how long that will take. Click supports simple progress bar rendering for that through the321{func}`click.progressbar` function.324{func}`progressbar` function.325326```{note} If you find that you have requirements beyond what Click's progress bar supports, try using [tqdm].327```328
25 unmodified lines
354 for user in bar:355 modify_the_user(user)356```357355Note that {func}`click.progressbar` updates the bar *after* each iteration of the loop. So code like this will render358Note that {func}`progressbar` updates the bar *after* each iteration of the loop. So code like this will render359correctly:360361```python362import time
31 unmodified lines