summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-09 01:03:21 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-09 01:03:21 -0400
commitd582466335d3182a15dc40bf2d8b6737fb158d46 (patch)
tree912c44bae977727cb0faa2d307fe87d1c48495b4 /cmd2/utils.py
parenta95034f9537b498acb09943cde7c323001206937 (diff)
downloadcmd2-git-d582466335d3182a15dc40bf2d8b6737fb158d46.tar.gz
Moved two classes from cmd2.py to utils.py.
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