Allow passing a format string to the generate command

The generate command now takes two optional arguments:

-c N
    Generate N number of sentences.

-f "some string"
    Use the given format string to generate sentences.
This commit is contained in:
Zorchenhimer 2021-02-21 20:37:15 -05:00
parent 71a0c01db9
commit 1ac2863a43
Signed by: Zorchenhimer
GPG Key ID: 70A1AB767AAB9C20
1 changed files with 18 additions and 20 deletions

View File

@ -1,20 +1,21 @@
package main package main
import ( import (
"os" "flag"
"fmt" "fmt"
"os"
"github.com/zorchenhimer/hacker-quotes" "github.com/zorchenhimer/hacker-quotes"
"github.com/zorchenhimer/hacker-quotes/database" "github.com/zorchenhimer/hacker-quotes/database"
) )
func main() { func main() {
fmt.Println("len(os.Args):", len(os.Args)) var count int
var format string
var count int = 1 flag.IntVar(&count, "c", 1, "Number of sentences to generate")
if len(os.Args) == 2 { flag.StringVar(&format, "f", "", "Custom format to use when generating sentences")
fmt.Sscanf(os.Args[1], "%d", &count) flag.Parse()
}
db, err := database.New("sqlite", "file:db.sqlite?mode=memory") db, err := database.New("sqlite", "file:db.sqlite?mode=memory")
if err != nil { if err != nil {
@ -34,23 +35,20 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if count == 1 { fmt.Println("")
sentence, err := hq.Hack() for i := 0; i < count; i++ {
var sentence string
var err error
if format != "" {
sentence, err = hq.HackThis(format)
} else {
sentence, err = hq.Hack()
}
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
fmt.Println(">>", sentence, "<<")
fmt.Println("\n>>", sentence, "<<\n")
} else {
fmt.Println("")
for i := 0; i < count; i++ {
sentence, err := hq.Hack()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(">>", sentence, "<<")
}
} }
} }