Skip to content

Commit 62f7818

Browse files
committed
Implement ls command functionality in Python
1 parent fbab9b0 commit 62f7818

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import argparse
2+
import os
3+
4+
parser = argparse.ArgumentParser(
5+
prog="check-for-ls",
6+
description="Implement my own version of ls",
7+
)
8+
9+
parser.add_argument(
10+
"paths",
11+
nargs="*",
12+
help="The file paths to process"
13+
)
14+
15+
parser.add_argument(
16+
"-1",
17+
"--one",
18+
action="store_true",
19+
help="List one file per line"
20+
)
21+
22+
parser.add_argument(
23+
"-a",
24+
action="store_true",
25+
help="Show all files"
26+
)
27+
28+
args = parser.parse_args()
29+
30+
paths = args.paths
31+
32+
if len(paths) == 0:
33+
paths = ["."]
34+
35+
for target in paths:
36+
37+
if os.path.isdir(target):
38+
show_files = os.listdir(target)
39+
40+
if args.a:
41+
show_files = [".", "..", *show_files]
42+
else:
43+
show_files = [
44+
name for name in show_files
45+
if not name.startswith(".")
46+
]
47+
48+
show_files.sort()
49+
50+
else:
51+
show_files = [target]
52+
53+
if args.one:
54+
for file in show_files:
55+
print(file)
56+
else:
57+
print(" ".join(show_files))

0 commit comments

Comments
 (0)