keleshev / docopt-dispatch

Dispatch from command-line arguments to functions
MIT License
20 stars 2 forks source link

Dispatch on best match #3

Open miso-belica opened 8 years ago

miso-belica commented 8 years ago

Hello, I use docopt and this lib a lot, but I have problems with dispatching mechanism by order. I am forced to reorder my functions every time because I have commands like these:

"""
Usage:
  cli
  cli parse <url>
  cli parse all
"""

@dispatch.on()
def foo(**kwargs):
  pass

@dispatch.on("parse")
def foo(**kwargs):
  pass

@dispatch.on("parse", "all")
def foo(**kwargs):
  pass

I know I can reorder them to be working, but it's kind of trap. Because I always forget about this "feature" and I am debugging why my code is doing something else. And it's not easy task to find the reason :(

IMHO better approach would be to match on best match - function that matches the greatest number of arguments. This way everybody can order functions the way he likes :) What do you think?