Skip to content

Commit d5615fc

Browse files
committed
Init
0 parents  commit d5615fc

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gh

help.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"text/template"
8+
)
9+
10+
var cmdVersion = &Command{
11+
Run: runVersion,
12+
Usage: "version",
13+
Short: "show gh version",
14+
Long: `Version shows the gh client version showstring.`,
15+
}
16+
17+
func runVersion(cmd *Command, args []string) {
18+
fmt.Println(Version)
19+
}
20+
21+
var cmdHelp = &Command{
22+
Usage: "help [command]",
23+
Short: "show help",
24+
Long: `Help shows usage for a command.`,
25+
}
26+
27+
func init() {
28+
cmdHelp.Run = runHelp // break init loop
29+
}
30+
31+
func runHelp(cmd *Command, args []string) {
32+
if len(args) == 0 {
33+
printUsage()
34+
return // not os.Exit(2); success
35+
}
36+
if len(args) != 1 {
37+
log.Fatal("too many arguments")
38+
}
39+
40+
for _, cmd := range commands {
41+
if cmd.Name() == args[0] {
42+
cmd.printUsage()
43+
return
44+
}
45+
}
46+
47+
fmt.Fprintf(os.Stderr, "Unknown help topic: %q. Run 'acldt helpp'.\n", args[0])
48+
os.Exit(2)
49+
}
50+
51+
var usageTemplate = template.Must(template.New("usage").Parse(`Usage: gh [command] [options] [arguments]
52+
53+
Supported commands are:
54+
{{range .Commands}}{{if .Runnable}}{{if .ShowUsage}}
55+
{{.Name | printf "%-8s"}} {{.Short}}{{end}}{{end}}{{end}}
56+
57+
See 'gh help [command]' for more information about a command.
58+
`))
59+
60+
func printUsage() {
61+
usageTemplate.Execute(os.Stdout, struct {
62+
Commands []*Command
63+
}{
64+
commands,
65+
})
66+
}
67+
68+
func usage() {
69+
printUsage()
70+
os.Exit(2)
71+
}

main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
type Command struct {
11+
Run func(cmd *Command, args []string)
12+
Flag flag.FlagSet
13+
14+
Usage string
15+
Short string
16+
Long string
17+
}
18+
19+
func (c *Command) printUsage() {
20+
if c.Runnable() {
21+
fmt.Printf("Usage: gh %s\n\n", c.Usage)
22+
}
23+
fmt.Println(strings.Trim(c.Long, "\n"))
24+
}
25+
26+
func (c *Command) Name() string {
27+
name := c.Usage
28+
i := strings.Index(name, " ")
29+
if i >= 0 {
30+
name = name[:i]
31+
}
32+
return name
33+
}
34+
35+
func (c *Command) Runnable() bool {
36+
return c.Run != nil
37+
}
38+
39+
var commands = []*Command{}
40+
41+
func main() {
42+
args := os.Args[1:]
43+
if len(args) < 1 {
44+
usage()
45+
}
46+
47+
for _, cmd := range commands {
48+
if cmd.Name() == args[0] && cmd.Run != nil {
49+
cmd.Flag.Usage = func() {
50+
cmd.printUsage()
51+
}
52+
if err := cmd.Flag.Parse(args[1:]); err != nil {
53+
os.Exit(2)
54+
}
55+
cmd.Run(cmd, cmd.Flag.Args())
56+
return
57+
}
58+
}
59+
60+
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", args[0])
61+
usage()
62+
}

version.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
const Version = "dev"

0 commit comments

Comments
 (0)