#!/usr/bin/env python
# Exercise 6
# find command

import sys
import argparse
from pathlib import Path


def list(args):
    if not args["path"].exists():
        print(f"find: ‘{args['path']}’: No existe el fichero o el directorio")
        sys.exit(1)
    for file in sorted(args["path"].glob(get_query(args))):
        if args["exec"]:
            ...
        else:
            print(file.relative_to("."))


def get_query(args):
    if args["type"] is not None:
        if args["type"] == "f":
            return "**/*.*"
        elif args["type"] == "d":
            return "**"
    else:
        return f"**/{args['name']}"


parser = argparse.ArgumentParser()
parser.add_argument("-name", default="**/*", type=str)
parser.add_argument("-type", choices=["f", "d"])
parser.add_argument("-print", default=True, action="store_true")
parser.add_argument("-exec", action="extend", nargs="+")

if len(sys.argv) > 1:
    path = Path(sys.argv[1])
    del sys.argv[1]
    args = parser.parse_args().__dict__
    args["path"] = path
else:
    args = {"path": Path("."), "name": "*", "type": None, "exec": None}

list(args)
