cmdflag

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

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

Go to latest
Published: Sep 7, 2020 License: BSD-3-Clause Imports: 7 Imported by: 1

README

cmdflag

Simple wiring for command flags that enables subcommands and shell completions.

The heavy lifting of completion is accomplished using https://github.com/posener/complete. cmdflags just makes the API more straightforward for a broad class of programs.

An Example Self-completing Program

A runnable example is included in example.go.

example is a simple program that generates its own completions.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/msolo/cmdflag"
)

const (
	doc = `example - a self-completing program`
)

var cmdDemo = &cmdflag.Command{
	Name:      "demo",
	Run:       runDemo,
	UsageLine: "example demo",
	UsageLong: `Run a simple demo.`,
	Flags: []cmdflag.Flag{
		{"subflag", cmdflag.FlagTypeBool, false, "demo a subflag", nil},
	},
}

func runDemo(ctx context.Context, cmd *cmdflag.Command, args []string) {
	subflag := false
	flags := cmd.BindFlagSet(map[string]interface{}{"subflag": &subflag})
	flags.Parse(args)
	fmt.Println("demo subflag:", subflag)
}

var cmdMain = &cmdflag.Command{
	Name:      "example",
	UsageLong: doc,
	Flags: []cmdflag.Flag{
		{"timeout", cmdflag.FlagTypeDuration, 0 * time.Millisecond, "timeout for command execution", nil},
		{"config-file", cmdflag.FlagTypeString, "", "local config file", cmdflag.PredictFiles("*")},
	},
}

var subcommands = []*cmdflag.Command{
	cmdDemo,
}

func main() {
	var timeout time.Duration
	var configFile string

	cmdMain.BindFlagSet(map[string]interface{}{
		"timeout": &timeout,
		"config-file": &configFile})

	cmd, args := cmdflag.Parse(cmdMain, subcommands)

	ctx := context.Background()
	if timeout > 0 {
		nctx, cancel := context.WithTimeout(ctx, timeout)
		defer cancel()
		ctx = nctx
	}

	cmd.Run(ctx, cmd, args)
}

Enabling Command Completion in Bash

> cd example/
> go build
> ./example 
Usage of example:
example - a self-completing program
  -config-file string
    	local config file
  -timeout duration
    	timeout for command execution

Install bash completions by running:
	complete -C example example
> complete -C ./example example
> ./example -<tab>
-config-file  -timeout      
> ./example <tab>demo 

Other Shells

zsh and fish are supported by the upstream completion libraries, so this should work.

Documentation

Index

Constants

View Source
const (
	FlagTypeInt = iota
	FlagTypeString
	FlagTypeDuration
	FlagTypeBool
)

Variables

View Source
var (
	PredictNothing  = complete.PredictNothing
	PredictAnything = complete.PredictAnything
	PredictDirs     = complete.PredictDirs
	PredictFiles    = complete.PredictFiles
	PredictFilesSet = complete.PredictFilesSet
	PredictSet      = complete.PredictSet
	PredictOr       = complete.PredictOr
)

Functions

This section is empty.

Types

type Args

type Args = complete.Args

type Command

type Command struct {
	Name      string
	UsageLine string
	UsageLong string
	Run       func(ctx context.Context, cmd *Command, args []string)
	Flags     []Flag
	Args      complete.Predictor
	// contains filtered or unexported fields
}

func Parse

func Parse(cmdMain *Command, subCmds []*Command) (cmd *Command, args []string)

func (*Command) BindFlagSet

func (cmd *Command) BindFlagSet(bindFlags map[string]interface{}) *flag.FlagSet

func (*Command) FlagSet

func (cmd *Command) FlagSet() *flag.FlagSet

type Flag

type Flag struct {
	Name         string
	FlagType     int
	DefaultValue interface{}
	Usage        string
	Predictor    complete.Predictor
}

Jump to

Keyboard shortcuts

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