Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command #5

Merged
merged 2 commits into from
Aug 13, 2017
Merged

Command #5

merged 2 commits into from
Aug 13, 2017

Conversation

silverweed
Copy link
Collaborator

@silverweed silverweed commented Aug 13, 2017

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:

  1. the Command exported variable, like:
package decode
var CmdDecode = &common.Command{
    // ...
}
  1. optionally, the 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 (see decode/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 the version commands were also changed to adapt to this interface.

What I added

I added a common package, containing the definition of a Command plus a convenient FindCommand function which contains the logic that was previously in invokeMain. I factored this logic out since the new help package needed the same exact thing. The new function returns an error instead of setting a success bool variable.

I also added a help package which exports a help 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:

  1. in the file containing your command's entrypoint, declare your flag variables (e.g. flagMyBool);
  2. create an init.go file for your package and define your Command atop of it;
  3. in the init function call CmdMyCommand.Flag.BoolVar(&flagMyBool, "false", "blah", "blah");
  4. when your main will be called, the flags will be ready for use, so just use them normally.

Ideas/critiques/suggestions are welcome :-)

@silverweed
Copy link
Collaborator Author

By the way, I couldn't really test if this change broke something in decode as I don't know how it works, so please review that part very carefully :D

@empijei empijei self-assigned this Aug 13, 2017
@empijei empijei merged commit 0b3958c into master Aug 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants