awalterschulze / goderive

Derives and generates mundane golang functions that you do not want to maintain yourself
Apache License 2.0
1.24k stars 43 forks source link

provide command line info on plugins #29

Open rogpeppe opened 7 years ago

rogpeppe commented 7 years ago

It would be nice to be able to list the available plugins from the command line and find out information on individual plugins, including the expected function signature.

awalterschulze commented 7 years ago

Sounds easy enough to emit a list, but the function signature depends on the input parameters. How would the user provide these on the command line?

rogpeppe commented 7 years ago

I'd just document it like:

The copy plugin generates a function of the form:

      func deriveCopy(dst, src T)

  for some type T, which copies the contents of src into dst.
  T must be a pointer or a map. Its contents are copied according to
  the following rules: .... etc
awalterschulze commented 7 years ago

May I ask why, since there are already godocs and examples available on github. Is this for some type of integration?

rogpeppe commented 7 years ago

I'd prefer not to go to github every time I wonder what plugins are available or when I'm wondering what a particular plugin does (in the same way that I often use "go doc" rather than going online to the Go documentation). Also, I'm not always online.

The help for a given plugin could just copy some of the package doc comment from github.com/awalterschulze/goderive/plugin/$pluginname and print that, I guess, to avoid too much textual duplication.

awalterschulze commented 7 years ago

Interesting.

Ok so then how would that be different than running:

go doc ./plugin/fmap

Or

go doc github.com/awalterschulze/goderive/plugin/fmap
rogpeppe commented 7 years ago

Not that much, apart from the fact that:

Note that the user will almost never be in the goderive root directory, so your first suggestion probably won't work most of the time.

awalterschulze commented 7 years ago

Good points.

Ok so for example fmap I would get a godoc like this

$ go doc github.com/awalterschulze/goderive/plugin/fmap
package fmap // import "github.com/awalterschulze/goderive/plugin/fmap"

Package fmap contains the implementation of the fmap plugin, which generates
the deriveFmap function.

The deriveFmap function applies a given function to each element of a list,
returning a list of results in the same order.

    deriveFmap(func(A) B, []A) []B
    deriveFmap(func(rune) B, string) []B

deriveFmap can also be applied to a function that returns a value and an
error.

    deriveFmap(func(A) B, func() (A, error)) (B, error)
    deriveFmap(func(A) (B, error), func() (A, error)) (func() (B, error), error)
    deriveFmap(func(A), func() (A, error)) error
    deriveFmap(func(A) (B, c, d, ...), func() (A, error)) (func() (B, c, d, ...), error)

deriveFmap will propogate the error and not apply the first function to the
result of the second function, if the second function returns an error.

func New(typesMap derive.TypesMap, p derive.Printer, deps map[string]derive.Dependency) derive.Generator
func NewPlugin() derive.Plugin
awalterschulze commented 7 years ago

But we would prefer

$ goderive doc fmap

The deriveFmap function applies a given function to each element of a list,
returning a list of results in the same order.

    deriveFmap(func(A) B, []A) []B
    deriveFmap(func(rune) B, string) []B

deriveFmap can also be applied to a function that returns a value and an
error.

    deriveFmap(func(A) B, func() (A, error)) (B, error)
    deriveFmap(func(A) (B, error), func() (A, error)) (func() (B, error), error)
    deriveFmap(func(A), func() (A, error)) error
    deriveFmap(func(A) (B, c, d, ...), func() (A, error)) (func() (B, c, d, ...), error)

deriveFmap will propogate the error and not apply the first function to the
result of the second function, if the second function returns an error.
awalterschulze commented 7 years ago

If so then we could generate this doc from the godoc as you suggested and avoid more duplication of documentation. What do you think?

rogpeppe commented 7 years ago

Yes, this looks nice. I think I'd suggest naming the subcommand "help" rather than "doc". Then we could have:

 goderive help (or --help):
     show information on command line flags and the available subcommands
 goderive help fmap
     show information on a particular command.
 goderive list
     show a brief description of each plugin (one line for each so it's greppable)

This has a potential ambiguity issue if the stdlib ever adds "help" or "list" packages, but I think we can be safe that even if they do, noone will actually want to run goderive on them :-)

awalterschulze commented 7 years ago

I think list should also take into account the pluginprefixes and print out a list of plugin name : prefix : first line of goderive help

fmap : deriveFmap : The deriveFmap function applies a given function to each element of a list, returning a list of results in the same order.

What do you think?

rogpeppe commented 7 years ago

Perhaps. That seems a little verbose though. I suspect it might be nicer if each plugin provided a single line summary:

 fmap: map a function onto a list

It might be nice to be able to get list to show the function signatures too, similarly to how the godoc page for a package shows the function signatures at the top.

awalterschulze commented 7 years ago

Is this acceptable?

goderive --list
    show a brief description of each plugin (one line for each so it's greppable
goderive --doc=fmap
    show information on a particular command.

where doc is the print out we discussed before. and --list becomes

$ goderive --list
...
equal: deriveEqual: checks for the equality of two values
fmap : deriveFmap : map a function onto a list.
...

So that we can also have:

$ goderive --list --pluginprefix="fmap=m"
...
equal: deriveEqual: checks for the equality of two values
fmap : m : map a function onto a list.
...
saltbo commented 2 years ago

Any update for goderive --list?

awalterschulze commented 2 years ago

No, but from the discussion I think the output format is decided. Now we just need someone to implement it in such a way that the list is extendible with new plugins.

Looking for volunteers.