Skip to content

Commit 6f824a3

Browse files
committed
Add command line args with switches
1 parent d7eebea commit 6f824a3

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

Main.go

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,55 @@ func CreateFile(path string) *os.File {
2929
return f
3030
}
3131

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+
}
42+
}
43+
3244
func main() {
3345
t := translator.New()
3446

35-
fmt.Println("Welcome")
36-
input := bufio.NewReader(os.Stdin)
47+
var SourcePath, DestinationPath string
48+
var Quiet bool = false
49+
50+
for i := 0; i < len(os.Args); i++ {
51+
currentArg := os.Args[i]
52+
if currentArg[0] == '-' {
53+
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+
}
3759

38-
fmt.Print("Input source file: ")
39-
filePath, _ := input.ReadString('\n')
60+
switch currentArg {
61+
case "-s", "--src", "--source":
62+
SourcePath = nextArg
63+
fmt.Println("source is " + SourcePath) // Temp
64+
case "-d", "--dest", "--destination":
65+
DestinationPath = nextArg
66+
fmt.Println("dst is " + DestinationPath) // Temp
67+
case "-q", "--quiet":
68+
Quiet = true
69+
default:
70+
panic("Unrecognized switch used!")
71+
}
72+
}
73+
}
4074

41-
// file, _ := os.Open("Assets/KO 105 tr.srt")
42-
file, _ := os.Open(filePath)
75+
//fmt.Println("Welcome")
4376

44-
fmt.Print("Input destination file: ")
45-
outPath, _ := input.ReadString('\n')
46-
outFile := CreateFile(outPath)
47-
//outFile := CreateFile("Assets/Translated-eng - 105.srt")
77+
inFile, _ := os.Open(SourcePath)
78+
outFile := CreateFile(DestinationPath)
4879

49-
scanner := bufio.NewScanner(file)
80+
scanner := bufio.NewScanner(inFile)
5081
for scanner.Scan() {
5182
line := scanner.Text()
5283
var outBuf string
@@ -62,10 +93,13 @@ func main() {
6293
}
6394

6495
outFile.WriteString(outBuf)
65-
fmt.Print(outBuf)
96+
97+
if !Quiet {
98+
fmt.Print(outBuf)
99+
}
66100
}
67101

68-
file.Close()
102+
inFile.Close()
69103
outFile.Close()
70104

71105
fmt.Println("\nTranslation has concluded!")

0 commit comments

Comments
 (0)