quack

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: MIT Imports: 13 Imported by: 0

README

quack

GoDoc Test

An interface driven cli lib for go.

Overview

Have you ever said

Why can't writing CLIs be as easy as defining a struct and writing a function?

Then quack is for you.

Integration with spf13/cobra

The Bind api creates a cobra.Command from the given structure. This allows for easy integration with existing cli's that use this framework.

Other framework support

Supporting other frameworks like urfave/cli would be pretty easy. Feel free to file an issue with your framework of choice if you want it added.

A simple command

main.go

type ToHex struct {
	Input int
}

func (t ToHex) Run() {
	fmt.Printf("%x", t.Input)
}

func main() {
	quack.MustBind("tohex", new(ToHex)).Execute()
}

Can now be run

go run main.go --input 12334
302e
A simple set of sub commands

examples/deeply_nested/main.go

package main

import "github.com/eliothedeman/quack"

type a struct {
}

func (a) SubCommands() quack.Map {
        return quack.Map{
                "b": new(b),
        }
}

type b struct {
}

func (b) SubCommands() quack.Map {
        return quack.Map{
                "c": new(c),
        }
}

type c struct {
        XX string `default:"YYY" short:"x"`
        Y  int    `help:"this is a help message"`
        Z  bool   `default:"true"`
}

func (c) Run([]string) {

}

func (c) Help() string {
	return "the nested c command"
}

func main() {
	quack.MustBind("nested", new(a))
}

go run examples/deeply_nested/main.go b -h
Usage:    b <cmd> [args]
      c the nested c command


go run examples/deeply_nested/main.go b c -h
Usage:    c [args]
        the nested c command
Flags:                                   
             --z         (default=true)  
Options:                                 
         -x, --xx string (default='YYY') 
             --y  int    (default=0)     this is a help message

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidType will be returned when a type that doesn't implement the correct interfaces is passed to a bidning function
	ErrInvalidType = errors.New("invlaid type")
	// ErrNotACommand will be returned when a binding target doesn't implement one of the command interfaces.
	ErrNotACommand = errors.New("not a command")
)

Functions

func BindCobra added in v0.2.0

func BindCobra(name string, root any) (*cobra.Command, error)

BindCobra a structure to a *cobra.Command (and sub-commands)

func MustBindCobra added in v0.2.0

func MustBindCobra(name string, root any) *cobra.Command

MustBindCobra will panic if BindCobra returns an error

Types

type CobraCommand added in v0.2.0

type CobraCommand interface {
	Run(cmd *cobra.Command, args []string)
}

CobraCommand is the a comand that implements the cobra.Command.Run interface. This is useful when you need lower level access to things like global options or the raw cli args.

type Command

type Command interface {
	Run([]string)
}

Command is a runnable command that doesn't have sub commands

type Defaulter

type Defaulter interface {
	Default()
}

Defaulter can set up the default arguments of a command

type Group

type Group interface {
	SubCommands() Map
}

Group is a set of subcommands or sub groups.

type Helper

type Helper interface {
	Help() string
}

Helper returns usage information for a command or group.

type Map

type Map = map[string]any

Map to cut down on repetitive use of map[string]any

type Parser

type Parser interface {
	Parse(string) error
}

Parser is an argument that wants to parse itself.

type ShortHelper added in v0.2.0

type ShortHelper interface {
	ShortHelp() string
}

ShortHelper implements a less verbose help.

type SimpleCommand added in v0.2.0

type SimpleCommand interface {
	Run()
}

SimpleCommand is a command that doesn't care about the raw args

type Validator

type Validator interface {
	Validate() error
}

Validator is a command or argument that wants to be validated.

Directories

Path Synopsis
examples
ls
tests

Jump to

Keyboard shortcuts

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