Skip to content

Commit 0d43179

Browse files
committed
feat: implement ls
1 parent 4b98427 commit 0d43179

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

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

0 commit comments

Comments
 (0)