Skip to content

Commit 766d65f

Browse files
committed
Working command-line file translation
1 parent 969027f commit 766d65f

File tree

2 files changed

+86
-71
lines changed

2 files changed

+86
-71
lines changed

Main.go

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,109 @@ import (
1010
)
1111

1212
func Help(exitCode int) {
13-
// messages
13+
fmt.Println("Help menu")
1414
os.Exit(exitCode)
1515
}
1616

17-
func main() {
17+
func TranslateFile(srcPath, dstPath, srcLang, dstLang string, quiet bool) error {
1818
t := translator.New()
1919

20+
inFile := OpenFile(srcPath)
21+
outFile := CreateFile(dstPath)
22+
23+
scanner := bufio.NewScanner(inFile)
24+
for scanner.Scan() {
25+
line := scanner.Text()
26+
var outBuf string
27+
28+
if !isNumber(line) && !strings.Contains(line, "-->") && line != "\n" {
29+
translated, err := t.Translate(line, srcLang, dstLang)
30+
if err != nil {
31+
return err
32+
}
33+
outBuf = translated.Text + "\n"
34+
} else {
35+
outBuf = line + "\n"
36+
}
37+
38+
outFile.WriteString(outBuf)
39+
if !quiet {
40+
fmt.Print(outBuf)
41+
}
42+
}
43+
44+
inFile.Close()
45+
outFile.Close()
46+
47+
return nil
48+
}
49+
50+
func main() {
2051
var SourcePath, DestinationPath string
2152
var DstLanguage string = "en"
2253
var SrcLanguage string = "auto"
23-
2454
var Quiet bool = false
2555

26-
if (len(os.Args) == 2) && ((os.Args[1] == "help") || (os.Args[1] == "?")) {
56+
if len(os.Args) <= 1 {
57+
Help(1)
58+
} else if (len(os.Args) == 2) && ((os.Args[1] == "help") || (os.Args[1] == "?")) {
2759
Help(0)
28-
// } else {
29-
// fmt.Println("Unknown argument!\nRun 'SubtitleTranslator help' for a list of valid arguments.")
30-
// os.Exit(1)
3160
}
3261

62+
validArgFound := false
3363
for i := 1; i < len(os.Args); i++ {
3464
currentArg := os.Args[i]
3565
if currentArg[0] == '-' {
36-
var nextArg string
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]
39-
4066
switch currentArg {
4167
case "-i", "--in", "--input":
42-
SourcePath = nextArg
43-
break
68+
Assert(i+1 < len(os.Args), "Switch "+"\""+currentArg+"\""+" is missing an argument!")
69+
i++
70+
SourcePath = os.Args[i]
71+
validArgFound = true
4472
case "-o", "--out", "--output":
45-
DestinationPath = nextArg
46-
break
73+
Assert(i+1 < len(os.Args), "Switch "+"\""+currentArg+"\""+" is missing an argument!")
74+
i++
75+
DestinationPath = os.Args[i]
76+
validArgFound = true
4777
case "-s", "--src", "--source":
48-
SrcLanguage = nextArg
49-
break
78+
Assert(i+1 < len(os.Args), "Switch "+"\""+currentArg+"\""+" is missing an argument!")
79+
i++
80+
SrcLanguage = os.Args[i]
81+
validArgFound = true
5082
case "-d", "--dest", "--destination":
51-
DstLanguage = nextArg
52-
break
83+
Assert(i+1 < len(os.Args), "Switch "+"\""+currentArg+"\""+" is missing an argument!")
84+
i++
85+
DstLanguage = os.Args[i]
86+
validArgFound = true
5387
case "-q", "--quiet":
5488
Quiet = true
55-
break
89+
validArgFound = true
5690
default:
57-
fmt.Printf("Bad switch: '%s'", currentArg)
91+
fmt.Printf("Unknown switch: \"%s\".\n", currentArg)
5892
Help(1)
5993
}
6094
}
6195
}
6296

63-
// Abstract this out to a separate function
64-
inFile := OpenFile(SourcePath)
65-
outFile := CreateFile(DestinationPath)
66-
67-
scanner := bufio.NewScanner(inFile)
68-
for scanner.Scan() {
69-
line := scanner.Text()
70-
var outBuf string
71-
72-
if !isNumber(line) && !strings.Contains(line, "-->") && line != "\n" {
73-
translated, err := t.Translate(line, SrcLanguage, DstLanguage)
74-
if err != nil {
75-
panic(err)
76-
}
77-
outBuf = translated.Text + "\n"
78-
} else {
79-
outBuf = line + "\n"
80-
}
97+
if !validArgFound {
98+
fmt.Println("Unknown command!")
99+
Help(1)
100+
}
81101

82-
outFile.WriteString(outBuf)
102+
if SourcePath == "" {
103+
fmt.Println("No source path was specified!")
104+
os.Exit(1)
105+
}
83106

84-
if !Quiet {
85-
fmt.Print(outBuf)
86-
}
107+
if DestinationPath == "" {
108+
fmt.Println("No destination path was specified!")
109+
os.Exit(1)
87110
}
88111

89-
inFile.Close()
90-
outFile.Close()
112+
fmt.Printf("Translating \"%s\" to \"%s\".\n", SourcePath, DestinationPath)
113+
fmt.Println("============================================================================")
114+
err := TranslateFile(SourcePath, DestinationPath, SrcLanguage, DstLanguage, Quiet)
115+
AssertError(err)
91116

92117
fmt.Println("\nTranslation has concluded!")
93118
fmt.Scanln()

Utils.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"strings"
76
"unicode"
87
)
98

@@ -17,39 +16,30 @@ func isNumber(s string) bool {
1716
return true
1817
}
1918

20-
func CreateFile(path string) *os.File {
21-
f, err := os.Create(path)
22-
if err != nil {
23-
panic(err)
19+
func Assert(check bool, msg string) {
20+
if !check {
21+
fmt.Println(msg)
22+
os.Exit(1)
2423
}
25-
26-
return f
2724
}
2825

29-
func OpenFile(path string) *os.File {
30-
file, err := os.Open(path)
26+
func AssertError(err error) {
3127
if err != nil {
32-
panic(err)
28+
fmt.Println(err.Error())
29+
os.Exit(1)
3330
}
34-
35-
return file
3631
}
3732

38-
func DispatchArg(arg string) {
39-
argSwitch := arg[1:strings.Index(arg, " ")]
40-
fmt.Println(argSwitch)
41-
}
33+
func CreateFile(path string) *os.File {
34+
f, err := os.Create(path)
35+
AssertError(err)
4236

43-
func GetCommandLineArgs() {
44-
args := os.Args[1:]
45-
for i := 0; i < len(args); i++ {
46-
DispatchArg(args[i])
47-
}
37+
return f
4838
}
4939

50-
func Assert(check bool, msg string) {
51-
if !check {
52-
fmt.Println(msg)
53-
os.Exit(1)
54-
}
40+
func OpenFile(path string) *os.File {
41+
file, err := os.Open(path)
42+
AssertError(err)
43+
44+
return file
5545
}

0 commit comments

Comments
 (0)