@@ -1304,7 +1304,7 @@ def _display_matches_pyreadline(self, matches: List[str]) -> None: # pragma: no
1304
1304
1305
1305
# ----- Methods which override stuff in cmd -----
1306
1306
1307
- def complete (self , text , state ) :
1307
+ def complete (self , text : str , state : int ) -> Optional [ str ] :
1308
1308
"""Override of command method which returns the next possible completion for 'text'.
1309
1309
1310
1310
If a command has not been entered, then complete against command list.
@@ -1315,8 +1315,8 @@ def complete(self, text, state):
1315
1315
This completer function is called as complete(text, state), for state in 0, 1, 2, …, until it returns a
1316
1316
non-string value. It should return the next possible completion starting with text.
1317
1317
1318
- :param text: str - the current word that user is typing
1319
- :param state: int - non-negative integer
1318
+ :param text: the current word that user is typing
1319
+ :param state: non-negative integer
1320
1320
"""
1321
1321
import functools
1322
1322
if state == 0 and rl_type != RlType .NONE :
@@ -1532,16 +1532,12 @@ def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int,
1532
1532
1533
1533
return results
1534
1534
1535
- def get_all_commands (self ):
1536
- """
1537
- Returns a list of all commands
1538
- """
1535
+ def get_all_commands (self ) -> List [str ]:
1536
+ """Returns a list of all commands."""
1539
1537
return [cur_name [3 :] for cur_name in self .get_names () if cur_name .startswith ('do_' )]
1540
1538
1541
- def get_visible_commands (self ):
1542
- """
1543
- Returns a list of commands that have not been hidden
1544
- """
1539
+ def get_visible_commands (self ) -> List [str ]:
1540
+ """Returns a list of commands that have not been hidden."""
1545
1541
commands = self .get_all_commands ()
1546
1542
1547
1543
# Remove the hidden commands
@@ -1551,11 +1547,11 @@ def get_visible_commands(self):
1551
1547
1552
1548
return commands
1553
1549
1554
- def get_help_topics (self ):
1550
+ def get_help_topics (self ) -> List [ str ] :
1555
1551
""" Returns a list of help topics """
1556
1552
return [name [5 :] for name in self .get_names () if name .startswith ('help_' )]
1557
1553
1558
- def complete_help (self , text , line , begidx , endidx ) :
1554
+ def complete_help (self , text : str , line : str , begidx : int , endidx : int ) -> List [ str ] :
1559
1555
"""
1560
1556
Override of parent class method to handle tab completing subcommands and not showing hidden commands
1561
1557
Returns a list of possible tab completions
@@ -1599,12 +1595,12 @@ def complete_help(self, text, line, begidx, endidx):
1599
1595
return matches
1600
1596
1601
1597
# noinspection PyUnusedLocal
1602
- def sigint_handler (self , signum , frame ):
1598
+ def sigint_handler (self , signum : int , frame ) -> None :
1603
1599
"""Signal handler for SIGINTs which typically come from Ctrl-C events.
1604
1600
1605
1601
If you need custom SIGINT behavior, then override this function.
1606
1602
1607
- :param signum: int - signal number
1603
+ :param signum: signal number
1608
1604
:param frame
1609
1605
"""
1610
1606
@@ -1617,7 +1613,7 @@ def sigint_handler(self, signum, frame):
1617
1613
# Re-raise a KeyboardInterrupt so other parts of the code can catch it
1618
1614
raise KeyboardInterrupt ("Got a keyboard interrupt" )
1619
1615
1620
- def preloop (self ):
1616
+ def preloop (self ) -> None :
1621
1617
""""Hook method executed once when the cmdloop() method is called."""
1622
1618
import signal
1623
1619
# Register a default SIGINT signal handler for Ctrl+C
@@ -1626,8 +1622,8 @@ def preloop(self):
1626
1622
def precmd (self , statement : Statement ) -> Statement :
1627
1623
"""Hook method executed just before the command is processed by ``onecmd()`` and after adding it to the history.
1628
1624
1629
- :param statement: Statement - subclass of str which also contains the parsed input
1630
- :return: Statement - a potentially modified version of the input Statement object
1625
+ :param statement: subclass of str which also contains the parsed input
1626
+ :return: a potentially modified version of the input Statement object
1631
1627
"""
1632
1628
return statement
1633
1629
@@ -1637,8 +1633,8 @@ def precmd(self, statement: Statement) -> Statement:
1637
1633
def preparse (self , raw : str ) -> str :
1638
1634
"""Hook method executed just before the command line is interpreted, but after the input prompt is generated.
1639
1635
1640
- :param raw: str - raw command line input
1641
- :return: str - potentially modified raw command line input
1636
+ :param raw: raw command line input
1637
+ :return: potentially modified raw command line input
1642
1638
"""
1643
1639
return raw
1644
1640
@@ -1679,25 +1675,24 @@ def postparsing_postcmd(self, stop: bool) -> bool:
1679
1675
proc .communicate ()
1680
1676
return stop
1681
1677
1682
- def parseline (self , line ) :
1678
+ def parseline (self , line : str ) -> Tuple [ str , str , str ] :
1683
1679
"""Parse the line into a command name and a string containing the arguments.
1684
1680
1685
1681
NOTE: This is an override of a parent class method. It is only used by other parent class methods.
1686
1682
1687
1683
Different from the parent class method, this ignores self.identchars.
1688
1684
1689
- :param line: str - line read by readline
1690
- :return: (str, str, str) - tuple containing (command, args, line)
1685
+ :param line: line read by readline
1686
+ :return: tuple containing (command, args, line)
1691
1687
"""
1692
-
1693
1688
statement = self .statement_parser .parse_command_only (line )
1694
1689
return statement .command , statement .args , statement .command_and_args
1695
1690
1696
- def onecmd_plus_hooks (self , line ) :
1691
+ def onecmd_plus_hooks (self , line : str ) -> bool :
1697
1692
"""Top-level function called by cmdloop() to handle parsing a line and running the command and all of its hooks.
1698
1693
1699
- :param line: str - line of text read from input
1700
- :return: bool - True if cmdloop() should exit, False otherwise
1694
+ :param line: line of text read from input
1695
+ :return: True if cmdloop() should exit, False otherwise
1701
1696
"""
1702
1697
import datetime
1703
1698
stop = False
@@ -1731,7 +1726,7 @@ def onecmd_plus_hooks(self, line):
1731
1726
finally :
1732
1727
return self .postparsing_postcmd (stop )
1733
1728
1734
- def runcmds_plus_hooks (self , cmds ) :
1729
+ def runcmds_plus_hooks (self , cmds : List [ str ]) -> bool :
1735
1730
"""Convenience method to run multiple commands by onecmd_plus_hooks.
1736
1731
1737
1732
This method adds the given cmds to the command queue and processes the
@@ -1749,8 +1744,8 @@ def runcmds_plus_hooks(self, cmds):
1749
1744
1750
1745
Example: cmd_obj.runcmds_plus_hooks(['load myscript.txt'])
1751
1746
1752
- :param cmds: list - Command strings suitable for onecmd_plus_hooks.
1753
- :return: bool - True implies the entire application should exit.
1747
+ :param cmds: command strings suitable for onecmd_plus_hooks.
1748
+ :return: True implies the entire application should exit.
1754
1749
1755
1750
"""
1756
1751
stop = False
@@ -1773,7 +1768,7 @@ def runcmds_plus_hooks(self, cmds):
1773
1768
# necessary/desired here.
1774
1769
return stop
1775
1770
1776
- def _complete_statement (self , line ) :
1771
+ def _complete_statement (self , line : str ) -> Statement :
1777
1772
"""Keep accepting lines of input until the command is complete.
1778
1773
1779
1774
There is some pretty hacky code here to handle some quirks of
@@ -1814,10 +1809,10 @@ def _complete_statement(self, line):
1814
1809
raise EmptyStatement ()
1815
1810
return statement
1816
1811
1817
- def _redirect_output (self , statement ) :
1812
+ def _redirect_output (self , statement : Statement ) -> None :
1818
1813
"""Handles output redirection for >, >>, and |.
1819
1814
1820
- :param statement: Statement - a parsed statement from the user
1815
+ :param statement: a parsed statement from the user
1821
1816
"""
1822
1817
import io
1823
1818
import subprocess
@@ -1874,12 +1869,11 @@ def _redirect_output(self, statement):
1874
1869
if statement .output == constants .REDIRECTION_APPEND :
1875
1870
self .poutput (get_paste_buffer ())
1876
1871
1877
- def _restore_output (self , statement ) :
1872
+ def _restore_output (self , statement : Statement ) -> None :
1878
1873
"""Handles restoring state after output redirection as well as
1879
1874
the actual pipe operation if present.
1880
1875
1881
- :param statement: Statement object which contains the parsed
1882
- input from the user
1876
+ :param statement: Statement object which contains the parsed input from the user
1883
1877
"""
1884
1878
# If we have redirected output to a file or the clipboard or piped it to a shell command, then restore state
1885
1879
if self .kept_state is not None :
@@ -1910,25 +1904,25 @@ def _restore_output(self, statement):
1910
1904
1911
1905
self .redirecting = False
1912
1906
1913
- def _func_named (self , arg ) :
1907
+ def _func_named (self , arg : str ) -> str :
1914
1908
"""Gets the method name associated with a given command.
1915
1909
1916
- :param arg: str - command to look up method name which implements it
1917
- :return: str - method name which implements the given command
1910
+ :param arg: command to look up method name which implements it
1911
+ :return: method name which implements the given command
1918
1912
"""
1919
1913
result = None
1920
1914
target = 'do_' + arg
1921
1915
if target in dir (self ):
1922
1916
result = target
1923
1917
return result
1924
1918
1925
- def onecmd (self , statement ) :
1919
+ def onecmd (self , statement : Statement ) -> Optional [ bool ] :
1926
1920
""" This executes the actual do_* method for a command.
1927
1921
1928
1922
If the command provided doesn't exist, then it executes _default() instead.
1929
1923
1930
1924
:param statement: Command - a parsed command from the input stream
1931
- :return: bool - a flag indicating whether the interpretation of commands should stop
1925
+ :return: a flag indicating whether the interpretation of commands should stop
1932
1926
"""
1933
1927
funcname = self ._func_named (statement .command )
1934
1928
if not funcname :
@@ -1946,11 +1940,10 @@ def onecmd(self, statement):
1946
1940
stop = func (statement )
1947
1941
return stop
1948
1942
1949
- def default (self , statement ) :
1943
+ def default (self , statement : Statement ) -> None :
1950
1944
"""Executed when the command given isn't a recognized command implemented by a do_* method.
1951
1945
1952
1946
:param statement: Statement object with parsed input
1953
- :return:
1954
1947
"""
1955
1948
arg = statement .raw
1956
1949
if self .default_to_shell :
@@ -1963,13 +1956,13 @@ def default(self, statement):
1963
1956
self .poutput ('*** Unknown syntax: {}\n ' .format (arg ))
1964
1957
1965
1958
@staticmethod
1966
- def _surround_ansi_escapes (prompt , start = "\x01 " , end = "\x02 " ):
1959
+ def _surround_ansi_escapes (prompt : str , start : str = "\x01 " , end : str = "\x02 " ) -> str :
1967
1960
"""Overcome bug in GNU Readline in relation to calculation of prompt length in presence of ANSI escape codes.
1968
1961
1969
- :param prompt: str - original prompt
1970
- :param start: str - start code to tell GNU Readline about beginning of invisible characters
1971
- :param end: str - end code to tell GNU Readline about end of invisible characters
1972
- :return: str - prompt safe to pass to GNU Readline
1962
+ :param prompt: original prompt
1963
+ :param start: start code to tell GNU Readline about beginning of invisible characters
1964
+ :param end: end code to tell GNU Readline about end of invisible characters
1965
+ :return: prompt safe to pass to GNU Readline
1973
1966
"""
1974
1967
# Windows terminals don't use ANSI escape codes and Windows readline isn't based on GNU Readline
1975
1968
if sys .platform == "win32" :
@@ -1990,9 +1983,8 @@ def _surround_ansi_escapes(prompt, start="\x01", end="\x02"):
1990
1983
1991
1984
return result
1992
1985
1993
- def pseudo_raw_input (self , prompt ):
1994
- """
1995
- began life as a copy of cmd's cmdloop; like raw_input but
1986
+ def pseudo_raw_input (self , prompt : str ) -> str :
1987
+ """Began life as a copy of cmd's cmdloop; like raw_input but
1996
1988
1997
1989
- accounts for changed stdin, stdout
1998
1990
- if input is a pipe (instead of a tty), look at self.echo
@@ -2033,14 +2025,14 @@ def pseudo_raw_input(self, prompt):
2033
2025
line = 'eof'
2034
2026
return line .strip ()
2035
2027
2036
- def _cmdloop (self ):
2028
+ def _cmdloop (self ) -> bool :
2037
2029
"""Repeatedly issue a prompt, accept input, parse an initial prefix
2038
2030
off the received input, and dispatch to action methods, passing them
2039
2031
the remainder of the line as argument.
2040
2032
2041
2033
This serves the same role as cmd.cmdloop().
2042
2034
2043
- :return: bool - True implies the entire application should exit.
2035
+ :return: True implies the entire application should exit.
2044
2036
"""
2045
2037
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
2046
2038
# has been split out so that it can be called separately
@@ -2070,7 +2062,7 @@ def _cmdloop(self):
2070
2062
# Enable tab completion
2071
2063
readline .parse_and_bind (self .completekey + ": complete" )
2072
2064
2073
- stop = None
2065
+ stop = False
2074
2066
try :
2075
2067
while not stop :
2076
2068
if self .cmdqueue :
@@ -2111,7 +2103,7 @@ def _cmdloop(self):
2111
2103
return stop
2112
2104
2113
2105
@with_argument_list
2114
- def do_alias (self , arglist ) :
2106
+ def do_alias (self , arglist : List [ str ]) -> None :
2115
2107
"""Define or display aliases
2116
2108
2117
2109
Usage: Usage: alias [name] | [<name> <value>]
@@ -2167,7 +2159,7 @@ def do_alias(self, arglist):
2167
2159
errmsg = "Aliases can not contain: {}" .format (invalidchars )
2168
2160
self .perror (errmsg , traceback_war = False )
2169
2161
2170
- def complete_alias (self , text , line , begidx , endidx ) :
2162
+ def complete_alias (self , text : str , line : str , begidx : int , endidx : int ) -> List [ str ] :
2171
2163
""" Tab completion for alias """
2172
2164
alias_names = set (self .aliases .keys ())
2173
2165
visible_commands = set (self .get_visible_commands ())
@@ -2180,7 +2172,7 @@ def complete_alias(self, text, line, begidx, endidx):
2180
2172
return self .index_based_complete (text , line , begidx , endidx , index_dict , self .path_complete )
2181
2173
2182
2174
@with_argument_list
2183
- def do_unalias (self , arglist ) :
2175
+ def do_unalias (self , arglist : List [ str ]) -> None :
2184
2176
"""Unsets aliases
2185
2177
2186
2178
Usage: Usage: unalias [-a] name [name ...]
@@ -2191,7 +2183,7 @@ def do_unalias(self, arglist):
2191
2183
-a remove all alias definitions
2192
2184
"""
2193
2185
if not arglist :
2194
- self .do_help ('unalias' )
2186
+ self .do_help ([ 'unalias' ] )
2195
2187
2196
2188
if '-a' in arglist :
2197
2189
self .aliases .clear ()
@@ -2208,12 +2200,12 @@ def do_unalias(self, arglist):
2208
2200
else :
2209
2201
self .perror ("Alias {!r} does not exist" .format (cur_arg ), traceback_war = False )
2210
2202
2211
- def complete_unalias (self , text , line , begidx , endidx ) :
2203
+ def complete_unalias (self , text : str , line : str , begidx : int , endidx : int ) -> List [ str ] :
2212
2204
""" Tab completion for unalias """
2213
2205
return self .basic_complete (text , line , begidx , endidx , self .aliases )
2214
2206
2215
2207
@with_argument_list
2216
- def do_help (self , arglist ) :
2208
+ def do_help (self , arglist : List [ str ]) -> None :
2217
2209
"""List available commands with "help" or detailed help with "help cmd"."""
2218
2210
if not arglist or (len (arglist ) == 1 and arglist [0 ] in ('--verbose' , '-v' )):
2219
2211
verbose = len (arglist ) == 1 and arglist [0 ] in ('--verbose' , '-v' )
@@ -2240,7 +2232,7 @@ def do_help(self, arglist):
2240
2232
# This could be a help topic
2241
2233
cmd .Cmd .do_help (self , arglist [0 ])
2242
2234
2243
- def _help_menu (self , verbose = False ):
2235
+ def _help_menu (self , verbose : bool = False ) -> None :
2244
2236
"""Show a list of commands which help can be displayed for.
2245
2237
"""
2246
2238
# Get a sorted list of help topics
@@ -2283,7 +2275,7 @@ def _help_menu(self, verbose=False):
2283
2275
self .print_topics (self .misc_header , help_topics , 15 , 80 )
2284
2276
self .print_topics (self .undoc_header , cmds_undoc , 15 , 80 )
2285
2277
2286
- def _print_topics (self , header , cmds , verbose ) :
2278
+ def _print_topics (self , header : str , cmds : List [ str ] , verbose : bool ) -> None :
2287
2279
"""Customized version of print_topics that can switch between verbose or traditional output"""
2288
2280
import io
2289
2281
@@ -2354,7 +2346,7 @@ def _print_topics(self, header, cmds, verbose):
2354
2346
command = ''
2355
2347
self .stdout .write ("\n " )
2356
2348
2357
- def do_shortcuts (self , _ ) :
2349
+ def do_shortcuts (self , _ : str ) -> None :
2358
2350
"""Lists shortcuts (aliases) available."""
2359
2351
result = "\n " .join ('%s: %s' % (sc [0 ], sc [1 ]) for sc in sorted (self .shortcuts ))
2360
2352
self .poutput ("Shortcuts for other commands:\n {}\n " .format (result ))
@@ -2722,7 +2714,7 @@ def do_pyscript(self, arglist):
2722
2714
"""
2723
2715
if not arglist :
2724
2716
self .perror ("pyscript command requires at least 1 argument ..." , traceback_war = False )
2725
- self .do_help ('pyscript' )
2717
+ self .do_help ([ 'pyscript' ] )
2726
2718
return
2727
2719
2728
2720
# Get the absolute path of the script
0 commit comments