Skip to content

Commit 62b3a4b

Browse files
committed
Fix total line behavior to match wc for flagged multi-file input
Print totals when multiple files are provided, even if -l, -w, or -c flags are used.This matches wc real behaviour.
1 parent ebd5f71 commit 62b3a4b

File tree

1 file changed

+5
-8
lines changed
  • implement-shell-tools/wc

1 file changed

+5
-8
lines changed

implement-shell-tools/wc/wc.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def main():
2424
totals= {"lines": 0, "words": 0, "chars": 0}
2525

2626
no_flags = not args.l and not args.w and not args.c
27-
width = 7
27+
width = 8
2828

2929
def format_output(counts, label):
3030
if no_flags:
31-
return f"{counts['lines']:> {width}}{counts['words']:>{width}}{counts['chars']:>{width}} {label}"
31+
return f"{counts['lines']:>{width}}{counts['words']:>{width}}{counts['chars']:>{width}} {label}"
3232

3333
parts = []
3434
if args.l:
@@ -46,7 +46,7 @@ def format_output(counts, label):
4646
with open(file_path, "rb") as f:
4747
data = f.read()
4848
except OSError as err:
49-
print(f"wc: cannot read file'{file_path}': {err}", file=sys.stderr)
49+
print(f"wc: cannot read file' {file_path} ': {err}", file=sys.stderr)
5050
had_error = True
5151
continue
5252

@@ -67,12 +67,9 @@ def format_output(counts, label):
6767
print(format_output(counts, file_path))
6868

6969
if len(args.paths) > 1:
70-
no_flags = not args.l and not args.w and not args.c
70+
print(format_output(totals, "total"))
7171

72-
if no_flags:
73-
print(format_output(totals, "total"))
74-
75-
sys.exit(1 if had_error else 0)
72+
sys.exit(1 if had_error else 0)
7673

7774
if __name__ == "__main__":
7875
main()

0 commit comments

Comments
 (0)