Skip to content

Commit d959237

Browse files
committed
refactoring
1 parent 27d97d0 commit d959237

File tree

1 file changed

+64
-17
lines changed
  • implement-shell-tools/ls

1 file changed

+64
-17
lines changed

implement-shell-tools/ls/ls.py

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,73 @@
11
import argparse
22
import os
33

4-
parser = argparse.ArgumentParser(
5-
prog="Implement ls command in Python",
6-
description="List files in a directory"
7-
)
4+
def main():
5+
parser = argparse.ArgumentParser(
6+
prog="Implement ls command in Python",
7+
description="List files in a directory"
8+
)
9+
parser.add_argument("paths", nargs='+', help="Path(s) to process")
10+
parser.add_argument("-a", action="store_true", help="Display all files include hidden files")
11+
parser.add_argument("-1", action="store_true", help="list one file per line")
12+
parser.add_argument("dir", nargs="?", default=".", help="Directory to list, default curent directory")
813

9-
parser.add_argument("-a", action="store_true", help="Display all files include hidden files")
10-
parser.add_argument("-1", action="store_true", help="list one file per line")
11-
parser.add_argument("dir", nargs="?", default=".", help="Directory to list, default curent directory")
14+
args = parser.parse_args()
1215

13-
args = parser.parse_args()
16+
17+
total_lines = total_words = total_chars = 0
18+
outputs = []
1419

15-
files = os.listdir(args.dir)
20+
for path in args.paths:
21+
line_count, word_count, char_count = wc(path)
22+
total_lines += line_count
23+
total_words += word_count
24+
total_chars += char_count
25+
outputs.append(format_output((line_count, word_count, char_count), path, args))
1626

17-
if not args.a:
18-
files = [f for f in files if not f.startswith(".")]
19-
files.sort()
27+
print("\n".join(outputs))
2028

21-
if args.__dict__["1"]:
22-
for f in files:
23-
print(f)
24-
else:
25-
print(' '.join(files))
29+
# Print totals if multiple files
30+
if len(args.paths) > 1:
31+
totals = []
32+
if not (args.l or args.w or args.c):
33+
totals = [str(total_lines), str(total_words), str(total_chars)]
34+
else:
35+
if args.l:
36+
totals.append(str(total_lines))
37+
if args.w:
38+
totals.append(str(total_words))
39+
if args.c:
40+
totals.append(str(total_chars))
41+
totals.append("total")
42+
print(" ".join(totals))
2643

44+
def wc(path):
45+
"""Return (line_count, word_count, char_count) for the given file."""
46+
with open(path, 'r') as f:
47+
content = f.read()
48+
line_count = content.count('\n')
49+
word_count = len(content.split())
50+
char_count = len(content)
51+
return line_count, word_count, char_count
52+
53+
54+
def format_output(counts, path, args):
55+
"""Format output for a single file based on provided flags."""
56+
line_count, word_count, char_count = counts
57+
parts = []
58+
59+
if not (args.l or args.w or args.c):
60+
parts = [str(line_count), str(word_count), str(char_count)]
61+
else:
62+
if args.l:
63+
parts.append(str(line_count))
64+
if args.w:
65+
parts.append(str(word_count))
66+
if args.c:
67+
parts.append(str(char_count))
68+
69+
parts.append(path)
70+
return " ".join(parts)
71+
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)