Add command line options to select day/implementation

This commit is contained in:
akp 2021-10-22 21:37:11 +01:00
parent 8e6135fc50
commit b8d99546a4
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
4 changed files with 50 additions and 13 deletions

2
go.mod
View file

@ -5,6 +5,8 @@ go 1.17
require github.com/AlecAivazis/survey/v2 v2.3.2
require (
github.com/alexflint/go-arg v1.4.2 // indirect
github.com/alexflint/go-scalar v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/mattn/go-colorable v0.1.2 // indirect

5
go.sum
View file

@ -2,6 +2,10 @@ github.com/AlecAivazis/survey/v2 v2.3.2 h1:TqTB+aDDCLYhf9/bD2TwSO8u8jDSmMUd2SUVO
github.com/AlecAivazis/survey/v2 v2.3.2/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0=
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
@ -23,6 +27,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/alexflint/go-arg"
"github.com/codemicro/adventOfCode/runtime/challenge"
"github.com/codemicro/adventOfCode/runtime/runners"
"io/ioutil"
@ -13,17 +14,22 @@ const (
challengeInfoFile = "info.json"
)
var args struct {
ChallengeDay *int `arg:"-d,--day" help:"challenge day number to run"`
Implementation string `arg:"-i,--implementation" help:"implementation to use"`
}
func run() error {
// List and select challenges
arg.MustParse(&args)
// List and select challenges
selectedChallenge, err := selectChallenge(challengeDir)
if err != nil {
return err
}
// List and select implementations
selectedImplementation, err := selectImplementation(selectedChallenge)
if err != nil {
return err

View file

@ -6,6 +6,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/codemicro/adventOfCode/runtime/challenge"
"github.com/codemicro/adventOfCode/runtime/runners"
"strings"
)
func userSelect(question string, choices []string) (int, error) {
@ -14,6 +15,7 @@ func userSelect(question string, choices []string) (int, error) {
Message: question,
Options: choices,
}
//err := survey.AskOne(prompt, &o, survey.WithStdio(os.Stdin, os.Stderr, os.Stderr))
err := survey.AskOne(prompt, &o)
if err != nil {
return 0, err
@ -35,16 +37,27 @@ func selectChallenge(dir string) (*challenge.Challenge, error) {
return nil, err
}
if len(challenges) == 0 {
return nil, errors.New("no challenges to run")
}
if args.ChallengeDay != nil {
for _, ch := range challenges {
if ch.Number == *args.ChallengeDay {
fmt.Printf("Selecting day %d (%s)\n", ch.Number, ch.Name)
return ch, nil
}
}
fmt.Printf("Could not locate day %d\n", *args.ChallengeDay)
}
var selectedChallengeIndex int
switch len(challenges) {
case 0:
return nil, errors.New("no challenges to run")
case 1:
if x := len(challenges); x == 1 {
selectedChallengeIndex = 0
c := challenges[0]
fmt.Printf("Automatically selecting day %d (%s)\n", c.Number, c.Name)
default:
} else {
var opts []string
for _, c := range challenges {
opts = append(opts, c.String())
@ -66,15 +79,26 @@ func selectImplementation(ch *challenge.Challenge) (string, error) {
return "", err
}
if len(implementations) == 0 {
return "", errors.New("no implementations to use")
}
if args.Implementation != "" {
for _, im := range implementations {
if strings.EqualFold(im, args.Implementation) {
fmt.Printf("Selecting %s implementation\n", runners.RunnerNames[im])
return im, nil
}
}
fmt.Printf("Could not locate implementation %#v\n", args.Implementation)
}
var selectedImplementationIndex int
switch len(implementations) {
case 0:
return "", errors.New("no implementations to use")
case 1:
if x := len(implementations); x == 1 {
selectedImplementationIndex = 0
fmt.Printf("Automatically selecting implementation %s", runners.RunnerNames[implementations[0]])
default:
fmt.Printf("Automatically selecting %s implementation", runners.RunnerNames[implementations[0]])
} else {
var opts []string
for _, i := range implementations {
opts = append(opts, runners.RunnerNames[i])