github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/util/options/errors.go (about)

     1  // Copyright (c) 2015, Kevin Walsh.  All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package options works in concert with flag, adding prettier printing of
    16  // options.
    17  package options
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  )
    23  
    24  // FailIf does the same thing as Fail, but only if err is not nil.
    25  func FailIf(err error, msg string, args ...interface{}) {
    26  	if err != nil {
    27  		Fail(err, msg, args...)
    28  	}
    29  }
    30  
    31  // WarnIf prints an error and accompanying message, but only if err is not nil.
    32  func WarnIf(err error, msg string, args ...interface{}) {
    33  	if err != nil {
    34  		s := fmt.Sprintf(msg, args...)
    35  		fmt.Fprintf(os.Stderr, "warning: %v: %s\n", err, s)
    36  	}
    37  }
    38  
    39  // Fail prints an error and accompanying message to os.Stderr, then exits the
    40  // program with status 2. The err parameter can be nil.
    41  func Fail(err error, msg string, args ...interface{}) {
    42  	s := fmt.Sprintf(msg, args...)
    43  	if err != nil {
    44  		fmt.Fprintf(os.Stderr, "%v: %s\n", err, s)
    45  	} else {
    46  		fmt.Fprintf(os.Stderr, "error: %s\n", s)
    47  	}
    48  	os.Exit(2)
    49  }
    50  
    51  // Usage prints a message to os.Stderr, along with a note about the -help
    52  // option, then exits the program with status 1.
    53  func Usage(msg string, args ...interface{}) {
    54  	fmt.Fprintf(os.Stderr, msg+"\n", args...)
    55  	fmt.Fprintf(os.Stderr, "Try -help instead!\n")
    56  	os.Exit(1)
    57  }