summaryrefslogtreecommitdiff
path: root/examples/paged_output.py
blob: 72efd4f5a52717d3c2d1cf6ee553596fbf50311d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# 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 which shows how to display output using a pager."""

    def __init__(self):
        super().__init__()

    @cmd2.with_argument_list
    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

        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


if __name__ == '__main__':
    app = PagedOutput()
    app.cmdloop()