command

package module
v0.0.0-...-620c6dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 5 Imported by: 1

README

Command

Command is a package that simplifies shell usage display in Go. It serves as a wrapper around the github.com/spf13/cobra package.

Installation

go get github.com/shengyanli1982/toolkit/pkg/command

Quick Start

command just needs to be imported and used directly.

PrettyCobraHelpAndUsage

Example

package main

import (
	"fmt"

	"github.com/shengyanli1982/toolkit/pkg/command"
	"github.com/spf13/cobra"
)

func main() {
	// 创建 cobra 命令
	// Create a cobra command
	cmd := cobra.Command{Use: "demo"}

	// 设置命令行使用说明生成器
	// Set the command line usage instruction generator
	command.PrettyCobraHelpAndUsage(&cmd)

	// 执行 cobra 命令
	// Execute the cobra command
	if err := cmd.Execute(); err != nil {
		// 如果执行命令时出现错误,打印错误并返回
		// If an error occurs while executing the command, print the error and return
		fmt.Println("Error:", err)
		return
	}
}

Result

$ go run demo.go -h
Usage:
        demo [flags]

Flags:
        -h, --help               help for demo

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomCobraHelp

func CustomCobraHelp() func(*cobra.Command, []string)

CustomCobraHelp 是一个自定义的 Cobra 命令行帮助信息生成器 CustomCobraHelp is a custom Cobra command line help message generator

func CustomCobraUsage

func CustomCobraUsage() func(*cobra.Command) error

CustomCobraUsage 是一个自定义的 Cobra 命令行使用说明生成器 CustomCobraUsage is a custom Cobra command line usage generator

func PrettyCobraHelpAndUsage

func PrettyCobraHelpAndUsage(cmd *cobra.Command)

PrettyCobraHelpAndUsage 设置美化后的 Cobra 命令行帮助信息和使用说明 PrettyCobraHelpAndUsage sets the beautified Cobra command line help message and usage

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL