Skip to content

Commit e0be462

Browse files
committed
feat: implement ls CLI tool
1 parent 8d8af3b commit e0be462

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
from pathlib import Path
3+
4+
parser = argparse.ArgumentParser(
5+
prog="wc",
6+
description="wc implementation",
7+
)
8+
9+
parser.add_argument("path", nargs="?", help="The file to process", default=".")
10+
parser.add_argument("-1", "--one_per_line",action="store_true", help="one file per line")
11+
parser.add_argument("-a", action="store_true", help="Show hidden files")
12+
13+
14+
args = parser.parse_args()
15+
16+
path = Path(args.path) # path is an object represent a location on disk
17+
18+
# else:
19+
# for item in path.iterdir(): # iterdir is a method reads directory contents
20+
# if args.a:
21+
# print(item.name)
22+
# else:
23+
# if not item.name.startswith("."):
24+
# print(item.name) # each item is a Path object
25+
items =[]
26+
for item in sorted(path.iterdir()):
27+
if args.a or not item.name.startswith("."):
28+
items.append(item.name)
29+
30+
if(args.one_per_line):
31+
for name in items:
32+
print(name)
33+
else:
34+
print(" ".join(items))

0 commit comments

Comments
 (0)