1

adding interactive mode for command line newbs

This commit is contained in:
Alex Yatskov 2017-01-01 12:53:39 -08:00
parent ce5ce1c902
commit 7477a44e2b
5 changed files with 53 additions and 15 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "zero-epwing"]
path = zero-epwing
url = https://github.com/FooSoft/zero-epwing

Binary file not shown.

Binary file not shown.

64
main.go
View File

@ -23,6 +23,7 @@
package main package main
import ( import (
"bufio"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
@ -31,6 +32,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strings"
) )
func usage() { func usage() {
@ -75,38 +77,70 @@ func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
if flag.NArg() != 1 && flag.NArg() != 2 { var (
usage() interactive bool
os.Exit(2) inputPath string
} outputDir string
)
inputPath := flag.Arg(0) if flag.NArg() == 0 {
if *format == "" { fmt.Print("Specify path of dictionary to convert: ")
if *format = detectFormat(inputPath); *format == "" {
log.Fatal("failed to detect dictionary format") reader := bufio.NewReader(os.Stdin)
inputPath, _ = reader.ReadString('\n')
inputPath = strings.Replace(inputPath, "\n", "", -1)
interactive = true
fmt.Print("\n")
} else {
inputPath = flag.Arg(0)
if flag.NArg() > 1 {
outputDir = flag.Arg(1)
} }
} }
var outputDir string terminate := func() {
if flag.NArg() == 2 { if interactive {
outputDir = flag.Arg(1) fmt.Print("\nPress [ENTER] to terminate...")
} else { reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
os.Exit(1)
}
if _, err := os.Stat(inputPath); err != nil {
log.Printf("dictionary path '%s' does not exist", inputPath)
terminate()
}
if *format == "" {
if *format = detectFormat(inputPath); *format == "" {
log.Print("failed to detect dictionary format")
terminate()
}
}
if outputDir == "" {
var err error var err error
outputDir, err = ioutil.TempDir("", "yomichan_tmp_") outputDir, err = ioutil.TempDir("", "yomichan_tmp_")
if err != nil { if err != nil {
log.Fatal(err) log.Print(err)
terminate()
} }
*serve = true *serve = true
} }
if err := exportDb(inputPath, outputDir, *format, *title, *stride, *pretty); err != nil { if err := exportDb(inputPath, outputDir, *format, *title, *stride, *pretty); err != nil {
log.Fatal(err) log.Print(err)
terminate()
} }
if *serve { if *serve {
if err := serveDb(outputDir, *port); err != nil { if err := serveDb(outputDir, *port); err != nil {
log.Fatal(err) log.Print(err)
terminate()
} }
} }
} }

1
zero-epwing Submodule

@ -0,0 +1 @@
Subproject commit 82158b0104880d462d47d5a27108cfc31f00208c