-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
42 lines (37 loc) · 899 Bytes
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package goshodo
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
)
const cmdName = "goshodo"
// Run the goshodo
func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) error {
log.SetOutput(errStream)
fs := flag.NewFlagSet(
fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision), flag.ContinueOnError)
fs.SetOutput(errStream)
ver := fs.Bool("version", false, "display version")
if err := fs.Parse(argv); err != nil {
return err
}
if *ver {
return printVersion(outStream)
}
argv = fs.Args()
if len(argv) < 1 {
return errors.New("no subcommand specified")
}
cmd, ok := dispatch[argv[0]]
if !ok {
return fmt.Errorf("unknown subcommand: %s", argv[0])
}
return cmd.Run(ctx, argv[1:], outStream, errStream)
}
func printVersion(out io.Writer) error {
_, err := fmt.Fprintf(out, "%s v%s (rev:%s)\n", cmdName, version, revision)
return err
}