bash completion written in go + bash completion for go command

Overview

complete

Package complete is everything for bash completion and Go.

The main development is done on the master branch, please follow the link to see an up to dat readme.

The default branch is the v1 branch for backward compatibility with Old libraries and compilers.

Issues
  • Support for COMP_POINT seems to have broken completion…

    Support for COMP_POINT seems to have broken completion…

    
    goroutine 1 [running]:
    .../vendor/github.com/posener/complete.(*Complete).Complete(0xc00038c690, 0x0)
    	.../vendor/github.com/posener/complete/complete.go:67 +0x634
    ...
    panic: runtime error: slice bounds out of range```
    
    on tabbing for completion right after the name of the command. I haven't investigated closer but reverting to `1.1.2` fixes the issue.
    bug 
    opened by metacosm 9
  • Add support for `~` and `..` during file completion

    Add support for `~` and `..` during file completion

    This PR adds support for auto-completion of file paths starting ~ and .. which translate to user home dir

    Signed-off-by: anmolbabu [email protected]

    opened by anmolbabu 9
  • can't find package

    can't find package "github.com/posener/complete/v2/gocomplete"

    I followed the install command, go get -u github.com/posener/complete/v2/gocomplete but got this error

    package github.com/posener/complete/v2/gocomplete: cannot find package "github.com/posener/complete/v2/gocomplete" in any of
    
    opened by wfsly 9
  • Autocomplete subcommands even with nonexistent command

    Autocomplete subcommands even with nonexistent command

    Hey,

    I noticed this bug when helping out with the other PRs. The autocomplete library can autocomplete a subcommand even if the given arguments wouldn't result in a valid command.

    The issue stems from here: https://github.com/posener/complete/blob/master/command.go#L75-L76

    For example nomad job has these subcommands:

    $ nomad job
    This command is accessed by using one of the subcommands below.
    
    Subcommands:
        deployments    List deployments for a job
        dispatch       Dispatch an instance of a parameterized job
        history        Display all tracked versions of a job
        promote        Promote a job's canaries
        revert         Revert to a prior version of the job
        status         Display status information about a job
    

    If I write: $ nomad job nonexistent stat<tab> it will autocomplete which is incorrect.

    It is because the loop iterates till it finds a match, but really should give up if the argument leads to an incorrect subcommand.

    opened by dadgar 7
  • Go 1.11 completion support

    Go 1.11 completion support

    This PR adds go module support for the complete package and also expands the gocomplete completion program for the go command to include completion for go help and go mod.

    This PR also adds some missing flags for go clean.

    opened by AdamSLevy 7
  • Allow restricting completion of flags

    Allow restricting completion of flags

    This PR allows a command to specify that flags should only be completed when a prefix is present. The motivation behind this is to have the initial complation to prefer displaying argument completions and only display flag completions when the user enters "hyphen ".

    opened by dadgar 6
  • Support specifying the completion command

    Support specifying the completion command

    As an alternative to inferring the completion command from the binary, allow the command to be specified.

    This is necessary for using a subcommand as a the completion command.

    opened by pwittrock 6
  • Complete packages names?

    Complete packages names?

    Hello, I am pretty exicted by the "Complete packages names" feature, which is exactly what I'm looking for. I assume it should work on any fully qualified package name that resides inside $GOPATH (which would default to ~/go if $GOPATH is not set).

    Unfortunately, I couldn't make it work: go build github.c:repeat_one: go test github.com/gorilla/mu:repeat_one:

    ...no results. I have tried with and without GOPATH set, even after starting new bash. Any idea?

    enhancement 
    opened by Deleplace 5
  • gocomplete go.mod should say

    gocomplete go.mod should say "module github.com/posener/complete/gocomplete"

    Trying to build gocomplete gives an error:

    go: finding github.com/posener/complete/gocomplete latest
    go: github.com/posener/complete/[email protected]: parsing go.mod: unexpected module path "gocomplete"
    go: error loading module requirements
    
    opened by tv42 5
  • Subcommands that share a name with a parent will not autocomplete

    Subcommands that share a name with a parent will not autocomplete

    The title isn't great. I'll explain. First, thanks for a great library. 🎉

    I have a diff at the bottom of this issue with a fix, but I'm not sure how you want to unit test it so I didn't open a PR. Its a one-line fix.

    The Problem

    We have a CLI with the following two commands:

    • nomad job status
    • nomad status

    They each implement their own arg autocompletion func. However, we noticed that only the outer one was ever being called. i.e. when you did nomad job status <tab>, it was returning the same output for nomad status <tab>. Further confusing: it seemed to call both autocomplete funcs, but only outputted the outer one.

    We expected the behavior to only call the inner one.

    A failing structure is shown below (where each Args PredictFucn outputs a different thing):

    complete.Command{
        Sub: {
            "job": {
                Sub: {
                    "status": {
                        Sub:         {},
                        Flags:       {},
                        GlobalFlags: {},
                        Args:        complete.PredictFunc {...},
                    },
                },
                Flags:       {},
                GlobalFlags: {},
                Args:        nil,
            },
            "status": {
                Sub:         {},
                Flags:       {},
                GlobalFlags: {},
                Args:        complete.PredictFunc {...},
            },
        },
        Flags:       {},
        GlobalFlags: {},
        Args:        nil,
    }
    

    The Solution

    I found the solution is to set only = true in c.predictwhen there is a subcommand found. This properly short-circuits the search. Otherwise, a parent matching command is found and overwrites any results. We also don't want the results merged, since the parent command does something totally different.

    Here is the diff:

    diff --git a/command.go b/command.go
    index eeeb9e0..42b7f43 100644
    --- a/command.go
    +++ b/command.go
    @@ -87,6 +87,7 @@ func (c *Command) predict(a Args) (options []string, only bool) {
            // if a sub command was entered, we won't add the parent command
            // completions and we return here.
            if subCommandFound {
    +         only = true
                    return
            }
    

    We'd love to see this fixed!

    Again, thanks for a great library. We're rolling this out to all HashiCorp tooling and its been great so far. This is the first bug we've ran into.

    bug 
    opened by mitchellh 5
  • Possible CodeCov.io Security Issue

    Possible CodeCov.io Security Issue

    Hello,

    The Travis pipeline uses the CodeCov.io bash uploader on successful builds. The bash uploader was recently involved in a security incident. This is both a heads up as well as a request to consider removing the CodeCov dependency. If CodeCov is still needed would it be feasible to instead use a static known-good copy of the uploader instead of grabbing latest and executing it?

    Thanks!

    opened by paulzerkel 0
  • Bring flag arguments to subcommand?

    Bring flag arguments to subcommand?

    Is there any way for a subcommands' prediction to get its parents flags?

    For example

    adb -s FA81Y1A02186 uninstall com.blabla
    

    -s flag correctly predicts FA81Y1A02186.

    However, if i in the handler want to predict what packages to uninstall, i need the predicted flag value from before. Because i want to query the specific device for packages that are installed. Currently i can't. Maybe i've missed something.

    opened by Arneball 7
  • 'Sub:' and 'Flags:' don't seem to work together.

    'Sub:' and 'Flags:' don't seem to work together.

    var force = flag.String("f", "", "force")
    ...
    
    // Auto completion
    cmd := &complete.Command{
    	Sub: map[string]*complete.Command{
    		"update": {
    			Sub: offPred,
    			Flags: map[string]complete.Predictor{
    				"f": predict.Nothing,
    			},
    		},
    	},
    }
    cmd.Complete("<cmd>")
    

    On the latest version, when I execute this code, only Sub: will get suggested. However, if I remove the Sub: entirely, then Flags: show up. I have the feeling I'm doing it wrong, but I've boiled it down to this, at least.

    PS: I'm using Kong as well, and the -h flag always shows up automatically (not sure if that's Kong or this package), but that flag shows up fine.

    need more info 
    opened by Mexican-Man 2
  • Error when installing to msys2 bash

    Error when installing to msys2 bash

    Thanks for making this lib it's quite useful. Please see the error and potential problem outlined below.

    10:21:07 [email protected] src → bit complete
    install completion for bit? y
    installing...
    regexp: Compile(`^complete -C C:\Users\chrome\go\bin\bit.exe bit$`): error parsing regexp: invalid escape sequence: `\U`
    
    goroutine 1 [running]:
    runtime/debug.Stack(0xf51840, 0xc000006018, 0xc00011cc78)
            C:/Dev/Go/src/runtime/debug/stack.go:24 +0xa5
    github.com/chriswalz/bit/cmd.HandleExit()
            C:/Users/chrome/go/pkg/mod/github.com/chriswalz/[email protected]/cmd/util.go:320 +0xea
    panic(0xe0cfe0, 0xc0000533f0)
            C:/Dev/Go/src/runtime/panic.go:965 +0x1c7
    regexp.MustCompile(0xc000376510, 0x30, 0x1)
            C:/Dev/Go/src/regexp/regexp.go:311 +0x15f
    github.com/chriswalz/complete/v3/install.lineInFile(0xc00001cd38, 0x17, 0xc0003764b0, 0x2e, 0x2)
            C:/Users/chrome/go/pkg/mod/github.com/chriswalz/complete/[email protected]/install/utils.go:12 +0x12c
    github.com/chriswalz/complete/v3/install.bash.IsInstalled(0xc00001cd38, 0x17, 0xea613a, 0x3, 0xc000376450, 0x1e, 0x2)
            C:/Users/chrome/go/pkg/mod/github.com/chriswalz/complete/[email protected]/install/bash.go:15 +0xfc
    github.com/chriswalz/complete/v3/install.bash.Install(0xc00001cd38, 0x17, 0xea613a, 0x3, 0xc000376450, 0x1e, 0x0, 0x0)
            C:/Users/chrome/go/pkg/mod/github.com/chriswalz/complete/[email protected]/install/bash.go:19 +0x77
    github.com/chriswalz/complete/v3/install.Install(0xea613a, 0x3, 0xc00011d350, 0xe)
            C:/Users/chrome/go/pkg/mod/github.com/chriswalz/complete/[email protected]/install/install.go:66 +0xf6
    

    According to this comment the problem is due to:

    1. https://github.com/posener/complete/blob/master/install/bash.go#L36

    2. https://github.com/posener/complete/blob/master/install/bash.go#L15

    3. https://github.com/posener/complete/blob/master/install/utils.go#L12

    The code seems to assume that cmd or bin doesn't contain any regexp characters. The 3. probably should escape all those characters. It's weird that it's using regexps for that in the first place.

    bug help wanted 
    opened by chriswalz 1
  • How to keep default bash filename completion for positional argument?

    How to keep default bash filename completion for positional argument?

    Hi, I really like how easily complete allows to add completion to CLI tools using flag but since I use it, I have lost the classic bash completion on filenames for positional arguments.

    Take this example:

    package main
    
    import (
    	"flag"
    	"fmt"
    	"os"
    
    	"github.com/posener/complete/v2"
    )
    
    func main() {
    	printJSON := flag.Bool("json", false, "prints the filename in JSON")
    
    	complete.CommandLine()
    	flag.Parse()
    
    	if flag.NArg() < 1 {
    		fmt.Printf("File argument is missing\n\n")
    		flag.Usage()
    		os.Exit(1)
    	}
    
    	if *printJSON {
    		fmt.Println("{\"filename\": \"" + flag.Arg(0) + "\"}")
    	} else {
    		fmt.Println("File: " + flag.Arg(0))
    	}
    }
    

    Without the complete.CommandLine() line, I do not get completion for the -json flag but my positional argument is well completed by bash. But with this line, I do not get completion anymore.

    Is there anything to get the file completion behavior back?

    Thank you

    enhancement question 
    opened by romainreignier 3
  • Add support for completing short flags (-x)

    Add support for completing short flags (-x)

    I have a setup like so:

    Flags: map[string]complete.Predictor{
      "o": predict.Nothing,
      "upload-pack": predict.Nothing,
      ...
    }
    

    Now when I type a command like <cmd> -- [TAB] I see the below

    Screen Shot 2020-10-05 at 12 15 48 AM

    I would expect to only see the long flags

    enhancement 
    opened by chriswalz 13
  • I have not been able to get it to work

    I have not been able to get it to work

    The instruction is quite confusing for me. Here is what I did. I wrote a piece of code very similar to the example code on the doc page, which generated a binary let's call "autocomplete", then I run "./autocomplete -install" and install it. Then I run my command as usual, and nothing was different. There has been an added line to my bashrc. Is there anything I am doing wrong?

    question 
    opened by Xichen96 1
  • Add check for autoloading compinit in zsh.go

    Add check for autoloading compinit in zsh.go

    Encountered issue when trying to install autocompletion for terraform on fresh MacOS with default zsh configuration, using their terraform -install-autocomplete command. Raised issue on terraform git and found it imports this module. Cloned zsh.go and added check for the line that autoloads the zsh compinit. Please review, thanks!

    opened by ratticon 1
  • Install without using Flags

    Install without using Flags

    I would prefer to use a completely different approach to exposing the install/uninstall than with the flags package. I'm using a completely different package for building CLI apps, and would like to integrate the output of this framework with that other package by simply Printing/Sprinting the output to wherever the user wants.

    opened by skyzyx 5
Releases(v2.0.1-alpha.13)
Owner
Eyal Posener
Eyal Posener
CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser

CONTRIBUTIONS ONLY What does this mean? I do not have time to fix issues myself. The only way fixes or new features will be added is by people submitt

Alec Thomas 3.1k Jun 10, 2021
Fully featured Go (golang) command line option parser with built-in auto-completion support.

go-getoptions Go option parser inspired on the flexibility of Perl’s GetOpt::Long. Table of Contents Quick overview Examples Simple script Program wit

David Gamba 35 Jun 8, 2021
A Commander for modern Go CLI interactions

Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files. Cobra is used i

Steve Francia 21.9k Jun 13, 2021
Integrated console application library, using Go structs as commands, with menus, completions, hints, history, Vim mode, $EDITOR usage, and more ...

Gonsole - Integrated Console Application library This package rests on a readline console library, (giving advanced completion, hint, input and histor

null 11 May 6, 2021
bash completion written in go + bash completion for go command

complete Package complete is everything for bash completion and Go. The main development is done on the master branch, please follow the link to see a

Eyal Posener 775 Jun 9, 2021
A versatile library for building CLI applications in Go

mow.cli Package cli provides a framework to build command line applications in Go with most of the burden of arguments parsing and validation placed o

Jawher Moussa 739 Jun 14, 2021
The standard library flag package with its missing features

cmd Package cmd is a minimalistic library that enables easy sub commands with the standard flag library. This library extends the standard library fla

Eyal Posener 33 Jun 11, 2021
🔥 [WIP] Interactive Jira Command Line

JiraCLI Interactive Jira CLI ?? This project is still a work in progress ?? This tool mostly focuses on issue search and navigation at the moment. How

Ankit Pokhrel 94 Jun 13, 2021
cod is a completion daemon for bash/fish/zsh

Cod is a completion daemon for {bash,fish,zsh}. It detects usage of --help commands parses their output and generates auto-completions for your shell.

Dmitry Ermolov 318 Jun 10, 2021
Source code editor in pure Go.

Editor Source code editor in pure Go. About This is a simple but advanced source code editor As the editor is being developed, the rules of how the UI

Jorge Miguel Pinto 224 Jun 5, 2021
Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.

go-prompt A library for building powerful interactive prompts inspired by python-prompt-toolkit, making it easier to build cross-platform command line

Masashi Shibata 4k Jun 14, 2021
Kong is a command-line parser for Go

Kong is a command-line parser for Go Introduction Help Help as a user of a Kong application Defining help in Kong Command handling Switch on the comma

Alec Thomas 553 Jun 13, 2021
git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.

Table of contents Introduction Reference Contributing Introduction Overview git-xargs is a command-line tool (CLI) for making updates across multiple

Gruntwork 433 Jun 13, 2021
Bit is a modern Git CLI

bit is an experimental modernized git CLI built on top of git that provides happy defaults and other niceties: command and flag suggestions to help yo

Chris Walz 5.5k Jun 11, 2021