github.com/haraldrudell/parl@v0.4.176/pos/exit.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pos
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"os"
    12  )
    13  
    14  const (
    15  	StatusCodeErr   = 1
    16  	StatusCodeUsage = 2
    17  )
    18  
    19  // Exit0 terminates the process successfully
    20  func Exit0() {
    21  	OsExit(0)
    22  }
    23  
    24  // Exit1 terminate the command echoing a failure to stderr and returning status code 1
    25  func Exit1(err error) {
    26  	Exit(StatusCodeErr, err)
    27  }
    28  
    29  // Exit1OneLine terminate the command echoing to stderr returning status code 1
    30  func Exit1OneLine(err error) {
    31  	if err == nil {
    32  		err = errors.New("Exit1OneLine with err nil")
    33  	}
    34  	fmt.Fprintf(os.Stderr, "%v\n", err)
    35  	OsExit(StatusCodeErr)
    36  }
    37  
    38  // Exit terminate the command echoing to stderr returning status code
    39  func Exit(code int, err error) {
    40  	if err != nil {
    41  		fmt.Fprintf(os.Stderr, "%+v\n", err)
    42  	}
    43  	OsExit(code)
    44  }
    45  
    46  // OsExit does os.Exit
    47  func OsExit(code int) {
    48  	os.Exit(code) // prints "exit status 1" to stderr
    49  }