Skip to content

Commit 969027f

Browse files
committed
Create Utils and add new switches
1 parent 5223f57 commit 969027f

File tree

3 files changed

+92
-46
lines changed

3 files changed

+92
-46
lines changed

Main.go

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4417
func 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
}

SubtitleTranslator.code-workspace

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"folders": []
2+
"folders": [],
3+
"launch": {
4+
"version": "0.2.0",
5+
"configurations": []
6+
}
37
}

Utils.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"unicode"
8+
)
9+
10+
func isNumber(s string) bool {
11+
for _, c := range s {
12+
if !unicode.IsDigit(c) {
13+
return false
14+
}
15+
}
16+
17+
return true
18+
}
19+
20+
func CreateFile(path string) *os.File {
21+
f, err := os.Create(path)
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
return f
27+
}
28+
29+
func OpenFile(path string) *os.File {
30+
file, err := os.Open(path)
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
return file
36+
}
37+
38+
func DispatchArg(arg string) {
39+
argSwitch := arg[1:strings.Index(arg, " ")]
40+
fmt.Println(argSwitch)
41+
}
42+
43+
func GetCommandLineArgs() {
44+
args := os.Args[1:]
45+
for i := 0; i < len(args); i++ {
46+
DispatchArg(args[i])
47+
}
48+
}
49+
50+
func Assert(check bool, msg string) {
51+
if !check {
52+
fmt.Println(msg)
53+
os.Exit(1)
54+
}
55+
}

0 commit comments

Comments
 (0)