cli

package module
v0.0.0-...-08de2c9 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 5 Imported by: 0

README

tinytoolkit/cli

A simple CLI library for Go.

Installation

go get github.com/tinytoolkit/cli

Example

package main

import (
	"context"
	"flag"
	"fmt"
	"os"

	"github.com/tinytoolkit/cli"
)

func main() {
	var name string

	fs := flag.NewFlagSet("hellocmd", flag.ExitOnError)
	fs.StringVar(&name, "name", "world", "name to say hello to")

	helloCmd := &cli.Command{
		Name:    "hello",
		Aliases: []string{"hi", "hey"},
		Usage:   "say hello to someone",
		FlagSet: fs,
		Exec: func(ctx context.Context, args []string) error {
			fmt.Printf("Hello %s!\n", name)
			return nil
		},
	}

	rootCmd := &cli.Command{
		Name:        "mycli",
		Aliases:     []string{"my"},
		SubCommands: []*cli.Command{helloCmd},
	}

	if err := rootCmd.ParseAndExec(context.Background(), os.Args[1:]); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name        string
	Aliases     []string
	Usage       string
	FlagSet     *flag.FlagSet
	SubCommands []*Command
	Exec        func(ctx context.Context, args []string) error
}

Command represents a single command in the CLI.

func (*Command) ParseAndExec

func (c *Command) ParseAndExec(ctx context.Context, args []string) error

ParseAndExec parses the given arguments and executes the appropriate command.

func (*Command) PrintHelp

func (c *Command) PrintHelp()

PrintHelp displays the help information for the command.

Jump to

Keyboard shortcuts

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