simplecli

package module
v0.0.0-...-0f78bf5 Latest Latest
Warning

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

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

README

simplecli

A simple CLI library for Go.

Installation

go get github.com/joeychilson/simplecli

Example

package main

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

	"github.com/joeychilson/simplecli"
)

func main() {
	var name string

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

	helloCmd := &simplecli.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 := &simplecli.Command{
		Name:        "mycli",
		Aliases:     []string{"my"},
		SubCommands: []*simplecli.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