<commit_before>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
	"github.com/GoogleContainerTools/kpt/internal/cmdcomplete"
	"github.com/GoogleContainerTools/kpt/internal/cmddesc"
	"github.com/GoogleContainerTools/kpt/internal/cmdget"
	"github.com/GoogleContainerTools/kpt/internal/cmdinit"
	"github.com/GoogleContainerTools/kpt/internal/cmdman"
	"github.com/GoogleContainerTools/kpt/internal/cmdsync"
	"github.com/GoogleContainerTools/kpt/internal/cmdtutorials"
	"github.com/GoogleContainerTools/kpt/internal/cmdupdate"
	"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
	"github.com/posener/complete/v2"
	"github.com/spf13/cobra"
)

// GetAllCommands returns the set of kpt commands to be registered
func GetAllCommands(name string) []*cobra.Command {
	c := []*cobra.Command{
		cmdcomplete.NewCommand(name),
		cmddesc.NewCommand(name),
		cmdget.NewCommand(name),
		cmdinit.NewCommand(name),
		cmdman.NewCommand(name),
		cmdsync.NewCommand(name),
		cmdupdate.NewCommand(name),
	}
	c = append(c, cmdtutorials.Tutorials(name)...)

	// apply cross-cutting issues to commands
	for i := range c {
		cmd := c[i]
		// check if silencing errors is off
		cmdutil.SetSilenceErrors(cmd)

		// check if stack printing is on
		if cmd.PreRunE != nil {
			fn := cmd.PreRunE
			cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
				err := fn(cmd, args)
				return cmdutil.HandlePreRunError(cmd, err)
			}
		}
		if cmd.RunE != nil {
			fn := cmd.RunE
			cmd.RunE = func(cmd *cobra.Command, args []string) error {
				err := fn(cmd, args)
				return cmdutil.HandleError(cmd, err)
			}
		}
	}
	return c
}

var allCommands = map[string]func(string) *cobra.Command{
	"desc":               cmddesc.NewCommand,
	"get":                cmdget.NewCommand,
	"init":               cmdinit.NewCommand,
	"man":                cmdman.NewCommand,
	"sync":               cmdsync.NewCommand,
	"update":             cmdupdate.NewCommand,
	"install-completion": cmdcomplete.NewCommand,
}

func GetCommands(name string, commands ...string) []*cobra.Command {
	var c []*cobra.Command
	for i := range commands {
		c = append(c, allCommands[commands[i]](name))
	}
	return c
}

// Complete returns a completion command
func Complete(cmd *cobra.Command) *complete.Command {
	return cmdcomplete.Complete(cmd, nil)
}<commit_msg>support specifying flag completions<commit_after>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
	"github.com/GoogleContainerTools/kpt/internal/cmdcomplete"
	"github.com/GoogleContainerTools/kpt/internal/cmddesc"
	"github.com/GoogleContainerTools/kpt/internal/cmdget"
	"github.com/GoogleContainerTools/kpt/internal/cmdinit"
	"github.com/GoogleContainerTools/kpt/internal/cmdman"
	"github.com/GoogleContainerTools/kpt/internal/cmdsync"
	"github.com/GoogleContainerTools/kpt/internal/cmdtutorials"
	"github.com/GoogleContainerTools/kpt/internal/cmdupdate"
	"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
	"github.com/posener/complete/v2"
	"github.com/spf13/cobra"
)

// GetAllCommands returns the set of kpt commands to be registered
func GetAllCommands(name string) []*cobra.Command {
	c := []*cobra.Command{
		cmdcomplete.NewCommand(name),
		cmddesc.NewCommand(name),
		cmdget.NewCommand(name),
		cmdinit.NewCommand(name),
		cmdman.NewCommand(name),
		cmdsync.NewCommand(name),
		cmdupdate.NewCommand(name),
	}
	c = append(c, cmdtutorials.Tutorials(name)...)

	// apply cross-cutting issues to commands
	for i := range c {
		cmd := c[i]
		// check if silencing errors is off
		cmdutil.SetSilenceErrors(cmd)

		// check if stack printing is on
		if cmd.PreRunE != nil {
			fn := cmd.PreRunE
			cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
				err := fn(cmd, args)
				return cmdutil.HandlePreRunError(cmd, err)
			}
		}
		if cmd.RunE != nil {
			fn := cmd.RunE
			cmd.RunE = func(cmd *cobra.Command, args []string) error {
				err := fn(cmd, args)
				return cmdutil.HandleError(cmd, err)
			}
		}
	}
	return c
}

var allCommands = map[string]func(string) *cobra.Command{
	"desc":               cmddesc.NewCommand,
	"get":                cmdget.NewCommand,
	"init":               cmdinit.NewCommand,
	"man":                cmdman.NewCommand,
	"sync":               cmdsync.NewCommand,
	"update":             cmdupdate.NewCommand,
	"install-completion": cmdcomplete.NewCommand,
}

func GetCommands(name string, commands ...string) []*cobra.Command {
	var c []*cobra.Command
	for i := range commands {
		c = append(c, allCommands[commands[i]](name))
	}
	return c
}

// Complete returns a completion command
func Complete(cmd *cobra.Command, fn cmdcomplete.VisitFlags) *complete.Command {
	return cmdcomplete.Complete(cmd, fn)
}
{'fraction_non_alphanumeric': 0.12077294685990338, 'fraction_numerical': 0.003795721187025535, 'mean_word_length': 3.648757016840417, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '768127', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1874, 'n_words': 467}