@@ -34,17 +34,24 @@ class CmdLineApp(cmd2.Cmd):
34
34
35
35
and have them all treated as valid input which prints a list of 10 numbers
36
36
starting with the number 5.
37
+
38
+ We also add a postcommand hook, which updates the shell prompt to show the
39
+ raw contents of the Statement after the postparsing hooks are finished. To
40
+ use this hook, run `(Cmd) set debug True`. All of the above variations of
41
+ the list command should produce the same raw content.
42
+
37
43
"""
38
44
39
45
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
40
46
# default_to_shell = True
41
47
def __init__ (self , * args , ** kwargs ):
42
48
super ().__init__ (* args , ** kwargs )
43
49
44
- # register three hooks
50
+ # register four hooks
45
51
self .register_postparsing_hook (self .add_whitespace_hook )
46
52
self .register_postparsing_hook (self .downcase_hook )
47
53
self .register_postparsing_hook (self .abbrev_hook )
54
+ self .register_postcmd_hook (self .proof_hook )
48
55
49
56
def add_whitespace_hook (self , data : cmd2 .plugin .PostparsingData ) -> cmd2 .plugin .PostparsingData :
50
57
"""A hook to split alphabetic command names immediately followed by a number.
@@ -88,6 +95,12 @@ def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.Postpars
88
95
data .statement = self .statement_parser .parse (raw )
89
96
return data
90
97
98
+ def proof_hook (self , data : cmd2 .plugin .PostcommandData ) -> cmd2 .plugin .PostcommandData :
99
+ """Update the shell prompt with the new raw statement after postparsing hooks are finished"""
100
+ if self .debug :
101
+ self .prompt = f'({ data .statement .raw } )'
102
+ return data
103
+
91
104
@cmd2 .with_argument_list
92
105
def do_list (self , arglist : List [str ]) -> None :
93
106
"""Generate a list of 10 numbers."""
0 commit comments