fixed parsing of arguments

This commit is contained in:
Daniel Sommer 2023-09-22 16:28:09 +02:00
parent 0804fadbb5
commit e9f89da36f
2 changed files with 25 additions and 22 deletions

12
.vscode/launch.json vendored
View file

@ -8,13 +8,15 @@
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}/main.go", "program": "${workspaceFolder}/main.go",
"args": [ "args": [
"/home/velvettear/downloads/music/*", "--verbose",
"192.168.1.11:/share/music", "--PASSWORD",
"--password", "$velvet90",
"$Velvet90",
"--concurrency", "--concurrency",
"12", "12",
"--verbose", "--delay",
"250",
"/home/velvettear/downloads/music/*",
"192.168.1.11:/share/tmp/music",
], ],
"console": "integratedTerminal" "console": "integratedTerminal"
}, },

View file

@ -12,17 +12,18 @@ import (
// exported function(s) // exported function(s)
func Initialize() { func Initialize() {
os.Args = os.Args[1:]
var arguments []string var arguments []string
for _, arg := range os.Args { for index := 1; index < len(os.Args); index++ {
arg = strings.ToLower(arg) arg := strings.ToLower(os.Args[index])
if arg != "-v" && arg != "--verbose" { if arg != "-v" && arg != "--verbose" {
arguments = append(arguments, arg)
continue continue
} }
setVerbose(true) setVerbose(true)
} }
for index := 0; index < len(os.Args); index++ { var filteredArguments []string
switch strings.ToLower(os.Args[index]) { for index := 0; index < len(arguments); index++ {
switch arguments[index] {
case "-h": case "-h":
fallthrough fallthrough
case "--help": case "--help":
@ -32,8 +33,8 @@ func Initialize() {
fallthrough fallthrough
case "--concurrency": case "--concurrency":
index++ index++
if index < len(os.Args) { if index < len(arguments) {
concurrency, error := strconv.Atoi(os.Args[index]) concurrency, error := strconv.Atoi(arguments[index])
if error != nil { if error != nil {
break break
} }
@ -43,8 +44,8 @@ func Initialize() {
fallthrough fallthrough
case "--delay": case "--delay":
index++ index++
if index < len(os.Args) { if index < len(arguments) {
delay, error := strconv.Atoi(os.Args[index]) delay, error := strconv.Atoi(arguments[index])
if error != nil { if error != nil {
break break
} }
@ -54,27 +55,27 @@ func Initialize() {
fallthrough fallthrough
case "--password": case "--password":
index++ index++
if index > len(os.Args) { if index > len(arguments) {
break break
} }
setPassword(os.Args[index]) setPassword(arguments[index])
case "-u": case "-u":
fallthrough fallthrough
case "--user": case "--user":
index++ index++
if index > len(os.Args) { if index > len(arguments) {
break break
} }
setUser(os.Args[index]) setUser(arguments[index])
default: default:
arguments = append(arguments, os.Args[index]) filteredArguments = append(filteredArguments, arguments[index])
} }
} }
if len(os.Args) < 2 { if len(arguments) < 2 {
log.Fatal("error: missing arguments") log.Fatal("error: missing arguments")
} }
setSource(arguments[0]) setSource(filteredArguments[0])
setTarget(arguments[1]) setTarget(filteredArguments[1])
if !SourceIsRemote() { if !SourceIsRemote() {
source, _ := strings.CutSuffix(Source, "/*") source, _ := strings.CutSuffix(Source, "/*")
_, error := os.Stat(source) _, error := os.Stat(source)