summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-09 03:06:21 -0400
committerGitHub <noreply@github.com>2020-09-09 03:06:21 -0400
commitc50def1eff00f7f44adcb911d215ace65d16aa8a (patch)
tree62575c664a651027cea415cea574454d1e4637ca /cmd2/utils.py
parenta975432ea87b8bde7d879f6e0974dcaffedc5f78 (diff)
parent1a2095e5c373430e5aa4bda2ee24f65f4527a002 (diff)
downloadcmd2-git-c50def1eff00f7f44adcb911d215ace65d16aa8a.tar.gz
Merge pull request #991 from python-cmd2/read_input
Read input enhancements
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py39
1 files changed, 37 insertions, 2 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index c5874d6e..b89d57bb 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -1,8 +1,10 @@
# coding=utf-8
"""Shared utility functions"""
+import argparse
import collections
import collections.abc as collections_abc
+import enum
import functools
import glob
import inspect
@@ -12,7 +14,6 @@ import subprocess
import sys
import threading
import unicodedata
-from enum import Enum
from typing import Any, Callable, Dict, Iterable, List, Optional, TextIO, Union
from . import constants
@@ -651,7 +652,7 @@ class RedirectionSavedState:
self.saved_redirecting = saved_redirecting
-class TextAlignment(Enum):
+class TextAlignment(enum.Enum):
"""Horizontal text alignment"""
LEFT = 1
CENTER = 2
@@ -1017,3 +1018,37 @@ def get_defining_class(meth):
if isinstance(cls, type):
return cls
return getattr(meth, '__objclass__', None) # handle special descriptor objects
+
+
+class CompletionMode(enum.Enum):
+ """Enum for what type of tab completion to perform in cmd2.Cmd.read_input()"""
+ # Tab completion will be disabled during read_input() call
+ # Use of custom up-arrow history supported
+ NONE = 1
+
+ # read_input() will tab complete cmd2 commands and their arguments
+ # cmd2's command line history will be used for up arrow if history is not provided.
+ # Otherwise use of custom up-arrow history supported.
+ COMMANDS = 2
+
+ # read_input() will tab complete based on one of its following parameters:
+ # choices, choices_provider, completer, parser
+ # Use of custom up-arrow history supported
+ CUSTOM = 3
+
+
+class CustomCompletionSettings:
+ """Used by cmd2.Cmd.complete() to tab complete strings other than command arguments"""
+ def __init__(self, parser: argparse.ArgumentParser, *, preserve_quotes: bool = False):
+ """
+ Initializer
+
+ :param parser: arg parser defining format of string being tab completed
+ :param preserve_quotes: if True, then quoted tokens will keep their quotes when processed by
+ ArgparseCompleter. This is helpful in cases when you're tab completing
+ flag-like tokens (e.g. -o, --option) and you don't want them to be
+ treated as argparse flags when quoted. Set this to True if you plan
+ on passing the string to argparse with the tokens still quoted.
+ """
+ self.parser = parser
+ self.preserve_quotes = preserve_quotes