Python Forum
i want to make a cli command that ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i want to make a cli command that ...
#3
import sys


def get_buffer_size():
    buffer_size = 1
    if len(sys.argv) > 1:
        buffer_size = abs(int(sys.argv[1]))
    return buffer_size


def consume_input(buffer_size):
    buffer = []
    head_sent = False
    for line in sys.stdin:
        buffer.append(line)
        if len(buffer) > buffer_size:
            if not head_sent:
                head_sent = True
                for line in buffer:
                    print(line, end="")
                buffer = []
            else:
                buffer.pop(0)

    if head_sent and buffer:
        print("-" * 78)
    for line in buffer:
        print(line, end="")


if __name__ == "__main__":
    try:
        buffer_size = get_buffer_size()
        consume_input(buffer_size)
    except ValueError as err:
        print("Invalid buffer size.  Any argument to this script should be a number.")
Output:
E:\Projects\etc>pip help | headtail.py Usage: ------------------------------------------------------------------------------ download. Implied with --no-index. E:\Projects\etc>pip help | headtail.py -4 Usage: pip <command> [options] Commands: ------------------------------------------------------------------------------ --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
Reply


Messages In This Thread
i want to make a cli command that ... - by Skaperen - Sep-28-2017, 03:44 AM
RE: i want to make a cli command that ... - by nilamo - Sep-28-2017, 06:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  i need to make a command that ... Skaperen 5 2,939 Nov-30-2019, 09:20 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020