Skip to content

Commit de70108

Browse files
authored
Merge pull request #630 from python-cmd2/pyscript_overhaul
Pyscript overhaul
2 parents d3208c8 + daf5e2b commit de70108

26 files changed

+67
-601
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
## 0.9.11 (TBD, 2019)
22
* Enhancements
33
* Added ``matches_sort_key`` to override the default way tab completion matches are sorted
4-
* Deprecations
5-
* Deprecated support for bash completion since this feature had slow performance. Also it relied on
6-
``AutoCompleter`` which has since developed a dependency on ``cmd2`` methods.
74
* Potentially breaking changes
85
* Made ``cmd2_app`` a positional and required argument of ``AutoCompleter`` since certain functionality now
96
requires that it can't be ``None``.
107
* ``AutoCompleter`` no longer assumes ``CompletionItem`` results are sorted. Therefore you should follow the
118
``cmd2`` convention of setting ``self.matches_sorted`` to True before returning the results if you have already
129
sorted the ``CompletionItem`` list. Otherwise it will be sorted using ``self.matches_sort_key``.
10+
* Removed support for bash completion since this feature had slow performance. Also it relied on
11+
``AutoCompleter`` which has since developed a dependency on ``cmd2`` methods.
12+
* Removed ability to call commands in ``pyscript`` as if they were functions (e.g ``app.help()``) in favor
13+
of only supporting one ``pyscript`` interface. This simplifies future maintenance.
1314

