|
2 | 2 | from typing import Optional, cast |
3 | 3 |
|
4 | 4 | import click |
5 | | -from noneprompt import Choice, ListPrompt, CancelledError |
| 5 | +from noneprompt import Choice, ListPrompt, ConfirmPrompt, CancelledError |
6 | 6 |
|
7 | 7 | from nb_cli import _, __version__ |
8 | 8 | from nb_cli.handlers import draw_logo |
|
16 | 16 | from .customize import ClickAliasedCommand as ClickAliasedCommand |
17 | 17 |
|
18 | 18 |
|
| 19 | +@click.command # this command only appears in interactive mode. |
| 20 | +@click.pass_context |
| 21 | +@run_async |
| 22 | +async def exit_(ctx: click.Context): |
| 23 | + if await ConfirmPrompt(_("Are you sure to exit NB CLI?"), True).prompt_async( |
| 24 | + style=CLI_DEFAULT_STYLE |
| 25 | + ): |
| 26 | + ctx.exit() |
| 27 | + |
| 28 | + |
| 29 | +@click.command # this command only appears in interactive mode. |
| 30 | +def back_(): |
| 31 | + # nothing to do, just to keep a command to avoid type violation |
| 32 | + return |
| 33 | + |
| 34 | + |
19 | 35 | def _set_global_working_dir( |
20 | 36 | ctx: click.Context, param: click.Option, value: Optional[Path] |
21 | 37 | ): |
@@ -93,21 +109,25 @@ async def cli(ctx: click.Context): |
93 | 109 | sub_cmd, |
94 | 110 | ) |
95 | 111 | ) |
| 112 | + _exit_choice = Choice(_("Exit NB CLI."), exit_) |
| 113 | + choices.append(_exit_choice) |
96 | 114 |
|
97 | 115 | click.secho(draw_logo(), fg="cyan", bold=True) |
98 | 116 | click.echo("\n\b") |
99 | 117 | click.secho(_("Welcome to NoneBot CLI!"), fg="green", bold=True) |
100 | 118 |
|
101 | | - # prompt user to choose |
102 | | - try: |
103 | | - result = await ListPrompt( |
104 | | - _("What do you want to do?"), choices=choices |
105 | | - ).prompt_async(style=CLI_DEFAULT_STYLE) |
106 | | - except CancelledError: |
107 | | - ctx.exit() |
108 | | - |
109 | | - sub_cmd = result.data |
110 | | - await run_sync(ctx.invoke)(sub_cmd) |
| 119 | + while True: |
| 120 | + # prompt user to choose |
| 121 | + try: |
| 122 | + result = await ListPrompt( |
| 123 | + _("What do you want to do?"), choices=choices |
| 124 | + ).prompt_async(style=CLI_DEFAULT_STYLE) |
| 125 | + except CancelledError: |
| 126 | + result = _exit_choice |
| 127 | + |
| 128 | + sub_cmd = result.data |
| 129 | + ctx.params["can_back_to_parent"] = True |
| 130 | + await run_sync(ctx.invoke)(sub_cmd) |
111 | 131 |
|
112 | 132 |
|
113 | 133 | from .commands import ( |
|
0 commit comments