github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/util/command.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  // This is the Command struct used by the noms utility. It is packaged in a separate util can be used by other programs as well.
    23  package util
    24  
    25  import (
    26  	"context"
    27  	"fmt"
    28  	"os"
    29  	"strings"
    30  
    31  	flag "github.com/juju/gnuflag"
    32  )
    33  
    34  type Command struct {
    35  	// Run runs the command.
    36  	// The args are the arguments after the command name.
    37  	Run func(ctx context.Context, args []string) int
    38  
    39  	// UsageLine is the one-line usage message.
    40  	// The first word in the line is taken to be the command name.
    41  	UsageLine string
    42  
    43  	// Short is the short description shown in the 'help' output.
    44  	Short string
    45  
    46  	// Long is the long message shown in the 'help <this-command>' output.
    47  	Long string
    48  
    49  	// Flag is a set of flags specific to this command.
    50  	Flags func() *flag.FlagSet
    51  
    52  	// Nargs is the minimum number of arguments expected after flags, specific to this command.
    53  	Nargs int
    54  }
    55  
    56  // Name returns the command's name: the first word in the usage line.
    57  func (nc *Command) Name() string {
    58  	name := nc.UsageLine
    59  	i := strings.Index(name, " ")
    60  	if i >= 0 {
    61  		name = name[:i]
    62  	}
    63  	return name
    64  }
    65  
    66  func countFlags(flags *flag.FlagSet) int {
    67  	if flags == nil {
    68  		return 0
    69  	} else {
    70  		n := 0
    71  		flags.VisitAll(func(f *flag.Flag) {
    72  			n++
    73  		})
    74  		return n
    75  	}
    76  }
    77  
    78  func (nc *Command) Usage() {
    79  	fmt.Fprintf(os.Stderr, "usage: %s\n\n", nc.UsageLine)
    80  	fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(nc.Long))
    81  	flags := nc.Flags()
    82  	if countFlags(flags) > 0 {
    83  		fmt.Fprintf(os.Stderr, "\noptions:\n")
    84  		flags.PrintDefaults()
    85  	}
    86  	os.Exit(1)
    87  }