Debian Codegraph @

docs/source/cookbook/random_tips.rst

raw
1 ===========
2 Random Tips
3 ===========
4 Improve discoverability by occasionally surfacing tips to users during normal CLI usage.
5
6 -----------
7 Basic Usage
8 -----------
9 Use a :ref:`Meta App` to display a random tip after each command invocation.
10 This keeps tip logic in one place rather than repeating it in every command.
11
12 .. code-block:: python
13
14 import random
15 import sys
16 from typing import Annotated
17
18 from cyclopts import App, Parameter
19
20 app = App()
21
22 tips = [
23 "Use 'my-app config --help' to see all configuration options.",
24 "Set the MY_APP_DEBUG=1 environment variable for verbose output.",
25 "Commands can be abbreviated: 'my-app d' matches 'my-app deploy'.",
26 "Suppress tips by setting MY_APP_NO_TIPS=1.",
27 ]
28
29
30 @app.command
31 def build():
32 """Build the project."""
33 print("Building...")
34
35
36 @app.command
37 def deploy():
38 """Deploy the project."""
39 print("Deploying...")
40
41
42 @app.meta.default
43 def launcher(
44 *tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)],
45 no_tips: Annotated[bool, Parameter(env_var="MY_APP_NO_TIPS", negative="")] = False,
46 ):
47 app(tokens)
48 if not no_tips and random.random() < 0.3:
49 print(f"\nšŸ’” Tip: {random.choice(tips)}", file=sys.stderr)
50
51
52 if __name__ == "__main__":
53 app.meta()
54
55 .. code-block:: console
56
57 $ python my-app.py build
58 Building...
59
60 šŸ’” Tip: Set the MY_APP_DEBUG=1 environment variable for verbose output.
61
62 $ MY_APP_NO_TIPS=1 python my-app.py build
63 Building...