help.go raw
1 package podhelp
2
3 import (
4 "fmt"
5 "os"
6 "sort"
7 "strings"
8 "unicode/utf8"
9
10 "github.com/p9c/p9/pod/state"
11 "github.com/p9c/p9/pkg/opts/binary"
12 "github.com/p9c/p9/pkg/opts/duration"
13 "github.com/p9c/p9/pkg/opts/float"
14 "github.com/p9c/p9/pkg/opts/integer"
15 "github.com/p9c/p9/pkg/opts/list"
16 "github.com/p9c/p9/pkg/opts/opt"
17 "github.com/p9c/p9/pkg/opts/text"
18 )
19
20 func HelpFunction(ifc interface{}) error {
21 ps := assertToPodState(ifc)
22 c := ps.Config
23 var o string
24 o += fmt.Sprintf("Parallelcoin Pod All-in-One Suite\n\n")
25 o += fmt.Sprintf("Usage:\n\t%s [options] [commands] [command parameters]\n\n", os.Args[0])
26 o += fmt.Sprintf("Commands:\n")
27 for i := range c.Commands {
28 oo := fmt.Sprintf("\t%s", c.Commands[i].Name)
29 nrunes := utf8.RuneCountInString(oo)
30 o += oo + fmt.Sprintf(strings.Repeat(" ", 9-nrunes)+"%s\n", c.Commands[i].Title)
31 }
32 o += fmt.Sprintf(
33 "\nOptions:\n\tset values on options concatenated against the option keyword or separated with '='\n",
34 )
35 o += fmt.Sprintf("\teg: addcheckpoints=deadbeefcafe,someothercheckpoint AP127.0.0.1:11047\n")
36 o += fmt.Sprintf("\tfor items that take multiple string values, you can repeat the option with further\n")
37 o += fmt.Sprintf("\tinstances of the option or separate the items with (only) commas as the above example\n\n")
38 // items := make(map[string][]opt.Option)
39 descs := make(map[string]string)
40 c.ForEach(func(ifc opt.Option) bool {
41 meta := ifc.GetMetadata()
42 oo := fmt.Sprintf("\t%s %v", meta.Option, meta.Aliases)
43 nrunes := utf8.RuneCountInString(oo)
44 var def string
45 switch ii := ifc.(type) {
46 case *binary.Opt:
47 def = fmt.Sprint(ii.Def)
48 case *list.Opt:
49 def = fmt.Sprint(ii.Def)
50 case *float.Opt:
51 def = fmt.Sprint(ii.Def)
52 case *integer.Opt:
53 def = fmt.Sprint(ii.Def)
54 case *text.Opt:
55 def = fmt.Sprint(ii.Def)
56 case *duration.Opt:
57 def = fmt.Sprint(ii.Def)
58 }
59 descs[meta.Group] += oo + fmt.Sprintf(strings.Repeat(" ", 32-nrunes)+"%s, default: %s\n", meta.Description, def)
60 return true
61 },
62 )
63 var cats []string
64 for i := range descs {
65 cats = append(cats, i)
66 }
67 // I.S(cats)
68 sort.Strings(cats)
69 for i := range cats {
70 if cats[i] != "" {
71 o += "\n" + cats[i] + "\n"
72 }
73 o += descs[cats[i]]
74 }
75 // for i := range cats {
76 // }
77 o += fmt.Sprintf("\nadd the name of the command or option after 'help' or append it after "+
78 "'help' in the commandline to get more detail - eg: %s help upnp\n\n", os.Args[0],
79 )
80 fmt.Fprintf(os.Stderr, o)
81 return nil
82 }
83
84 func assertToPodState(ifc interface{}) (c *state.State) {
85 var ok bool
86 if c, ok = ifc.(*state.State); !ok {
87 panic("wth")
88 }
89 return
90 }
91