changeset 4:8bc574052fcb

def cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 31 Mar 2020 12:47:04 +0900
parents e4088b031eba
children a0d23f38344d
files cmd.go cmd_donwload.go go.mod go.sum lectable.go
diffstat 5 files changed, 80 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cmd.go	Tue Mar 31 12:17:52 2020 +0900
+++ b/cmd.go	Tue Mar 31 12:47:04 2020 +0900
@@ -1,9 +1,12 @@
 package lectable
 
-import "io"
+import (
+	"context"
+	"io"
+)
 
 type cmd interface {
 	name() string
 	description() string
-	run(cotnext.Context, []string, io.Writer, io.Writer)
+	run(context.Context, []string, io.Writer, io.Writer) error
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_donwload.go	Tue Mar 31 12:47:04 2020 +0900
@@ -0,0 +1,22 @@
+package lectable
+
+import (
+	"context"
+	"fmt"
+	"io"
+)
+
+type cmdDownload struct{}
+
+func (cd *cmdDownload) name() string {
+	return "download"
+}
+
+func (cd *cmdDownload) description() string {
+	return "donwload html from lecture ids"
+}
+
+func (cd *cmdDownload) run(ctx context.Context, argv []string, outStream, errStream io.Writer) error {
+	fmt.Println("download now!!")
+	return nil
+}
--- a/go.mod	Tue Mar 31 12:17:52 2020 +0900
+++ b/go.mod	Tue Mar 31 12:47:04 2020 +0900
@@ -2,4 +2,7 @@
 
 go 1.14
 
-require github.com/pkg/errors v0.9.1
+require (
+	github.com/pkg/errors v0.9.1
+	golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
+)
--- a/go.sum	Tue Mar 31 12:17:52 2020 +0900
+++ b/go.sum	Tue Mar 31 12:47:04 2020 +0900
@@ -1,1 +1,3 @@
 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
--- a/lectable.go	Tue Mar 31 12:17:52 2020 +0900
+++ b/lectable.go	Tue Mar 31 12:47:04 2020 +0900
@@ -6,16 +6,43 @@
 	"fmt"
 	"io"
 	"log"
+
+	"golang.org/x/xerrors"
 )
 
 const cmdName = "lectable"
 
+var (
+	subCommands = []cmd{
+		&cmdDownload{},
+	}
+	dispatch          = make(map[string]cmd, len(subCommands))
+	maxSubcommandName int
+)
+
+func init() {
+	for _, r := range subCommands {
+		n := r.name()
+		l := len(n)
+		if l > maxSubcommandName {
+			maxSubcommandName = l
+		}
+		dispatch[n] = r
+	}
+}
+
 // Run the lectable
 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)
+	nameAndVer := fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision)
+	fs := flag.NewFlagSet(nameAndVer, flag.ContinueOnError)
 	fs.SetOutput(errStream)
+	fs.Usage = func() {
+		fmt.Fprintf(fs.Output(), "Usage of %s:\n", nameAndVer)
+		fs.PrintDefaults()
+		fmt.Fprintf(fs.Output(), "\nCommands:\n")
+		formatCommands(fs.Output())
+	}
 	ver := fs.Bool("version", false, "display version")
 	if err := fs.Parse(argv); err != nil {
 		return err
@@ -23,10 +50,27 @@
 	if *ver {
 		return printVersion(outStream)
 	}
-	return nil
+
+	argv = fs.Args()
+	if len(argv) < 1 {
+		fs.Usage()
+		return xerrors.New("no subcommand specified")
+	}
+	rnr, ok := dispatch[argv[0]]
+	if !ok {
+		return xerrors.Errorf("unknown subcommand: %s", argv[0])
+	}
+	return rnr.run(context.Background(), 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
 }
+
+func formatCommands(out io.Writer) {
+	format := fmt.Sprintf("    %%-%ds  %%s\n", maxSubcommandName)
+	for _, r := range subCommands {
+		fmt.Fprintf(out, format, r.name(), r.description())
+	}
+}