Skip to content

Commit 8d8af3b

Browse files
committed
implement wc CLI tool done
1 parent 57914a3 commit 8d8af3b

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
parser.add_argument("-c", action="store_true", help="count characters")
1212
args = parser.parse_args()
1313

14+
total = {
15+
"lines_counter": 0,
16+
"words_counter": 0,
17+
"characters_counter": 0,
18+
}
1419
for path in args.paths:
1520

1621
with open(path, "r") as f:
@@ -20,15 +25,38 @@
2025
words_counter = len(content.split())
2126
characters_counter = len(content)
2227
if not args.l and not args.w and not args.c:
23-
print(f"{line_counter} {words_counter} {characters_counter} {args.path}")
24-
28+
print(f"{line_counter} {words_counter} {characters_counter} {path}")
29+
total["lines_counter"] += line_counter
30+
total["words_counter"] += words_counter
31+
total["characters_counter"] += characters_counter
2532
else:
2633
results = []
2734
if args.l:
2835
results.append(line_counter)
36+
total["lines_counter"] += line_counter
2937
if args.w:
3038
results.append(words_counter)
39+
total["words_counter"] += words_counter
3140
if args.c:
3241
results.append(characters_counter)
42+
total["characters_counter"] += characters_counter
3343
results = " ".join(map(str, results))
3444
print(results, path)
45+
46+
if len(args.paths) > 1:
47+
if not args.l and not args.w and not args.c:
48+
print(
49+
f"{total['lines_counter']} {total['words_counter']} {total['characters_counter']} total"
50+
)
51+
52+
else:
53+
total_with_flags = []
54+
55+
if args.l:
56+
total_with_flags.append(total["lines_counter"])
57+
if args.w:
58+
total_with_flags.append(total["words_counter"])
59+
if args.c:
60+
total_with_flags.append(total["characters_counter"])
61+
print(" ".join(map(str, total_with_flags))," total")
62+

0 commit comments

Comments
 (0)