2017-12-12 15:00:34 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2024-10-20 13:50:44 +07:00
|
|
|
package config
|
2017-12-10 22:58:12 +07:00
|
|
|
|
|
|
|
import (
|
2024-10-24 16:56:21 +07:00
|
|
|
"encoding"
|
|
|
|
"errors"
|
2017-12-10 22:58:12 +07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-10-22 20:08:12 +07:00
|
|
|
"io"
|
2017-12-10 22:58:12 +07:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2024-10-22 23:04:50 +07:00
|
|
|
"strings"
|
2024-10-22 21:46:13 +07:00
|
|
|
"time"
|
2017-12-10 22:58:12 +07:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
var ErrInternalError = errors.New("internal error")
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// anyValue wraps a reflect.Value object and implements flag.Value interface
|
|
|
|
// the reflect.Value could be Bool, String, Int, Uint and Float
|
2017-12-10 22:58:12 +07:00
|
|
|
type anyValue struct {
|
|
|
|
any reflect.Value
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// newAnyValue creates an anyValue object
|
2017-12-10 22:58:12 +07:00
|
|
|
func newAnyValue(v reflect.Value) *anyValue {
|
|
|
|
return &anyValue{any: v}
|
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
func (av *anyValue) String() string {
|
|
|
|
kind := av.any.Kind()
|
2017-12-10 22:58:12 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2024-10-21 00:15:51 +07:00
|
|
|
return strconv.FormatBool(av.any.Bool())
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.String:
|
2024-10-21 00:15:51 +07:00
|
|
|
return av.any.String()
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64:
|
2024-10-21 00:15:51 +07:00
|
|
|
return strconv.FormatInt(av.any.Int(), 10)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64:
|
2024-10-21 00:15:51 +07:00
|
|
|
return strconv.FormatUint(av.any.Uint(), 10)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Float32:
|
2024-10-21 00:15:51 +07:00
|
|
|
return strconv.FormatFloat(av.any.Float(), 'E', -1, 32)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Float64:
|
2024-10-21 00:15:51 +07:00
|
|
|
return strconv.FormatFloat(av.any.Float(), 'E', -1, 64)
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
|
|
|
|
return "unsupported type: " + kind.String()
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
const (
|
|
|
|
Float32Size = 32
|
|
|
|
Float64Size = 64
|
|
|
|
Int8Size = 8
|
|
|
|
Int16Size = 16
|
|
|
|
Int32Size = 32
|
|
|
|
Int64Size = 64
|
|
|
|
Uint8Size = 8
|
|
|
|
Uint16Size = 16
|
|
|
|
Uint32Size = 32
|
|
|
|
UintSize = 32
|
|
|
|
Uint64Size = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
func (av *anyValue) Set(value string) error {
|
2024-10-19 22:48:50 +07:00
|
|
|
kind := av.any.Kind()
|
2017-12-10 22:58:12 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.String:
|
2024-10-21 00:15:51 +07:00
|
|
|
av.any.SetString(value)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Float32:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithFloatX(av.any, value, Float32Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Float64:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithFloatX(av.any, value, Float64Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Int8:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithIntX(av.any, value, Int8Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Int16:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithIntX(av.any, value, Int16Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Int, reflect.Int32:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithIntX(av.any, value, Int32Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Int64:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithIntX(av.any, value, Int64Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Uint8:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithUintX(av.any, value, Uint8Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Uint16:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithUintX(av.any, value, Uint16Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Uint, reflect.Uint32:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithUintX(av.any, value, Uint32Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
case reflect.Uint64:
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithUintX(av.any, value, Uint64Size)
|
2017-12-10 22:58:12 +07:00
|
|
|
default:
|
2024-10-21 18:32:24 +07:00
|
|
|
return fmt.Errorf("%w: %s", errUnsupportedType, kind.String())
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// sliceValue wraps a reflect.Value object and implements flag.Value interface
|
|
|
|
// the reflect.Value could only be a sliceable type
|
2017-12-11 22:13:02 +07:00
|
|
|
type sliceValue struct {
|
|
|
|
value reflect.Value
|
|
|
|
separator string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSliceValue(v reflect.Value, separator string) *sliceValue {
|
|
|
|
return &sliceValue{value: v, separator: separator}
|
|
|
|
}
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
func (sv *sliceValue) String() string {
|
|
|
|
return sv.value.String()
|
2017-12-11 22:13:02 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
func (sv *sliceValue) Set(value string) error {
|
2024-10-19 22:48:50 +07:00
|
|
|
sp := sv.separator
|
2017-12-11 22:13:02 +07:00
|
|
|
if sp == "" {
|
|
|
|
sp = ":"
|
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
return setValueWithSlice(sv.value, value, sp)
|
2017-12-11 22:13:02 +07:00
|
|
|
}
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
// errorHandling is a global flag.ErrorHandling
|
2017-12-10 22:58:12 +07:00
|
|
|
var errorHandling = flag.ExitOnError
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// UsageFunc defines a callback function for printing command usage
|
2017-12-10 22:58:12 +07:00
|
|
|
type UsageFunc func(*Command) func()
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// usageHandler is a global UsageFunc callback, default is nil which means it
|
|
|
|
// will use default flag.Usage function
|
2017-12-10 22:58:12 +07:00
|
|
|
var usageHandler UsageFunc = nil
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// Command defines a command line structure
|
2017-12-10 22:58:12 +07:00
|
|
|
type Command struct {
|
2024-10-21 00:15:51 +07:00
|
|
|
Name string // Command name
|
|
|
|
prefix string // Args prefix
|
|
|
|
FlagSet *flag.FlagSet // Command arguments
|
|
|
|
Usage string // Command usage description
|
|
|
|
SubCommands map[string]*Command // Sub-commands
|
|
|
|
Args []string // Rest of args after parsing
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-20 13:50:44 +07:00
|
|
|
// NewCLI creates a command with given name, the command will use default
|
2017-12-12 15:00:34 +07:00
|
|
|
// ErrorHandling: flag.ExitOnError and default usage function: flag.Usage
|
2024-10-20 13:50:44 +07:00
|
|
|
func NewCLI(name string) *Command {
|
2017-12-10 22:58:12 +07:00
|
|
|
cmd := Command{
|
|
|
|
Name: name,
|
|
|
|
FlagSet: flag.NewFlagSet(name, errorHandling),
|
|
|
|
SubCommands: make(map[string]*Command),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
// NewCliWith creates a command with given name, error handling and customized
|
2017-12-12 15:00:34 +07:00
|
|
|
// usage function
|
2024-10-21 00:15:51 +07:00
|
|
|
func NewCliWith(
|
2024-10-20 12:39:44 +07:00
|
|
|
name string, errHandling flag.ErrorHandling, usageHandling UsageFunc,
|
|
|
|
) *Command {
|
2017-12-10 22:58:12 +07:00
|
|
|
errorHandling = errHandling
|
|
|
|
usageHandler = usageHandling
|
|
|
|
|
|
|
|
cmd := Command{
|
|
|
|
Name: name,
|
|
|
|
FlagSet: flag.NewFlagSet(name, errorHandling),
|
|
|
|
SubCommands: make(map[string]*Command),
|
|
|
|
}
|
|
|
|
|
|
|
|
if usageHandler != nil {
|
|
|
|
cmd.FlagSet.Usage = usageHandler(&cmd)
|
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 22:58:12 +07:00
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// Init analyzes the given structure interface, extracts cli definitions from
|
|
|
|
// its tag and installs command flagset by flag APIs. The interface must be a
|
|
|
|
// structure pointer, otherwise will return an error
|
2024-10-22 23:04:50 +07:00
|
|
|
func (c *Command) Init(in interface{}) error {
|
|
|
|
ptrRef := reflect.ValueOf(in)
|
2017-12-10 22:58:12 +07:00
|
|
|
|
|
|
|
if ptrRef.IsNil() || ptrRef.Kind() != reflect.Ptr {
|
2024-10-21 18:32:24 +07:00
|
|
|
return fmt.Errorf("%w: %s",
|
|
|
|
errExpectStructPointerInsteadOf, ptrRef.Kind().String())
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
valueOfStruct := ptrRef.Elem()
|
|
|
|
if valueOfStruct.Kind() != reflect.Struct {
|
2024-10-21 18:32:24 +07:00
|
|
|
return fmt.Errorf("%w: %s",
|
|
|
|
errExpectStructPointerInsteadOf, valueOfStruct.Kind().String())
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
return c.parseValue(valueOfStruct)
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-22 23:04:50 +07:00
|
|
|
// Capture filter config args and return rest args
|
|
|
|
func (c *Command) Capture(args []string) []string {
|
|
|
|
var (
|
|
|
|
result []string
|
|
|
|
expectValue bool
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
if !strings.HasPrefix(arg, "-") {
|
|
|
|
if expectValue {
|
|
|
|
c.Args = append(c.Args, arg)
|
|
|
|
expectValue = false
|
|
|
|
} else {
|
|
|
|
result = append(result, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := strings.TrimPrefix(strings.TrimPrefix(arg, "-"), "-")
|
2024-10-22 23:28:56 +07:00
|
|
|
parts := strings.Split(name, "=")
|
2024-10-22 23:04:50 +07:00
|
|
|
|
2024-10-22 23:28:56 +07:00
|
|
|
if c.FlagSet.Lookup(parts[0]) != nil {
|
2024-10-22 23:04:50 +07:00
|
|
|
c.Args = append(c.Args, arg)
|
2024-10-22 23:28:56 +07:00
|
|
|
expectValue = len(parts) == 1
|
2024-10-22 23:04:50 +07:00
|
|
|
} else {
|
|
|
|
result = append(result, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// parseValue parses a reflect.Value object and extracts cli definitions
|
2024-10-21 00:15:51 +07:00
|
|
|
func (c *Command) parseValue(value reflect.Value) error {
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
2017-12-10 22:58:12 +07:00
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
typeOfStruct := value.Type()
|
|
|
|
|
|
|
|
for i := 0; i < value.NumField() && err == nil; i++ {
|
|
|
|
valueOfField := value.Field(i)
|
2017-12-10 22:58:12 +07:00
|
|
|
kindOfField := valueOfField.Kind()
|
|
|
|
structOfField := typeOfStruct.Field(i)
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
switch kindOfField {
|
|
|
|
case reflect.Ptr:
|
2017-12-10 22:58:12 +07:00
|
|
|
if !valueOfField.IsNil() && valueOfField.CanSet() {
|
2024-10-19 22:48:50 +07:00
|
|
|
cmd := c.createSubCommand(structOfField.Tag)
|
2017-12-11 16:14:53 +07:00
|
|
|
err = cmd.Init(valueOfField.Interface())
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
case reflect.Struct:
|
2024-10-19 22:48:50 +07:00
|
|
|
cmd := c.createSubCommand(structOfField.Tag)
|
2017-12-11 16:14:53 +07:00
|
|
|
err = cmd.parseValue(valueOfField)
|
2024-10-21 00:15:51 +07:00
|
|
|
default:
|
2024-10-19 22:48:50 +07:00
|
|
|
err = c.addFlag(valueOfField, structOfField)
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
return err
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// addFlag installs a command flag variable by flag API
|
2024-10-21 00:15:51 +07:00
|
|
|
func (c *Command) addFlag(value reflect.Value, field reflect.StructField) error {
|
|
|
|
name, ok := field.Tag.Lookup("cli")
|
|
|
|
if !ok || name == "" {
|
2017-12-10 22:58:12 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
if len(c.prefix) > 0 {
|
|
|
|
name = c.prefix + name
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
usage, _ := field.Tag.Lookup("usage")
|
|
|
|
|
2024-10-22 21:46:13 +07:00
|
|
|
if value.Type().PkgPath() == "time" && value.Type().Name() == "Duration" {
|
|
|
|
c.FlagSet.DurationVar(
|
|
|
|
(*time.Duration)(unsafe.Pointer(value.UnsafeAddr())),
|
|
|
|
name, time.Duration(0), usage,
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
unmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
|
|
|
valuePtr := reflect.New(value.Type())
|
|
|
|
|
|
|
|
if valuePtr.Type().Implements(unmarshalerType) {
|
|
|
|
c.FlagSet.Func(name, usage, func(s string) error {
|
|
|
|
if decoder, ok := valuePtr.Interface().(encoding.TextUnmarshaler); ok {
|
|
|
|
if err := decoder.UnmarshalText([]byte(s)); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal text: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value.Set(valuePtr.Elem())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrInternalError
|
|
|
|
},
|
|
|
|
)
|
2024-10-24 17:58:49 +07:00
|
|
|
|
2024-10-24 17:58:16 +07:00
|
|
|
return nil
|
2024-10-24 16:56:21 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
kind := value.Kind()
|
2017-12-10 22:58:12 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2024-10-21 00:15:51 +07:00
|
|
|
c.FlagSet.BoolVar(
|
|
|
|
(*bool)(unsafe.Pointer(value.UnsafeAddr())), name, false, usage,
|
|
|
|
)
|
|
|
|
|
2017-12-10 22:58:12 +07:00
|
|
|
return nil
|
|
|
|
case reflect.String,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64,
|
|
|
|
reflect.Float32,
|
|
|
|
reflect.Float64:
|
2024-10-21 00:15:51 +07:00
|
|
|
anyValue := newAnyValue(value)
|
2024-10-19 22:48:50 +07:00
|
|
|
c.FlagSet.Var(anyValue, name, usage)
|
2017-12-11 22:13:02 +07:00
|
|
|
case reflect.Slice:
|
2024-10-21 00:15:51 +07:00
|
|
|
sliceValue := newSliceValue(value, field.Tag.Get("separator"))
|
2024-10-19 22:48:50 +07:00
|
|
|
c.FlagSet.Var(sliceValue, name, usage)
|
2017-12-10 22:58:12 +07:00
|
|
|
default:
|
2024-10-21 18:32:24 +07:00
|
|
|
return fmt.Errorf("%w: %s", errUnsupportedType, kind.String())
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// createSubCommand creates sub-commands
|
2024-10-19 22:48:50 +07:00
|
|
|
func (c *Command) createSubCommand(tag reflect.StructTag) *Command {
|
2024-10-21 00:15:51 +07:00
|
|
|
name, ok := tag.Lookup("cli")
|
|
|
|
if !ok || name == "" {
|
2024-10-19 22:48:50 +07:00
|
|
|
return c
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := Command{SubCommands: make(map[string]*Command)}
|
|
|
|
|
|
|
|
cmd.Name = name
|
2024-10-21 00:15:51 +07:00
|
|
|
cmd.prefix = c.prefix + name + "-"
|
|
|
|
cmd.FlagSet = c.FlagSet
|
|
|
|
cmd.Usage, _ = tag.Lookup("usage")
|
2017-12-10 22:58:12 +07:00
|
|
|
|
|
|
|
if usageHandler != nil {
|
|
|
|
cmd.FlagSet.Usage = usageHandler(&cmd)
|
|
|
|
}
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
c.SubCommands[name] = &cmd
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 22:58:12 +07:00
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// Parse parses values from command line and save values into given structure.
|
|
|
|
// The Init(interface{}) function must be called before parsing
|
2024-10-19 22:48:50 +07:00
|
|
|
func (c *Command) Parse(args []string) error {
|
|
|
|
if err := c.FlagSet.Parse(args); err != nil {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("parse flag set: %w", err)
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
c.Args = c.FlagSet.Args()
|
2017-12-10 22:58:12 +07:00
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
return nil
|
2017-12-10 22:58:12 +07:00
|
|
|
}
|
2024-10-22 20:08:12 +07:00
|
|
|
|
|
|
|
// PrintUsage prints command description
|
|
|
|
func (c *Command) PrintUsage(w io.Writer) error {
|
|
|
|
if _, err := w.Write([]byte(c.Usage + "\n")); err != nil {
|
|
|
|
return fmt.Errorf("write usage: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.FlagSet.SetOutput(w)
|
|
|
|
c.FlagSet.Usage()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|