Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/trygolang/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "flag"

// Args は、プログラム引数の値を持つ構造体です
type Args struct {
// 一度だけ実行するかどうか
OneTime bool
// 実行可能な名前を表示するかどうか
ShowNames bool
}

// NewArgs は、Argsのコンストラクタ関数です
func NewArgs() *Args {
return new(Args)
}

// Parse は、コマンドライン引数を解析しパッケージ変数に格納します
func (a *Args) Parse() {
flag.BoolVar(&a.OneTime, "onetime", false, "run only one time")
flag.BoolVar(&a.ShowNames, "list", false, "show all example names")

flag.Parse()
}
81 changes: 54 additions & 27 deletions cmd/trygolang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"flag"
"fmt"
"github.com/devlights/try-golang/lib"
"log"
Expand All @@ -11,9 +10,16 @@ import (
"strings"
)

var mapping = make(lib.SampleMapping)
var (
args *Args
mapping lib.SampleMapping
)

func init() {
args = NewArgs()
args.Parse()

mapping = lib.NewSampleMapping()
mapping.MakeMapping()
}

Expand All @@ -34,15 +40,30 @@ func printAllExampleNames() {
}
}

func main() {
var (
onetime = flag.Bool("onetime", false, "run only one time")
showNames = flag.Bool("list", false, "show all example names")
)
func makeCandidates(userInput string) []string {
candidates := make([]string, 0, len(mapping))
for k := range mapping {
if strings.Contains(k, userInput) {
candidates = append(candidates, k)
}
}

flag.Parse()
return candidates
}

if *showNames {
func exec(target string) error {
if v, ok := mapping[target]; ok {
fmt.Printf("[Name] %q\n", target)
if err := v(); err != nil {
return err
}
}

return nil
}

func main() {
if args.ShowNames {
printAllExampleNames()
return
}
Expand All @@ -66,37 +87,43 @@ func main() {
break
}

candidates = make([]string, 0, len(mapping))
for k := range mapping {
if strings.Contains(k, userInput) {
candidates = append(candidates, k)
}
}

candidates = makeCandidates(userInput)
numberOfCandidate = len(candidates)

switch {
case numberOfCandidate == 0:
fmt.Printf("Not found...Try Again")
goto nextinput
case numberOfCandidate == 1:
userInput = candidates[0]
if v, ok := mapping[userInput]; ok {
fmt.Printf("[Name] %q\n", userInput)
if err := v(); err != nil {
log.Fatal(err)
}
if err := exec(userInput); err != nil {
log.Fatal(err)
}
case 1 < numberOfCandidate:
fmt.Printf("There's %d candidates found\n", len(candidates))

for _, item := range candidates {
fmt.Printf("\t%s\n", item)
isPerfectMatchFound := false
for _, c := range candidates {
if c == userInput {
if err := exec(userInput); err != nil {
log.Fatal(err)
}

isPerfectMatchFound = true
break
}
}

goto nextinput
if !isPerfectMatchFound {
fmt.Printf("There's %d candidates found\n", len(candidates))

for _, item := range candidates {
fmt.Printf("\t%s\n", item)
}

goto nextinput
}
}

if *onetime {
if args.OneTime {
break
}

Expand Down
9 changes: 7 additions & 2 deletions lib/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ import (
"github.com/devlights/try-golang/basic/variables"
)

// サンプル名とサンプル呼び出し関数のマッピング定義の型
// SampleMappingは、サンプル名とサンプル呼び出し関数のマッピング定義を持つ型です
type SampleMapping map[string]func() error

// マッピング生成
// NewSampleMapping は、SampleMappingのコンストラクタ関数です
func NewSampleMapping() SampleMapping {
return make(SampleMapping)
}

// MakeMapping は、マッピング生成します
func (m SampleMapping) MakeMapping() {
m["error01"] = error_.Error01
m["helloworld"] = helloworld.HelloWorld
Expand Down