diff --git a/exercises/ex12/ls b/exercises/ex12/ls new file mode 100755 index 0000000..2991905 --- /dev/null +++ b/exercises/ex12/ls @@ -0,0 +1,34 @@ +#!/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()) diff --git a/exercises/ex12/rm b/exercises/ex12/rm new file mode 100755 index 0000000..e8b601d --- /dev/null +++ b/exercises/ex12/rm @@ -0,0 +1,12 @@ +#!/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("-r", "--recursive", action="store_true") +args = parser.parse_args() +result = ""