github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/base/base.go (about)

     1  // Copyright 2017 The Go 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 base defines shared basic pieces of the go command,
     6  // in particular logging and the Command structure.
     7  package base
     8  
     9  import (
    10  	"github.com/shogo82148/std/context"
    11  	"github.com/shogo82148/std/flag"
    12  )
    13  
    14  // A Command is an implementation of a go command
    15  // like go build or go fix.
    16  type Command struct {
    17  	// Run runs the command.
    18  	// The args are the arguments after the command name.
    19  	Run func(ctx context.Context, cmd *Command, args []string)
    20  
    21  	// UsageLine is the one-line usage message.
    22  	// The words between "go" and the first flag or argument in the line are taken to be the command name.
    23  	UsageLine string
    24  
    25  	// Short is the short description shown in the 'go help' output.
    26  	Short string
    27  
    28  	// Long is the long message shown in the 'go help <this-command>' output.
    29  	Long string
    30  
    31  	// Flag is a set of flags specific to this command.
    32  	Flag flag.FlagSet
    33  
    34  	// CustomFlags indicates that the command will do its own
    35  	// flag parsing.
    36  	CustomFlags bool
    37  
    38  	// Commands lists the available commands and help topics.
    39  	// The order here is the order in which they are printed by 'go help'.
    40  	// Note that subcommands are in general best avoided.
    41  	Commands []*Command
    42  }
    43  
    44  var Go = &Command{
    45  	UsageLine: "go",
    46  	Long:      `Go is a tool for managing Go source code.`,
    47  }
    48  
    49  // Lookup returns the subcommand with the given name, if any.
    50  // Otherwise it returns nil.
    51  //
    52  // Lookup ignores subcommands that have len(c.Commands) == 0 and c.Run == nil.
    53  // Such subcommands are only for use as arguments to "help".
    54  func (c *Command) Lookup(name string) *Command
    55  
    56  // LongName returns the command's long name: all the words in the usage line between "go" and a flag or argument,
    57  func (c *Command) LongName() string
    58  
    59  // Name returns the command's short name: the last word in the usage line before a flag or argument.
    60  func (c *Command) Name() string
    61  
    62  func (c *Command) Usage()
    63  
    64  // Runnable reports whether the command can be run; otherwise
    65  // it is a documentation pseudo-command such as importpath.
    66  func (c *Command) Runnable() bool
    67  
    68  func AtExit(f func())
    69  
    70  func Exit()
    71  
    72  func Fatalf(format string, args ...any)
    73  
    74  func Errorf(format string, args ...any)
    75  
    76  func ExitIfErrors()
    77  
    78  func Error(err error)
    79  
    80  func Fatal(err error)
    81  
    82  func SetExitStatus(n int)
    83  
    84  func GetExitStatus() int
    85  
    86  // Run runs the command, with stdout and stderr
    87  // connected to the go command's own stdout and stderr.
    88  // If the command fails, Run reports the error using Errorf.
    89  func Run(cmdargs ...any)
    90  
    91  // RunStdin is like run but connects Stdin.
    92  func RunStdin(cmdline []string)
    93  
    94  // Usage is the usage-reporting function, filled in by package main
    95  // but here for reference by other packages.
    96  var Usage func()
    97  
    98  type Counter interface {
    99  	Inc()
   100  }
   101  
   102  // NewCounter registers a new counter. It must be called from an init function
   103  // or global variable initializer.
   104  func NewCounter(name string) Counter
   105  
   106  func RegisteredCounterNames() []string