kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/cmdutil/cmdutil.go (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package cmdutil exports shared logic for implementing command-line
    18  // subcommands using the github.com/google/subcommands package.
    19  package cmdutil // import "kythe.io/kythe/go/util/cmdutil"
    20  
    21  import (
    22  	"context"
    23  	"flag"
    24  	"fmt"
    25  	"log"
    26  	"strings"
    27  
    28  	"github.com/google/subcommands"
    29  )
    30  
    31  // Info implements the methods of the subcommands.Command interface that handle
    32  // the command name and documentation. It also provides a noop default SetFlags
    33  // method and a default Execute method that prints its usage and exits.
    34  type Info struct {
    35  	name     string
    36  	synopsis string
    37  	usage    string
    38  }
    39  
    40  // NewInfo constructs an Info that reports the specified arguments for command
    41  // name, brief synopsis, and usage.
    42  func NewInfo(name, synopsis, usage string) Info {
    43  	if !strings.HasSuffix(usage, "\n") {
    44  		usage += "\n"
    45  	}
    46  	return Info{name: name, synopsis: synopsis, usage: usage}
    47  }
    48  
    49  // Name implements part of subcommands.Command.
    50  func (i Info) Name() string { return i.name }
    51  
    52  // Synopsis implements part of subcommands.Command.
    53  func (i Info) Synopsis() string { return i.synopsis }
    54  
    55  // Usage implements part of subcommands.Command.
    56  func (i Info) Usage() string { return i.usage + "\nOptions:\n" }
    57  
    58  // SetFlags implements part of subcommands.Command.
    59  func (i Info) SetFlags(*flag.FlagSet) {}
    60  
    61  // Execute implements part of subcommands.Command.
    62  // It prints the usage string to stdout and returns success.
    63  func (i Info) Execute(context.Context, *flag.FlagSet, ...any) subcommands.ExitStatus {
    64  	fmt.Print(i.usage) // the undecorated usage string
    65  	return subcommands.ExitSuccess
    66  }
    67  
    68  // Fail logs an error message and returns subcommands.ExitFailure.
    69  func (i Info) Fail(msg string, args ...any) subcommands.ExitStatus {
    70  	log.Output(1, fmt.Sprintf(msg, args...))
    71  	return subcommands.ExitFailure
    72  }