@@ -5,76 +5,63 @@ import (
55 "fmt"
66 "os"
77 "strings"
8- "unicode"
98
109 translator "go-googletrans"
1110)
1211
13- func isNumber (s string ) bool {
14- for _ , c := range s {
15- if ! unicode .IsDigit (c ) {
16- return false
17- }
18- }
19-
20- return true
21- }
22-
23- func CreateFile (path string ) * os.File {
24- f , err := os .Create (path )
25- if err != nil {
26- panic (err )
27- }
28-
29- return f
30- }
31-
32- func DispatchArg (arg string ) {
33- argSwitch := arg [1 :strings .Index (arg , " " )]
34- fmt .Println (argSwitch )
35- }
36-
37- func GetCommandLineArgs () {
38- args := os .Args [1 :]
39- for i := 0 ; i < len (args ); i ++ {
40- DispatchArg (args [i ])
41- }
12+ func Help (exitCode int ) {
13+ // messages
14+ os .Exit (exitCode )
4215}
4316
4417func main () {
4518 t := translator .New ()
4619
4720 var SourcePath , DestinationPath string
21+ var DstLanguage string = "en"
22+ var SrcLanguage string = "auto"
23+
4824 var Quiet bool = false
4925
50- for i := 0 ; i < len (os .Args ); i ++ {
26+ if (len (os .Args ) == 2 ) && ((os .Args [1 ] == "help" ) || (os .Args [1 ] == "?" )) {
27+ Help (0 )
28+ // } else {
29+ // fmt.Println("Unknown argument!\nRun 'SubtitleTranslator help' for a list of valid arguments.")
30+ // os.Exit(1)
31+ }
32+
33+ for i := 1 ; i < len (os .Args ); i ++ {
5134 currentArg := os .Args [i ]
5235 if currentArg [0 ] == '-' {
5336 var nextArg string
54- if i + 2 > len (os .Args ) { // Array indices are zero-based but array lengths are not
55- panic ("Switch is missing an argument!" )
56- } else {
57- nextArg = os .Args [i + 1 ]
58- }
37+ Assert (i + 1 < len (os .Args ), "Switch is missing an argument!" ) // Array indices are zero-based but array lengths are not
38+ nextArg = os .Args [i + 1 ]
5939
6040 switch currentArg {
61- case "-s " , "--src " , "--source " :
41+ case "-i " , "--in " , "--input " :
6242 SourcePath = nextArg
63- fmt . Println ( "source is " + SourcePath ) // Temp
64- case "-d " , "--dest " , "--destination " :
43+ break
44+ case "-o " , "--out " , "--output " :
6545 DestinationPath = nextArg
66- fmt .Println ("dst is " + DestinationPath ) // Temp
46+ break
47+ case "-s" , "--src" , "--source" :
48+ SrcLanguage = nextArg
49+ break
50+ case "-d" , "--dest" , "--destination" :
51+ DstLanguage = nextArg
52+ break
6753 case "-q" , "--quiet" :
6854 Quiet = true
55+ break
6956 default :
70- panic ("Unrecognized switch used!" )
57+ fmt .Printf ("Bad switch: '%s'" , currentArg )
58+ Help (1 )
7159 }
7260 }
7361 }
7462
75- //fmt.Println("Welcome")
76-
77- inFile , _ := os .Open (SourcePath )
63+ // Abstract this out to a separate function
64+ inFile := OpenFile (SourcePath )
7865 outFile := CreateFile (DestinationPath )
7966
8067 scanner := bufio .NewScanner (inFile )
@@ -83,7 +70,7 @@ func main() {
8370 var outBuf string
8471
8572 if ! isNumber (line ) && ! strings .Contains (line , "-->" ) && line != "\n " {
86- translated , err := t .Translate (line , "tr" , "en" )
73+ translated , err := t .Translate (line , SrcLanguage , DstLanguage )
8774 if err != nil {
8875 panic (err )
8976 }
0 commit comments