cliche

package module
v0.0.0-...-a80d97c Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 11 Imported by: 0

README

cliche: Simple CLIs for Go

//go:generate TODO(christian)
type Hello struct {
    Name string
}

func (h *Hello) Run(ctx context.Context, out io.Writer) error {
    fmt.Fprintf(out, "Hello, %v!", h.Name)
}

After running go generate and go build, there is now a hello command:

$ hello -name=World
Hello, World!
...
//go:generate TODO(christian)
type Hello struct {
    Name string `cli:"arg"`
}

func (h *Hello) Run(ctx context.Context, out io.Writer) error {
    fmt.Fprintf(out, "Hello, %v!", h.Name)
}
$ hello World
Hello, World!
...
//go:generate TODO(christian)
type Hello struct {
    Name string `cli:"default:World"`
}

func (h *Hello) Run(ctx context.Context, out io.Writer) error {
    fmt.Fprintf(out, "Hello, %v!", h.Name)
}
$ hello World
Hello, World!
...
//go:generate TODO(christian)
type Hello struct {
    Name string `cli:"flag;default:World"`
}

func (h *Hello) Run(ctx context.Context, out io.Writer) error {
    fmt.Fprintf(out, "Hello, %v!", h.Name)
}
$ hello World
Hello, World!
...

Documentation

Overview

Package cliche provides types and functions for declaratively creating CLIs in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandInputMetadata

type CommandInputMetadata struct {
	FieldName string
	Tag       Tag
	Doc       string
	Type      string
}

CommandInputMetadata contains details about how a Command's inputs should be mapped to the struct members of the implementing type.

type CommandMetadata

type CommandMetadata struct {
	// Name of the command to be generated. This defaults to the name of the
	// package, unless overridden.
	Name string

	// Package name from which the  Command is sourced.
	Package string

	// Type name of the  Command implementation.
	Type string

	// Help output for the  Command. This will be displayed along with usage
	// information on the command line. By default, sourced from doc comment for
	// the package in which the wrapped Command will live.
	Help string

	// Description of the command. Should be short and human readable. By
	// default, sourced from the doc comment on the wrapped  Command type.
	Description string

	// Inputs describe the handling of struct fields on the wrapped Command
	// implementation as inputs on the command line. The inputs are derived from
	// struct tags, when set.
	Inputs []CommandInputMetadata
	// contains filtered or unexported fields
}

CommandMetadata compiles details about how the Command type should be wrapped from the AST describing it. This type is used to execute a Go template, to generate the resulting Go source file.

func FromFile

func FromFile(typeName, file string) *CommandMetadata

FromFile attempts to find a type typeName and generate command metadata from file, returning nil on failure.

func (*CommandMetadata) Compile

func (meta *CommandMetadata) Compile(n ast.Node) bool

Compile the AST of a Go file into command metadata. Designed to be used as an argument to ast.Inspect.

type IO

type IO struct {
	In  io.Reader
	Out io.Writer
	Err io.Writer
}

IO wraps the I/O targets for a facile command.

type Tag

type Tag string

Directories

Path Synopsis
Package meta contains intermediate types and utilities used to parse a Go AST and generate code to implement a cliche CLI.
Package meta contains intermediate types and utilities used to parse a Go AST and generate code to implement a cliche CLI.

Jump to

Keyboard shortcuts

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