Skip to content

Commit 8ee7c0e

Browse files
ls command implemented
1 parent d843352 commit 8ee7c0e

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import argparse
2+
import os
23

34
parser = argparse.ArgumentParser(
45
prog="py-ls",
56
description="A Python implementation of the Unix ls command",
67
)
78

8-
parser.add_argument("-1", action="store_true", help="List one file per line")
9-
parser.add_argument("-a", "--all", action="store_true", help="Include directory entries whose names begin with a dot (.)")
10-
parser.add_argument("path", nargs="+", help="The file path to process")
9+
parser.add_argument("-1", dest="_1", action="store_true", help="List one file per line")
10+
parser.add_argument("-a", "--all", action="store_true", help="Include entries that begin with a dot (.)")
11+
parser.add_argument("path", nargs="?", default=".", help="Directory to list")
1112

1213
args = parser.parse_args()
1314

15+
files = os.listdir(args.path)
16+
17+
if args.all:
18+
files = [".", ".."] + files
19+
else:
20+
files = [file for file in files if not file.startswith(".")]
21+
22+
if args._1:
23+
print("\n".join(files))
24+
else:
25+
print(" ".join(files))

0 commit comments

Comments
 (0)