35 lines
891 B
Python
Executable File
35 lines
891 B
Python
Executable File
#!/usr/bin/env python
|
|
# Exercise 12a
|
|
# ls command
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("paths", default=Path().cwd(), nargs="*", type=Path)
|
|
parser.add_argument("-a", "--all", action="store_true")
|
|
args = parser.parse_args()
|
|
result = ""
|
|
|
|
|
|
def get_items(path, with_hidden):
|
|
items = path.glob("*")
|
|
if not with_hidden:
|
|
items = list(filter(lambda i: i.name[0] != ".", items))
|
|
return list(sorted(set(items)))
|
|
|
|
|
|
if isinstance(args.paths, list):
|
|
roots = list(map(lambda p: get_items(p, args.all), args.paths))
|
|
else:
|
|
roots = [get_items(args.paths, args.all)]
|
|
|
|
for items in roots:
|
|
if len(items) > 0:
|
|
root = items[0].parent
|
|
items = "\n".join(list(map(lambda i: i.name, items)))
|
|
result += items if len(roots) == 1 else f"{root}:\n{items}\n\n"
|
|
|
|
if result != "":
|
|
print(result.strip())
|