diff --git a/README.md b/README.md index 2f69d04..e0e4955 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ go install github.com/onyx-and-iris/lottery-cli/cmd/lottery@latest *flags* +- --kind/-k: The kind of lottery. - --count/-c: Number of draws to generate. - --count-prompt/-C: Prompt for the number of draws to generate. > Note. If both --count and --count-prompt are passed the count prompt will win. @@ -29,19 +30,42 @@ go install github.com/onyx-and-iris/lottery-cli/cmd/lottery@latest ```bash #!/usr/bin/env bash +export LOTTERY_KIND=lotto export LOTTERY_COUNT=3 export LOTTERY_COUNT_PROMPT=false ``` ## Use -There are no subcommands, just run the CLI directly passing any desired flags: +Run with the selection prompt without prompting for a count: ```console -lottery --count=3 +lottery ``` -You will then be entered into the selection prompt. +Run with the selection prompt but also prompt for a count: + +```console +lottery --count-prompt +``` + +Run with the selection prompt but pass in the count directly: + +```console +lottery --count=4 +``` + +Run a single draw directly: + +```console +lottery -k=euromillions +``` + +Run multiple draws directly: + +```console +lottery -k=euromillions -c=3 +``` ## Special Thanks diff --git a/cmd/lottery/main.go b/cmd/lottery/main.go index 0b5ee6d..ed469ac 100644 --- a/cmd/lottery/main.go +++ b/cmd/lottery/main.go @@ -32,29 +32,40 @@ func versionFromBuild() string { var cmd = &cobra.Command{ Use: "lottery", Short: "A CLI for National Lottery games.", + PreRunE: func(cmd *cobra.Command, args []string) error { + kindStr := viper.GetString("kind") + if kindStr != "" { + _, err := lottery.ParseKind(kindStr) + if err != nil { + return err + } + } + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { - var selected string - - form := huh.NewForm( - huh.NewGroup( - huh.NewSelect[string](). - Title("Pick a lottery."). - Options( - huh.NewOption("Lotto", "lotto"), - huh.NewOption("EuroMillions", "euromillions"), - huh.NewOption("Set For Life", "setforlife"), - huh.NewOption("Thunderball", "thunderball"), - huh.NewOption("Powerball", "powerball"), - ). - Value(&selected), - ), - ) - err := form.Run() - if err != nil { - return err + kindStr := viper.GetString("kind") + if kindStr == "" { + form := huh.NewForm( + huh.NewGroup( + huh.NewSelect[string](). + Title("Pick a lottery."). + Options( + huh.NewOption("Lotto", "lotto"), + huh.NewOption("EuroMillions", "euromillions"), + huh.NewOption("Set For Life", "setforlife"), + huh.NewOption("Thunderball", "thunderball"), + huh.NewOption("Powerball", "powerball"), + ). + Value(&kindStr), + ), + ) + err := form.Run() + if err != nil { + return err + } } - kind, err := lottery.ParseKind(selected) + kind, err := lottery.ParseKind(kindStr) if err != nil { return err } @@ -101,6 +112,7 @@ var cmd = &cobra.Command{ } func init() { + cmd.Flags().StringP("kind", "k", "", "Lottery kind to generate draws for.") cmd.Flags().IntP("count", "c", 1, "Number of draws to generate.") cmd.Flags().BoolP("count-prompt", "C", false, "Prompt for the number of draws to generate.") diff --git a/kinds.go b/kinds.go index c41dbba..ea631f6 100644 --- a/kinds.go +++ b/kinds.go @@ -25,6 +25,9 @@ func ParseKind(kind string) (Kind, error) { case "powerball": return KindPowerball, nil default: - return "", fmt.Errorf("invalid lottery kind: %s", kind) + return "", fmt.Errorf( + "invalid lottery kind: %s, must be one of: lotto, euromillions, setforlife, thunderball, powerball", + kind, + ) } } diff --git a/util.go b/util.go index f58c11a..bb79279 100644 --- a/util.go +++ b/util.go @@ -8,8 +8,8 @@ import ( // drawUnique returns count unique numbers in the range [1, max]. // It uses the rand.Perm function to generate a random permutation of numbers and selects the first count numbers from it. // The result is sorted before being returned. -func drawUnique(count, max int) []int { - perm := rand.Perm(max) +func drawUnique(count, maxNum int) []int { + perm := rand.Perm(maxNum) result := make([]int, count) for i := range result { result[i] = perm[i] + 1