<commit_before>// Copyright 2021 The Go-Commander Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package commander

import (
	"flag"
	"os"
	"path/filepath"
	"strconv"

	"github.com/posener/complete"
)

// CompleterFrom creates a new tab-completer from the provided command.
func CompleterFrom(cmdr *Command) *complete.Command {
	const user = true
	return makeDefaultCompleter(cmdr, user)
}

func makeDefaultCompleter(cmdr *Command, user bool) *complete.Command {
	o := complete.Command{
		Sub:         make(complete.Commands),
		GlobalFlags: make(complete.Flags),
	}
	for _, sub := range cmdr.Subcommands {
		o.Sub[sub.Name()] = *sub.Complete
	}
	cmdr.Flag.VisitAll(func(f *flag.Flag) {
		k := "-" + f.Name
		switch {
		case user:
			// early circuit-breaker: if the user invoked this function,
			// cmdr.Parent may not be correctly setup.
			// better put that flag as a non-global one.
			o.Flags[k] = complete.PredictNothing
		case cmdr.Parent == nil:
			o.GlobalFlags[k] = complete.PredictNothing
		default:
			o.Flags[k] = complete.PredictNothing
		}
	})

	return &o
}

// completion returns true when [TAB]-completion is requested.
func completion() bool {
	shell := os.Getenv("SHELL")
	if shell == "" {
		return false
	}

	switch name := filepath.Base(shell); name {
	case "bash":
		pt := os.Getenv("COMP_POINT")
		if pt == "" {
			return false
		}
		_, err := strconv.Atoi(pt)
		if err != nil {
			return false
		}
		return true

	case "csh", "tcsh":
		// TODO
	case "fish":
		// TODO
	case "zsh":
		// TODO
	}

	return false
}
<commit_msg>complete: fix uninitialized Flags map<commit_after>// Copyright 2021 The Go-Commander Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package commander

import (
	"flag"
	"os"
	"path/filepath"
	"strconv"

	"github.com/posener/complete"
)

// CompleterFrom creates a new tab-completer from the provided command.
func CompleterFrom(cmdr *Command) *complete.Command {
	const user = true
	return makeDefaultCompleter(cmdr, user)
}

func makeDefaultCompleter(cmdr *Command, user bool) *complete.Command {
	o := complete.Command{
		Sub:         make(complete.Commands),
		Flags:       make(complete.Flags),
		GlobalFlags: make(complete.Flags),
	}
	for _, sub := range cmdr.Subcommands {
		o.Sub[sub.Name()] = *sub.Complete
	}
	cmdr.Flag.VisitAll(func(f *flag.Flag) {
		k := "-" + f.Name
		switch {
		case user:
			// early circuit-breaker: if the user invoked this function,
			// cmdr.Parent may not be correctly setup.
			// better put that flag as a non-global one.
			o.Flags[k] = complete.PredictNothing
		case cmdr.Parent == nil:
			o.GlobalFlags[k] = complete.PredictNothing
		default:
			o.Flags[k] = complete.PredictNothing
		}
	})

	return &o
}

// completion returns true when [TAB]-completion is requested.
func completion() bool {
	shell := os.Getenv("SHELL")
	if shell == "" {
		return false
	}

	switch name := filepath.Base(shell); name {
	case "bash":
		pt := os.Getenv("COMP_POINT")
		if pt == "" {
			return false
		}
		_, err := strconv.Atoi(pt)
		if err != nil {
			return false
		}
		return true

	case "csh", "tcsh":
		// TODO
	case "fish":
		// TODO
	case "zsh":
		// TODO
	}

	return false
}

{'fraction_non_alphanumeric': 0.12893553223388307, 'fraction_numerical': 0.0023988005997001498, 'mean_word_length': 3.2015113350125946, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '859756', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1143, 'n_words': 332}