github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/uroot/util/usage.go (about)

     1  // Copyright 2014-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"os"
     9  )
    10  
    11  // Usage wraps a passed in func() with a func() that sets
    12  // os.Args[0] to a string and then calls the func().
    13  //
    14  // It is intended to be called with Usage function from a flag package,
    15  // such as flag or spf13/pflag.
    16  // E.g., flag.usage = util.Usage(flag.Usage, "some message")
    17  //
    18  // Usage must not import "flag", since callers might use an alternate flags
    19  // package such as spf13/pflag, and would set Usage for a flag
    20  // package that the caller is not using.
    21  func Usage(wrapUsage func(), message string) func() {
    22  	return func() {
    23  		os.Args[0] = message
    24  		wrapUsage()
    25  	}
    26  }