Skip to content

Commit f578699

Browse files
committed
Restore previous behaivor for cmd2.Cmd.read_input
Changes for read_input: - Put default value for completion_mode back to NONE - Restored docstring to full detail - Restored history behavior when mode and history are both NONE
1 parent 1da9708 commit f578699

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

cmd2/cmd2.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,7 +3264,7 @@ def read_input(
32643264
prompt: str = '',
32653265
*,
32663266
history: list[str] | None = None,
3267-
completion_mode: utils.CompletionMode = utils.CompletionMode.COMMANDS,
3267+
completion_mode: utils.CompletionMode = utils.CompletionMode.NONE,
32683268
preserve_quotes: bool = False,
32693269
choices: Iterable[Any] | None = None,
32703270
choices_provider: ChoicesProviderFunc | None = None,
@@ -3273,22 +3273,40 @@ def read_input(
32733273
) -> str:
32743274
"""Read input from appropriate stdin value.
32753275
3276-
:param prompt: prompt to display
3277-
:param history: optional list of history items to use for this input
3278-
:param completion_mode: type of tab completion to perform
3279-
:param preserve_quotes: if True, quotes are preserved in the completion items
3280-
:param choices: optional list of choices to tab complete
3281-
:param choices_provider: optional function that provides choices
3282-
:param completer: optional completer function
3283-
:param parser: optional argparse parser
3284-
:return: the line read from input
3276+
Also supports tab completion and up-arrow history while input is being entered.
3277+
3278+
:param prompt: prompt to display to user
3279+
:param history: optional list of strings to use for up-arrow history. If completion_mode is
3280+
CompletionMode.COMMANDS and this is None, then cmd2's command list history will
3281+
be used. The passed in history will not be edited. It is the caller's responsibility
3282+
to add the returned input to history if desired. Defaults to None.
3283+
:param completion_mode: tells what type of tab completion to support. Tab completion only works when
3284+
self.use_rawinput is True and sys.stdin is a terminal. Defaults to
3285+
CompletionMode.NONE.
3286+
The following optional settings apply when completion_mode is CompletionMode.CUSTOM:
3287+
:param preserve_quotes: if True, then quoted tokens will keep their quotes when processed by
3288+
ArgparseCompleter. This is helpful in cases when you're tab completing
3289+
flag-like tokens (e.g. -o, --option) and you don't want them to be
3290+
treated as argparse flags when quoted. Set this to True if you plan
3291+
on passing the string to argparse with the tokens still quoted.
3292+
A maximum of one of these should be provided:
3293+
:param choices: iterable of accepted values for single argument
3294+
:param choices_provider: function that provides choices for single argument
3295+
:param completer: tab completion function that provides choices for single argument
3296+
:param parser: an argument parser which supports the tab completion of multiple arguments
3297+
:return: the line read from stdin with all trailing new lines removed
3298+
:raises Exception: any exceptions raised by prompt()
32853299
"""
32863300
self._reset_completion_defaults()
32873301
if self.use_rawinput and self.stdin.isatty():
32883302
# Determine completer
32893303
completer_to_use: Completer
32903304
if completion_mode == utils.CompletionMode.NONE:
32913305
completer_to_use = DummyCompleter()
3306+
3307+
# No up-arrow history when CompletionMode.NONE and history is None
3308+
if history is None:
3309+
history = []
32923310
elif completion_mode == utils.CompletionMode.COMMANDS:
32933311
completer_to_use = self.completer
32943312
else:
@@ -3335,6 +3353,7 @@ def get_prompt() -> ANSI | str:
33353353
bottom_toolbar=self._bottom_toolbar if self.include_bottom_toolbar else None,
33363354
)
33373355

3356+
# history is None
33383357
return self.session.prompt(
33393358
prompt_to_use,
33403359
completer=completer_to_use,

0 commit comments

Comments
 (0)