compflag

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 7 Imported by: 2

README

PkgGoDev License Go Version Tag

CI Go Report Card Maintainability Test Coverage Issues

compflag

Auto-completion for stdlib flag items

usage

you got two options here:

  • Call compflag.Complete() somewhere before actual app logic, best point is right at the start:
    package main

    import (
        "os"
        "flag"

        "github.com/s0rg/compflag"
    )

    func main() {
        if compflag.Complete() {
            os.Exit(0)
        }

        flag.Parse()

        // other startup logic...
    }
  • Define trigger flag for completion:
    package main

    import (
        "os"
        "flag"

        "github.com/s0rg/compflag"
    )

    func main() {
	    compflag.Var("complete")

        flag.Parse()

        // other startup logic...
    }

Please note, that you need to exit app if any completion happened.

Build your app, put binary somewhere in your "PATH", then run:

    complete -C %your-binary-name% %your-binary-name%

if you prefer flag-triggered version:

    complete -C "%your-binary-name% -%your-flag% bash" %your-binary-name%

Now enter %your-binary-name%, and hit TAB twice )

shell compatability

This will work with any shell compatible with complete (bash and zsh are both good with it), for zsh you may need also use bashcompinit in addition to compinit.

Documentation

Overview

Package compflag generates bash completions on-the-fly from stdlib flag values.

Usage - just call compflag.Complete() somewhere before actual app logic, best point is right at the start:

func main() {
  if compflag.Complete() {
    os.Exit(0)
  }

  flag.Parse()

  // other startup logic...
}

Please note, that you need to exit app if any completion happened.

Build your app, put binary somewhere in your "PATH", then run:

complete -C %your-binary-name% %your-binary-name%

Now enter "%your-binary-name%", and hit tab twice )

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownShell = errors.New("shell unknown")

Functions

func Complete

func Complete(opts ...Option) (ok bool)

Complete handles completion process started by shell, returns false if no completion was requested.

func Var added in v1.1.0

func Var(name string, opts ...Option)

Types

type Option

type Option func(*options)

Option is a functional option type.

func WithExitFunc added in v1.1.0

func WithExitFunc(fn func(int)) Option

WithExitFunc set custom exit handler on successful complete.

func WithFlagSet

func WithFlagSet(fset *flag.FlagSet) Option

WithFlagSet set custom flag.FlagSet as completion source.

func WithHidden

func WithHidden(names ...string) Option

WithHidden hides given options from completion.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter set custom output for completion.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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