1
0
forked from tdwojak/Python2017

task04 ok

This commit is contained in:
s45147 2017-12-31 13:37:11 +01:00
parent 14ba0bb0ea
commit e0d6872eda

View File

@ -7,7 +7,7 @@ Zwraca liczbę słów, znaków i linii.
"""
import sys
import argparse
def count_lines(text):
""" return number of lines. """
@ -32,8 +32,31 @@ def wc(text):
def main():
""" main """
print(wc(sys.stdin.read()))
parser = argparse.ArgumentParser()
parser.add_argument("-l", help="-l switch returns number of lines", action='store_true')
parser.add_argument("-w", help="-w switch returns number of words", action='store_true')
parser.add_argument("-c", help="-c switch returns number of characters", action='store_true')
parser.add_argument("filename", help="path to a file to read from", type=argparse.FileType('rt'), nargs='?')
args = parser.parse_args()
if args.filename is None:
readinput = sys.stdin.read()
else:
readinput = args.filename.read()
outputmsg = ""
if args.l:
outputmsg = "Number of typed lines: {}".format(count_lines(readinput))
elif args.w:
outputmsg = "Number of typed words: {}".format(count_words(readinput))
elif args.c:
outputmsg = "Number of typed characters: {}".format(count_chars(readinput))
else:
outputmsg = "Number of typed (lines, words, characters): {}".format(wc(readinput))
print(outputmsg)
if __name__ == "__main__":
main()