summaryrefslogtreecommitdiff
path: root/examples/paged_output.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-11 20:48:03 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-11 20:48:03 -0400
commit0d4e633e8e0698cecc851af2f8967b6ead0e748b (patch)
tree26f9634a091cd10709415dbb4f1d22d8335d770e /examples/paged_output.py
parent3ae71e80e61a908ad0a0dec84ee6496592176022 (diff)
downloadcmd2-git-0d4e633e8e0698cecc851af2f8967b6ead0e748b.tar.gz
Added pager settable parameter to control the command used to display paged output
Also: - ppaged() now uses the pager settable parameter - By default the pager settable parameter uses the PAGER environment variable if present - If PAGER environment variable is not present, pager gets set to: - "less -SXRF" on POSIX OSes (macOS and Linux) - "more" on Windows
Diffstat (limited to 'examples/paged_output.py')
-rwxr-xr-xexamples/paged_output.py21
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