diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/paged_output.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/examples/paged_output.py b/examples/paged_output.py index c56dcb89..72efd4f5 100755 --- a/examples/paged_output.py +++ b/examples/paged_output.py @@ -2,26 +2,35 @@ # coding=utf-8 """A simple example demonstrating the using paged output via the ppaged() method. """ +import os +from typing import List import cmd2 class PagedOutput(cmd2.Cmd): - """ Example cmd2 application where we create commands that just print the arguments they are called with.""" + """ Example cmd2 application which shows how to display output using a pager.""" def __init__(self): super().__init__() @cmd2.with_argument_list - def do_page_file(self, args): - """Read in a text file and display its output in a pager.""" + def do_page_file(self, args: List[str]): + """Read in a text file and display its output in a pager. + + Usage: page_file <file_path> + """ if not args: self.perror('page_file requires a path to a file as an argument', traceback_war=False) return - with open(args[0], 'r') as f: - text = f.read() - self.ppaged(text) + filename = os.path.expanduser(args[0]) + try: + with open(filename, 'r') as f: + text = f.read() + self.ppaged(text) + except FileNotFoundError as ex: + self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False) complete_page_file = cmd2.Cmd.path_complete |