Hi, your for loop condition skips odd numbered argument counts. Consider:
- args = ["--bad"] - len(args) = 1 - for i+1 < len(args) --> condition is false (1 not less than 1) Thus the only way to trigger the os.Exit(1) is to have at least 2 arguments, of which the first is not "--name" or "--age". For example: args= ["--bad", "anything"] However if the bad argument is the last, then the for loop exits early. -Diego On Mon, 23 Sept 2024 at 15:42, Maisa Unbelievable < emilygraceseville7...@gmail.com> wrote: > 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 > <https://groups.google.com/d/msgid/golang-nuts/03655054-4de5-43c2-9c05-72971c7a961cn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Diego Joss Electrical Engineer SamanTree Medical SA Avenue de Provence 12, 1007 Lausanne, Switzerland Tel: +41 21 625 08 97 -- 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/CAGjxhKkb2d%2BQbP68R_aiogwF1MHBNj8qGJRyx3Zah9XWCHUm0Q%40mail.gmail.com.