From 98aac33f0b1ea0c6ec6ffb24b814820b223c210d Mon Sep 17 00:00:00 2001 From: Ewelina Date: Tue, 26 Dec 2017 15:37:41 +0100 Subject: [PATCH] task04 (labs05) done --- labs05/task04.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/labs05/task04.py b/labs05/task04.py index 4dfc78c..fb9f714 100644 --- a/labs05/task04.py +++ b/labs05/task04.py @@ -7,6 +7,7 @@ Zwraca liczbę słów, znaków i linii. """ import sys +import argparse def count_lines(text): @@ -32,7 +33,32 @@ def wc(text): def main(): """ main """ - print(wc(sys.stdin.read())) + #print(wc(sys.stdin.read())) + + parser = argparse.ArgumentParser() + parser.add_argument("--l", help="liczba linii", action = 'store_true') + parser.add_argument("--w", help="liczba slow", action = 'store_true') + parser.add_argument("--c", help="liczba znakow", action = 'store_true') + parser.add_argument("file", help="sciezka do pliku", type = argparse.FileType('r'), nargs = '?') + args = parser.parse_args() + + + if args.file is None: + fh_lines = sys.stdin.read() + else: + fh_lines = args.file.read() + + if args.l: + output = count_lines(fh_lines) + elif args.w: + output = count_words(fh_lines) + elif args.c: + output = count_chars(fh_lines) + elif not (args.l and args.w and args.c): + output = wc(fh_lines) + + print(output) + if __name__ == "__main__":