86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
|
package cmdline
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
printDebug bool
|
||
|
outputFile string
|
||
|
)
|
||
|
|
||
|
// func searchGoogle(song string, lg *log.Logger) {
|
||
|
|
||
|
// }
|
||
|
|
||
|
// func searchGenius(song string, lg *log.Logger) {
|
||
|
|
||
|
// }
|
||
|
|
||
|
func Main() int {
|
||
|
programName := os.Args[0]
|
||
|
errorLog := log.New(os.Stderr, "", 0)
|
||
|
mainLog := log.New(os.Stdout, "", 0)
|
||
|
|
||
|
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||
|
flags.Usage = func() {
|
||
|
out := flags.Output()
|
||
|
fmt.Fprintf(out, "Usage: %v [dir]\n\n", programName)
|
||
|
fmt.Fprint(out, " [dir] is optional; if not passed, '.' is used.\n\n")
|
||
|
fmt.Fprint(out, " By default, the server listens on localhost:8080. Both the\n")
|
||
|
fmt.Fprint(out, " host and the port are configurable with flags. Set the host\n")
|
||
|
fmt.Fprint(out, " to something else if you want the server to listen on a\n")
|
||
|
fmt.Fprint(out, " specific network interface. Setting the port to 0 will\n")
|
||
|
fmt.Fprint(out, " instruct the server to pick a random available port.\n\n")
|
||
|
flags.PrintDefaults()
|
||
|
}
|
||
|
|
||
|
outputFlag := flags.String("output", "", "Lyrics filename")
|
||
|
verboseFlag := flags.Bool("verbose", false, "Turn on debug. Default is false.")
|
||
|
searchFlag := flags.String("search", "", "Name of song to search. If the name of the song is not a single word, put in quotes\"\"")
|
||
|
|
||
|
flags.Parse(os.Args[1:])
|
||
|
|
||
|
if len(flags.Args()) > 1 {
|
||
|
errorLog.Println("Error: too many command-line arguments")
|
||
|
flags.Usage()
|
||
|
return 1
|
||
|
}
|
||
|
|
||
|
allSetFlags := flagsSet(flags)
|
||
|
|
||
|
songToSearch := *searchFlag
|
||
|
|
||
|
if len(songToSearch) == 0 {
|
||
|
errorLog.Println("Error: the song name must be provided for search")
|
||
|
flags.Usage()
|
||
|
return 1
|
||
|
}
|
||
|
|
||
|
if allSetFlags["output"] {
|
||
|
outputFile = *outputFlag
|
||
|
} else {
|
||
|
mainLog.Printf("Using %s as the name of the file(s) for downloaded lyrics..\n", songToSearch)
|
||
|
}
|
||
|
|
||
|
printDebug = *verboseFlag
|
||
|
if printDebug {
|
||
|
mainLog.Printf("Output flag: %s, Debug flag: %t, Search flag: %s\n", outputFile, printDebug, songToSearch)
|
||
|
}
|
||
|
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
// flagsSet returns a set of all the flags what were actually set on the
|
||
|
// command line.
|
||
|
func flagsSet(flags *flag.FlagSet) map[string]bool {
|
||
|
s := make(map[string]bool)
|
||
|
flags.Visit(func(f *flag.Flag) {
|
||
|
s[f.Name] = true
|
||
|
})
|
||
|
return s
|
||
|
}
|