basho / clique

CLI Framework for Erlang
Apache License 2.0
147 stars 49 forks source link

Allow the user to specify wildcards when registering command specs #58

Closed nickelization closed 9 years ago

nickelization commented 9 years ago

This PR contains new code that allow commands to have freeform wildcard fields, which must appear after the command base, but before any flags or key/value pairs. For example, we could reimplement "riak-admin cluster join " using this new functionality by registering a ["riak-admin", "cluster", "join", '*'] command specification.

Prior to these changes, the only args that were passed into the command callbacks were two lists: flags and k/v options. With this PR, we add a third argument to the beginning of the arg list for the callback, which contains the actual strings the user entered for the command. For instance, continuing with the example above, we could implement a callback that looks like this:

cluster_join(["riak-admin", "cluster", "join", NodeName], KeyList, FlagList) ->
    ...

This does, unfortunately, break compatibility with existing command callbacks, since they expect two arguments, not three. But any existing commands can just add _, to the beginnings of their callbacks, since the actual input strings are only interesting when you're using wildcards (or perhaps if you want to overload the same callback for multiple different commands, but that's not anything that would've really been possible prior to these changes, so it's a moot point for backward compatibility). I couldn't really come up with a better way than this, unless we want to do something ugly like run erlang:fun_info and check if the callback takes two or three arguments at callback time, and since we're still pre-1.0, it seems like it shouldn't be a huge deal to break compatibility in a small way.

nickelization commented 9 years ago

@borshop merge