1415
## 0.9.10 (February 22, 2019)
1516
* Bug Fixes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Instructions for implementing each feature follow.
117117
- Syntax for calling `cmd2` commands in a `pyscript` is essentially identical to what they would enter on the command line
118118
- See the [Python](https://cmd2.readthedocs.io/en/latest/freefeatures.html#python) section of the `cmd2` docs for more info
119119
- Also see the [python_scripting.py](https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py)
120-
example in conjunciton with the [conditional.py](https://github.com/python-cmd2/cmd2/blob/master/examples/scripts/conditional.py) script
120+
example in conjunction with the [conditional.py](https://github.com/python-cmd2/cmd2/blob/master/examples/scripts/conditional.py) script
121121

122122
- Parsing commands with `argparse`
123123
- Two decorators provide built-in capability for using `argparse.ArgumentParser` to parse command arguments

cmd2/argparse_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ def __init__(self, *args, **kwargs) -> None:
988988
self._custom_error_message = ''
989989

990990
# Begin cmd2 customization
991-
def set_custom_message(self, custom_message: str='') -> None:
991+
def set_custom_message(self, custom_message: str = '') -> None:
992992
"""
993993
Allows an error message override to the error() function, useful when forcing a
994994
re-parse of arguments with newly required parameters

cmd2/cmd2.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def cat_decorator(func):
176176

177177

178178
def with_argument_list(func: Callable[[Statement], Optional[bool]],
179-
preserve_quotes: bool=False) -> Callable[[List], Optional[bool]]:
179+
preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
180180
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
181181
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
182182
shlex.split().
@@ -196,7 +196,7 @@ def cmd_wrapper(self, cmdline):
196196
return cmd_wrapper
197197

198198

199-
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool=False) -> \
199+
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \
200200
Callable[[argparse.Namespace, List], Optional[bool]]:
201201
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
202202
instance of argparse.ArgumentParser, but also returning unknown args as a list.
@@ -239,7 +239,7 @@ def cmd_wrapper(instance, cmdline):
239239

240240

241241
def with_argparser(argparser: argparse.ArgumentParser,
242-
preserve_quotes: bool=False) -> Callable[[argparse.Namespace], Optional[bool]]:
242+
preserve_quotes: bool = False) -> Callable[[argparse.Namespace], Optional[bool]]:
243243
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
244244
with the given instance of argparse.ArgumentParser.
245245
@@ -365,9 +365,9 @@ class Cmd(cmd.Cmd):
365365
'quiet': "Don't print nonessential feedback",
366366
'timing': 'Report execution times'}
367367

368-
def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_history_file: str='',
369-
persistent_history_length: int=1000, startup_script: Optional[str]=None, use_ipython: bool=False,
370-
transcript_files: Optional[List[str]]=None) -> None:
368+
def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent_history_file: str = '',
369+
persistent_history_length: int = 1000, startup_script: Optional[str] = None, use_ipython: bool = False,
370+
transcript_files: Optional[List[str]] = None) -> None:
371371
"""An easy but powerful framework for writing line-oriented command interpreters, extends Python's cmd package.
372372
373373
:param completekey: (optional) readline name of a completion key, default to Tab
@@ -585,7 +585,7 @@ def decolorized_write(self, fileobj: IO, msg: str) -> None:
585585
msg = utils.strip_ansi(msg)
586586
fileobj.write(msg)
587587

588-
def poutput(self, msg: Any, end: str='\n', color: str='') -> None:
588+
def poutput(self, msg: Any, end: str = '\n', color: str = '') -> None:
589589
"""Smarter self.stdout.write(); color aware and adds newline of not present.
590590
591591
Also handles BrokenPipeError exceptions for when a commands's output has
@@ -613,8 +613,8 @@ def poutput(self, msg: Any, end: str='\n', color: str='') -> None:
613613
if self.broken_pipe_warning:
614614
sys.stderr.write(self.broken_pipe_warning)
615615

616-
def perror(self, err: Union[str, Exception], traceback_war: bool=True, err_color: str=Fore.LIGHTRED_EX,
617-
war_color: str=Fore.LIGHTYELLOW_EX) -> None:
616+
def perror(self, err: Union[str, Exception], traceback_war: bool = True, err_color: str = Fore.LIGHTRED_EX,
617+
war_color: str = Fore.LIGHTYELLOW_EX) -> None:
618618
""" Print error message to sys.stderr and if debug is true, print an exception Traceback if one exists.
619619
620620
:param err: an Exception or error message to print out
@@ -647,7 +647,7 @@ def pfeedback(self, msg: str) -> None:
647647
else:
648648
self.decolorized_write(sys.stderr, "{}\n".format(msg))
649649

650-
def ppaged(self, msg: str, end: str='\n', chop: bool=False) -> None:
650+
def ppaged(self, msg: str, end: str = '\n', chop: bool = False) -> None:
651651
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
652652
653653
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
@@ -929,7 +929,7 @@ def delimiter_complete(self, text: str, line: str, begidx: int, endidx: int, mat
929929

930930
def flag_based_complete(self, text: str, line: str, begidx: int, endidx: int,
931931
flag_dict: Dict[str, Union[Iterable, Callable]],
932-
all_else: Union[None, Iterable, Callable]=None) -> List[str]:
932+
all_else: Union[None, Iterable, Callable] = None) -> List[str]:
933933
"""
934934
Tab completes based on a particular flag preceding the token being completed
935935
:param text: the string prefix we are attempting to match (all returned matches must begin with it)
@@ -1184,7 +1184,7 @@ def get_exes_in_path(starts_with: str) -> List[str]:
11841184
return list(exes_set)
11851185

11861186
def shell_cmd_complete(self, text: str, line: str, begidx: int, endidx: int,
1187-
complete_blank: bool=False) -> List[str]:
1187+
complete_blank: bool = False) -> List[str]:
11881188
"""Performs completion of executables either in a user's path or a given path
11891189
:param text: the string prefix we are attempting to match (all returned matches must begin with it)
11901190
:param line: the current input line with leading whitespace removed
@@ -2610,7 +2610,7 @@ def do_help(self, args: argparse.Namespace) -> None:
26102610
# No special behavior needed, delegate to cmd base class do_help()
26112611
super().do_help(args.command)
26122612

2613-
def _help_menu(self, verbose: bool=False) -> None:
2613+
def _help_menu(self, verbose: bool = False) -> None:
26142614
"""Show a list of commands which help can be displayed for.
26152615
"""
26162616
# Get a sorted list of help topics
@@ -2753,7 +2753,8 @@ def do_quit(self, _: argparse.Namespace) -> bool:
27532753
self._should_quit = True
27542754
return self._STOP_AND_EXIT
27552755

2756-
def select(self, opts: Union[str, List[str], List[Tuple[Any, Optional[str]]]], prompt: str='Your choice? ') -> str:
2756+
def select(self, opts: Union[str, List[str], List[Tuple[Any, Optional[str]]]],
2757+
prompt: str = 'Your choice? ') -> str:
27572758
"""Presents a numbered menu to the user. Modeled after
27582759
the bash shell's SELECT. Returns the item chosen.
27592760
@@ -2809,7 +2810,7 @@ def cmdenvironment(self) -> str:
28092810
Output redirection and pipes allowed: {}"""
28102811
return read_only_settings.format(str(self.terminators), self.allow_cli_args, self.allow_redirection)
28112812

2812-
def show(self, args: argparse.Namespace, parameter: str='') -> None:
2813+
def show(self, args: argparse.Namespace, parameter: str = '') -> None:
28132814
"""Shows current settings of parameters.
28142815
28152816
:param args: argparse parsed arguments from the set command
@@ -3608,7 +3609,7 @@ def set_window_title(self, title: str) -> None: # pragma: no cover
36083609
else:
36093610
raise RuntimeError("another thread holds terminal_lock")
36103611

3611-
def cmdloop(self, intro: Optional[str]=None) -> None:
3612+
def cmdloop(self, intro: Optional[str] = None) -> None:
36123613
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
36133614
36143615
_cmdloop() provides the main loop equivalent to cmd.cmdloop(). This is a wrapper around that which deals with
@@ -3863,7 +3864,7 @@ def append(self, new: str) -> None:
38633864
list.append(self, new)
38643865
new.idx = len(self)
38653866

3866-
def get(self, getme: Optional[Union[int, str]]=None) -> List[HistoryItem]:
3867+
def get(self, getme: Optional[Union[int, str]] = None) -> List[HistoryItem]:
38673868
"""Get an item or items from the History list using 1-based indexing.
38683869
38693870
:param getme: optional item(s) to get (either an integer index or string to search for)

0 commit comments

Comments
 (0)