Hello there! I don't quiet understand why
os.Exit doesn't terminate my
program according to its documentation:
Exit causes the current program to
exit with the given status code.
My program implements a rudimentary CLI parsing:
package main
import (
"fmt"
"os"
)
func main() {
var name string
var age string
args := os.Args[1:]
i := 0
for i+1 < len(args) {
option := args[i]
value := args[i+1]
switch option {
case "--name":
name = value
case "--age":
age = value
default:
fmt.Printf("Unknown option %s", option)
os.Exit(1) // <-- I expected this function to terminate the program
}
i += 2
}
fmt.Printf("name == %s, age == %s", name, age)
}Basically, I wanna rewrite this Fish program to Go:
#!/usr/bin/env fish
set --local name
set --local age
set --local i 1
while set --query argv[$i]
set --local option "$argv[$i]"
set --local value "$argv[$(math $i + 1)]"
switch "$option"
case --name
set name $value
case --age
set age $value
case '*'
printf "Unknown option %s" $option
return 1 # I expected os.Exit to be a "return {{code}}" shell equivalent
end
set i (math $i + 2)
end
printf "name == %s, age == %s" $name $age
For the context: I'm new to go but have shell scripting experience.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/03655054-4de5-43c2-9c05-72971c7a961cn%40googlegroups.com.