Skip to content

Commit be9fc62

Browse files
committed
Import argparse library.
1 parent 403ac52 commit be9fc62

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

  • implement-shell-tools/wc

‎implement-shell-tools/wc/wc.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import sys
22
import os
3+
import argparse
34

4-
count_lines = "-l" in sys.argv
5-
count_words = "-w" in sys.argv
6-
count_bytes = "-c" in sys.argv
5+
parser = argparse.ArgumentParser(
6+
description="Count lines, words, and bytes in files."
7+
)
78

8-
filenames = []
9+
parser.add_argument("-l", action="store_true", help="Count lines")
10+
parser.add_argument("-w", action="store_true", help="Count words")
11+
parser.add_argument("-c", action="store_true", help="Count bytes")
12+
parser.add_argument("filenames", nargs="+")
913

10-
for argument in sys.argv[1:]:
11-
if not argument.startswith("-"):
12-
filenames.append(argument)
14+
args = parser.parse_args()
15+
16+
count_lines = args.l
17+
count_words = args.w
18+
count_bytes = args.c
19+
filenames = args.filenames
1320

1421
def format_counts(line_count, word_count, byte_count):
1522
output = []

0 commit comments

Comments
 (0)