Skip to content

Commit 94e3a33

Browse files
committed
feat: implement ls in python with -a && -1 flags
1 parent 84c8f72 commit 94e3a33

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+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
parser = argparse.ArgumentParser(
8+
prog='ls',
9+
description='CLI tool to list contents of a directory'
10+
)
11+
12+
parser.add_argument('-1', '--one', action='store_true', help='Force output to be entry per line')
13+
parser.add_argument('-a','--all', action='store_true', help='Include hidden files')
14+
parser.add_argument('path', nargs='?', default='.', help='Directories to list')
15+
16+
args = parser.parse_args()
17+
18+
try:
19+
entries = os.listdir(args.path)
20+
# print(entries)
21+
if not args.all:
22+
entries = [entry for entry in entries if not entry.startswith('.')]
23+
entries.sort(key=str.lower)
24+
# print(entries)
25+
26+
if args.one:
27+
for entry in entries:
28+
print(entry)
29+
30+
else:
31+
print(f" ".join(entries))
32+
33+
except FileNotFoundError:
34+
print(f"ls: No such file or directory ")

0 commit comments

Comments
 (0)