Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses #4. Many ideas were taken from https://github.com/golang/go/blob/master/src/cmd/go.
What I changed
Basically I slightly changed the way that wapty subcommands are organized. Now every package that exports a standalone main must declare a
*common.Command
variable which contains the information needed to run it, the flags it accepts (if any) and the usage description.More specifically, these packages should have a
init.go
file containing:init()
function of that package. I suggest this be in the same file as the exported Command variable as it can be used to initialize the command flagset if needed (seedecode/init.go
for an example).The wapty main entrypoint was changed to adapt to these changes, but the logic remains the same.
Note that the
proxy
and theversion
commands were also changed to adapt to this interface.What I added
I added a
common
package, containing the definition of aCommand
plus a convenientFindCommand
function which contains the logic that was previously ininvokeMain
. I factored this logic out since the newhelp
package needed the same exact thing. The new function returns anerror
instead of setting asuccess
bool variable.I also added a
help
package which exports ahelp
command that just prints the detailed information about a subcommand (which is different from the-h
of that command).How the flags work now
As I mentioned, every Command contains its flagset, whose parsing is now completely done by the
invokeMain
function. This is convenient as it eliminates repeated flag parsing logic in every command's entrypoint.So, if your command needs some custom flags, do this:
flagMyBool
);init.go
file for your package and define your Command atop of it;init
function callCmdMyCommand.Flag.BoolVar(&flagMyBool, "false", "blah", "blah")
;Ideas/critiques/suggestions are welcome :-)