codegangsta/cli — pull request #23 by carlosdp (2013-07-23 04:49:56+00:00)
Title: Make Context argument optional

This is a (very hacky) implementation for #10 . I am relatively new to Go, but this was an interesting problem, so I wanted to find out if it was possible. Again, this is probably a horrible abomination, but it works.

## How it works
This PR makes the `Action` parameter it's own interface, with two implementing struct types: `ContextAction` and `PlainAction`. When passing in a `func` to `app.SetAction` or `command.SetAction`, they as `ParseAction` for an Action for the field. `ParseAction` prints the `func` to a string using `Sprintf` and then performs a simple RegEx to check if the function has any arguments (currently, it matches any non-whitespace character and does not count arguments, only checks if there is any at all). If it has, it returns a `ContextAction` with the `func(*Context)` attached, else it returns a `PlainAction` with a plain old `func()` attached.

## Why this works
For debugging purposes, `func` exports the ability to print to string so that we can see what function an error is referring to and information about it. `Sprintf` calls that ability, giving us that same string we would see in an error output message (looks like `%sfunc(*Context)=0x398c4)`). We can then check the string with Regex for its arguments.

## Should this be used?
Probably not (like 99% sure). There's a reason this is so hacky and that's because it's not really what Go is for. Go is very much about explicit over implicit and a little more on the verbose side versus terse. The current implementation is probably the ideal way to go, but this is here to play with!

## Other Changes
You will notice I also moved the tests to the `cli` package. There's no reason to have the tests in a separate package afaik and that isn't the established convention from what I can see. Also, importing in the tests means that you have to update the package in GOROOT in order to test against the development code. Having the tests in the `cli` package makes development and CI testing easier. (Let me know if there is an explicit reason for separating them as I have seen it several times, just never understood it)

Hope you enjoy this!