MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / do_history

Method do_history

cmd2/cmd2.py:5195–5279  ·  view source on GitHub ↗

View, run, edit, save, or clear previously entered commands. :return: True if running of commands should stop

(self, args: argparse.Namespace)

Source from the content-addressed store, hash-verified

5193
5194 @with_argparser(_build_history_parser)
5195 def do_history(self, args: argparse.Namespace) -> bool | None:
5196 """View, run, edit, save, or clear previously entered commands.
5197
5198 :return: True if running of commands should stop
5199 """
5200 self.last_result = False
5201
5202 # -v must be used alone with no other options
5203 if args.verbose: # noqa: SIM102
5204 if args.clear or args.edit or args.output_file or args.run or args.expanded or args.script:
5205 self.poutput("-v cannot be used with any other options")
5206 return None
5207
5208 # -s and -x can only be used if none of these options are present: [-c -r -e -o -t]
5209 if (args.script or args.expanded) and (args.clear or args.edit or args.output_file or args.run):
5210 self.poutput("-s and -x cannot be used with -c, -r, -e, or -o")
5211 return None
5212
5213 if args.clear:
5214 self.last_result = True
5215
5216 # Clear command and prompt-toolkit history
5217 self.history.clear()
5218 cast(Cmd2History, self.main_session.history).clear()
5219
5220 if self.persistent_history_file:
5221 try:
5222 os.remove(self.persistent_history_file)
5223 except FileNotFoundError:
5224 pass
5225 except OSError as ex:
5226 self.perror(f"Error removing history file '{self.persistent_history_file}': {ex}")
5227 self.last_result = False
5228 return None
5229
5230 return None
5231
5232 # If an argument was supplied, then retrieve partial contents of the history, otherwise retrieve it all
5233 history = self._get_history(args)
5234
5235 if args.run:
5236 if not args.arg:
5237 self.perror("Cowardly refusing to run all previously entered commands.")
5238 self.perror("If this is what you want to do, specify '1:' as the range of history.")
5239 else:
5240 stop = self.runcmds_plus_hooks(list(history.values()))
5241 self.last_result = True
5242 return stop
5243 elif args.edit:
5244 fd, fname = tempfile.mkstemp(suffix=".txt", text=True)
5245 fobj: TextIO
5246 with os.fdopen(fd, "w") as fobj:
5247 for command in history.values():
5248 if command.statement.multiline_command:
5249 fobj.write(f"{command.expanded}\n")
5250 else:
5251 fobj.write(f"{command.raw}\n")
5252 try:

Callers

nothing calls this directly

Calls 11

poutputMethod · 0.95
perrorMethod · 0.95
_get_historyMethod · 0.95
runcmds_plus_hooksMethod · 0.95
run_editorMethod · 0.95
do_run_scriptMethod · 0.95
pfeedbackMethod · 0.95
removeMethod · 0.80
prMethod · 0.80
clearMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected