92
92
# BrokenPipeError is only in Python 3. Use IOError for Python 2.
93
93
if six .PY3 :
94
94
BROKEN_PIPE_ERROR = BrokenPipeError
95
+
96
+ # redirect_stdout and redirect_stderr weren't added to contextlib until Python 3.4
97
+ from contextlib import redirect_stdout , redirect_stderr
95
98
else :
96
99
BROKEN_PIPE_ERROR = IOError
100
+ from contextlib2 import redirect_stdout , redirect_stderr
97
101
98
102
# On some systems, pyperclip will import gtk for its clipboard functionality.
99
103
# The following code is a workaround for gtk interfering with printing from a background
@@ -668,6 +672,9 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, use_ipython=False
668
672
# Used when piping command output to a shell command
669
673
self .pipe_proc = None
670
674
675
+ # Used by complete() for readline tab completion
676
+ self .completion_matches = []
677
+
671
678
# ----- Methods related to presenting output to the user -----
672
679
673
680
@property
@@ -771,13 +778,14 @@ def completenames(self, text, line, begidx, endidx):
771
778
772
779
return cmd_completion
773
780
781
+ # noinspection PyUnusedLocal
774
782
def complete_subcommand (self , text , line , begidx , endidx ):
775
783
"""Readline tab-completion method for completing argparse sub-command names."""
776
- cmd , args , foo = self .parseline (line )
784
+ command , args , foo = self .parseline (line )
777
785
arglist = args .split ()
778
786
779
- if len (arglist ) <= 1 and cmd + ' ' + args == line :
780
- funcname = self ._func_named (cmd )
787
+ if len (arglist ) <= 1 and command + ' ' + args == line :
788
+ funcname = self ._func_named (command )
781
789
if funcname :
782
790
# Check to see if this function was decorated with an argparse ArgumentParser
783
791
func = getattr (self , funcname )
@@ -799,7 +807,7 @@ def complete_subcommand(self, text, line, begidx, endidx):
799
807
return []
800
808
801
809
def complete (self , text , state ):
802
- """Override of cmd method which returns the next possible completion for 'text'.
810
+ """Override of command method which returns the next possible completion for 'text'.
803
811
804
812
If a command has not been entered, then complete against command list.
805
813
Otherwise try to call complete_<command> to get list of completions.
@@ -819,17 +827,17 @@ def complete(self, text, state):
819
827
stripped = len (origline ) - len (line )
820
828
begidx = readline .get_begidx () - stripped
821
829
endidx = readline .get_endidx () - stripped
822
- if begidx > 0 :
823
- cmd , args , foo = self .parseline (line )
824
- if cmd == '' :
830
+ if begidx > 0 :
831
+ command , args , foo = self .parseline (line )
832
+ if command == '' :
825
833
compfunc = self .completedefault
826
834
else :
827
835
arglist = args .split ()
828
836
829
837
compfunc = None
830
838
# If the user has entered no more than a single argument after the command name
831
- if len (arglist ) <= 1 and cmd + ' ' + args == line :
832
- funcname = self ._func_named (cmd )
839
+ if len (arglist ) <= 1 and command + ' ' + args == line :
840
+ funcname = self ._func_named (command )
833
841
if funcname :
834
842
# Check to see if this function was decorated with an argparse ArgumentParser
835
843
func = getattr (self , funcname )
@@ -842,7 +850,7 @@ def complete(self, text, state):
842
850
if compfunc is None :
843
851
# This command either doesn't have sub-commands or the user is past the point of entering one
844
852
try :
845
- compfunc = getattr (self , 'complete_' + cmd )
853
+ compfunc = getattr (self , 'complete_' + command )
846
854
except AttributeError :
847
855
compfunc = self .completedefault
848
856
else :
@@ -1319,7 +1327,11 @@ def do_help(self, arglist):
1319
1327
# Function has an argparser, so get help based on all the arguments in case there are sub-commands
1320
1328
new_arglist = arglist [1 :]
1321
1329
new_arglist .append ('-h' )
1322
- func (new_arglist )
1330
+
1331
+ # Temporarily redirect all argparse output to both sys.stdout and sys.stderr to self.stdout
1332
+ with redirect_stdout (self .stdout ):
1333
+ with redirect_stderr (self .stdout ):
1334
+ func (new_arglist )
1323
1335
else :
1324
1336
# No special behavior needed, delegate to cmd base class do_help()
1325
1337
cmd .Cmd .do_help (self , funcname [3 :])
0 commit comments