Standard library / flag

flag.ExitOnError

Terminates the process with status 2 on parse failure, or 0 for a help request. Run only in an isolated process. Expected stdout: 'before\n'. Expected exit status 2.

Formal Syntax & Structure

go syntaxgo
const ExitOnError ErrorHandling = 1

Working Example

Example Snippetgo
package main
import (  "flag"  "fmt"  "io")
func main() {  f := flag.NewFlagSet("demo", flag.ExitOnError)  f.SetOutput(io.Discard)  fmt.Println("before")  _ = f.Parse([]string{"-unknown"})  fmt.Println("unreachable")}
Go Reference