Closed shailendrasingh117 closed 1 year ago
import click # Define the commands that can be parsed COMMANDS = { "create": {"args": ["path", "data"], "kwargs": ["ephemeral", "sequence"]}, "get_data": {"args": ["path"], "kwargs": []}, "set_data": {"args": ["path", "data"], "kwargs": []}, } # Define the mapping between user inputs and Python types ARGS_MAP = { "path": str, "data": bytes, "ephemeral": bool, "sequence": bool, } # Define the CLI command for the parser @click.command() @click.argument("command")` @click.argument("args", nargs=-1) def cli(command, args): if command not in COMMANDS: click.echo(f"Invalid command: {command}") return cmd_info = COMMANDS[command] kwargs = {} i = 0 for arg in cmd_info["args"]: if i >= len(args): click.echo(f"Missing argument: {arg}") return kwargs[arg] = ARGS_MAP[arg](args[i]) i += 1 for arg in cmd_info["kwargs"]: kwargs[arg] = False for arg in args[i:]: if arg.startswith("-"): if arg[1:] not in cmd_info["kwargs"]: click.echo(f"Invalid flag: {arg}") return kwargs[arg[1:]] = True else: click.echo(f"Invalid argument: {arg}") return if command == "create": create_node(**kwargs) elif command == "get_data": get_data(**kwargs) elif command == "set_data": set_data(**kwargs) else: click.echo(f"Unknown command: {command}")