flag

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

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

Go to latest
Published: May 25, 2017 License: MIT Imports: 7 Imported by: 0

README

flag

Build Status codecov GoDoc Go Report Card

Like flag, but with bash completion support.

Features

  • Fully compatible with standard library flag package
  • Bash completions for flag names and flag values
  • Additional flag types provided:

Example

Here is an example

Usage

import (
-	"flag"
+	"github.com/posener/flag"
)

var (
-	file = flag.String("file", "", "file value")
+	file = flag.File("file", "*.md", "", "file value")
-	dir  = flag.String("dir", "", "dir value")
+	dir  = flag.Dir("dir", "*", "", "dir value")
	b    = flag.Bool("bool", false, "bool value")
	s    = flag.String("any", "", "string value")
-	opts = flag.String("choose", "", "some items to choose from")
+	opts = flag.Choice("choose", []string{"work", "drink"}, "", "some items to choose from")
)

func main() {
+	flag.SetInstallFlags("complete", "uncomplete")
	flag.Parse()
+	if flag.Complete() {  // runs bash completion if necessary
+		return  // return from main without executing the rest of the command
+	}
    ...
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Parse  = gflag.Parse
	Parsed = gflag.Parsed

	String      = gflag.String
	StringVar   = gflag.StringVar
	Int         = gflag.Int
	IntVar      = gflag.IntVar
	Int64       = gflag.Int64
	Int64Var    = gflag.Int64Var
	Float64     = gflag.Float64
	Float64Var  = gflag.Float64Var
	DurationVar = gflag.DurationVar
	UintVar     = gflag.UintVar
	NArg        = gflag.NArg
	Args        = gflag.Args
)

Copy functions from standard library

View Source
var CommandLine = &FlagSet{gflag.CommandLine}

CommandLine is a copy of CommandLine variable from flag standard library

Functions

func Bool

func Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

func BoolVar

func BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument s points to a bool variable in which to store the value of the flag.

func Choice

func Choice(name string, choices []string, value string, usage string) *string

Choice is a flag that has a choice of possible string values

func ChoiceVar

func ChoiceVar(p *string, name string, choices []string, value string, usage string)

ChoiceVar is a Choice with a given pointer

func Complete

func Complete() bool

Complete tries to complete the command line that was already entered. It returns true on success, on which the main program should exit

Example

ExampleComplete shows how bash completion works

package main

import (
	"os"
	"sync"

	"github.com/posener/flag"
)

var once = sync.Once{}

func chdir() {
	once.Do(func() {
		os.Chdir("tests")
	})
}

func main() {
	chdir()

	// define a flag
	flag.File("file", "*.md", "", "file value")

	// set the environment variable used for setting the completion
	// look that there is a space after the '-file' flag, indicating that the user
	// want to complete by that flag
	os.Setenv("COMP_LINE", "demo -file ")

	// complete
	flag.Complete()

}
Output:

readme.md
sub/
./

func Dir

func Dir(name string, pattern string, value string, usage string) *string

Dir is a flag the relates to a directory on the filesystem according to a given pattern. For any directory name use "*" as the pattern.

func DirVar

func DirVar(p *string, name string, pattern string, value string, usage string)

DirVar is a Dir with a given pointer

func File

func File(name string, pattern string, value string, usage string) *string

File is a flag that is related to a file path on the filesystem according to a given pattern. for any file use "*" as the pattern.

func FileVar

func FileVar(p *string, name string, pattern string, value string, usage string)

FileVar is a File with a given pointer

func SetInstallFlags

func SetInstallFlags(installName, uninstallName string)

SetInstallFlags adds to a flagset install and uninstall flags which invokes installation of bash completion in the user's home folder.

func StringCompleter

func StringCompleter(name string, completer Completer, value string, usage string) *string

StringCompleter is a string flag with custom bash completion

func StringCompleterVar

func StringCompleterVar(p *string, name string, completer Completer, value string, usage string)

StringCompleterVar is a StringCompleter with a given pointer

Types

type CompleteFn

type CompleteFn func(string) ([]string, bool)

CompleteFn is function implementing the Completer interface

func (CompleteFn) Complete

func (c CompleteFn) Complete(last string) ([]string, bool)

Complete implements the Complete interface

type Completer

type Completer interface {
	// Complete according to the given last string in the command line.
	// returns list of options to complete the last word in the command
	// line, and a boolean indicating if those options should be the
	// only shown options.
	Complete(last string) (options []string, only bool)
}

Completer interface is for the Complete function

type FlagSet

type FlagSet struct {
	*gflag.FlagSet
}

FlagSet is a copy of the standard library FlagSet struct.

func NewFlagSet

func NewFlagSet(name string, h gflag.ErrorHandling) *FlagSet

NewFlagSet creates a new FlagSet

func (*FlagSet) Bool

func (f *FlagSet) Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

func (*FlagSet) BoolVar

func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument s points to a bool variable in which to store the value of the flag.

func (*FlagSet) Choice

func (f *FlagSet) Choice(name string, choices []string, value string, usage string) *string

Choice is a flag that has a choice of possible string values

func (*FlagSet) ChoiceVar

func (f *FlagSet) ChoiceVar(p *string, name string, choices []string, value string, usage string)

ChoiceVar is a Choice with a given pointer

func (*FlagSet) Complete

func (f *FlagSet) Complete() bool

Complete tries to complete the command line according to a FlagSet. It returns true on success, on which the main program should exit.

func (*FlagSet) Dir

func (f *FlagSet) Dir(name string, pattern string, value string, usage string) *string

Dir is a flag the relates to a directory on the filesystem according to a given pattern. For any directory name use "*" as the pattern.

func (*FlagSet) DirVar

func (f *FlagSet) DirVar(p *string, name string, pattern string, value string, usage string)

DirVar is a Dir with a given pointer

func (*FlagSet) File

func (f *FlagSet) File(name string, pattern string, value string, usage string) *string

File is a flag that is related to a file path on the filesystem according to a given pattern. for any file use "*" as the pattern.

func (*FlagSet) FileVar

func (f *FlagSet) FileVar(p *string, name string, pattern string, value string, usage string)

FileVar is a File with a given pointer

func (*FlagSet) SetInstallFlags

func (f *FlagSet) SetInstallFlags(installName, uninstallName string)

SetInstallFlags adds to a flagset install and uninstall flags which invokes installation of bash completion in the user's home folder.

func (*FlagSet) StringCompleter

func (f *FlagSet) StringCompleter(name string, completer Completer, value string, usage string) *string

StringCompleter is a string flag with custom bash completion

func (*FlagSet) StringCompleterVar

func (f *FlagSet) StringCompleterVar(p *string, name string, completer Completer, value string, usage string)

StringCompleterVar is a StringCompleter with a given pointer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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