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