Skip to content

Commit fedd7be

Browse files
committed
Refactor ls to use argparse
1 parent b381274 commit fedd7be

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import sys
1+
import argparse
22
import os
33

4-
args = sys.argv[1:]
4+
parser = argparse.ArgumentParser()
5+
parser.add_argument("-a", "--all", action="store_true")
6+
parser.add_argument("-1", dest="one_per_line", action="store_true")
7+
parser.add_argument("path", nargs="?", default=".")
58

6-
show_all = False
7-
one_per_line = False
8-
path = "."
9+
args = parser.parse_args()
910

10-
for arg in args:
11-
if arg == "-a":
12-
show_all = True
13-
elif arg == "-1":
14-
one_per_line = True
15-
else:
16-
path = arg
11+
files = sorted(os.listdir(args.path))
1712

18-
files = sorted(os.listdir(path))
13+
files = [f for f in files if args.all or not f.startswith(".")]
1914

20-
for file in files:
21-
if not show_all and file.startswith("."):
22-
continue
23-
24-
print(file)
15+
if args.one_per_line:
16+
for f in files:
17+
print(f)
18+
else:
19+
print(" ".join(files))

0 commit comments

Comments
 (0)