Skip to content

How to keep default bash filename completion for positional argument? #131

@romainreignier

Description

@romainreignier

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions