In this case you are getting the status of ‘go run’. You need to compile and run the binary to get the correct status. 

On Sep 23, 2024, at 9:38 AM, Maisa Unbelievable <emilygraceseville7...@gmail.com> wrote:

> Are you seeing the “unknown option” being output?

Yes, but I didn't see that the program stopped right after it. I am not sure why... Maybe I've misinterpreted something (I don't exclude this possibility, as I'm new to Go).

> What are your test arguments to the program?

go run test.go --name a --unknown 35

I don't get why when I write os.Exit(25) my exit status is set to 1 inside a shell instead of 25:

package main

import (
"fmt"
"os"
)

func main() {
fmt.Println("Execution started...")
os.Exit(25)
}

<terminal.png>


And why I have exit status 25 line printed to stderr?.. I mean, it's not a problem to 2> /dev/null it but anyway, why?

BTW, is this forum suitable for newbie in Go questions, or should I ask them somewhere else?
On Tuesday, September 24, 2024 at 12:12:10 AM UTC+10 Brian Candler wrote:
What makes you think that the line which calls os.Exit(1) is being reached?

What are your test arguments to the program?

On Monday 23 September 2024 at 14:42:31 UTC+1 Maisa Unbelievable 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/7d82bf76-0c1d-478b-bbca-75c10f868a92n%40googlegroups.com.
<terminal.png>

--
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/B2D34D0A-8260-4E4A-A6FE-B6A40AE71BE5%40ix.netcom.com.

Reply via email to