<commit_before>package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/spf13/cobra"

	"github.com/telepresenceio/telepresence/v2/pkg/client"
	"github.com/telepresenceio/telepresence/v2/pkg/client/cli"
	"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
	"github.com/telepresenceio/telepresence/v2/pkg/client/logging"
	"github.com/telepresenceio/telepresence/v2/pkg/client/rootd"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd/commands"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd/trafficmgr"
	"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
)

func main() {
	ctx := context.Background()
	if dir := os.Getenv("DEV_TELEPRESENCE_CONFIG_DIR"); dir != "" {
		ctx = filelocation.WithAppUserConfigDir(ctx, dir)
	}
	if dir := os.Getenv("DEV_TELEPRESENCE_LOG_DIR"); dir != "" {
		ctx = filelocation.WithAppUserLogDir(ctx, dir)
	}

	env, err := client.LoadEnv(ctx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load environment: %v", err)
		os.Exit(1)
	}
	ctx = client.WithEnv(ctx, env)

	var cmd *cobra.Command
	if isDaemon() {
		// Avoid the initialization of all subcommands except for [connector|daemon]-foreground and
		// avoids checks for legacy commands.
		cmd = &cobra.Command{
			Use:  "telepresence",
			Args: cli.OnlySubcommands,
			RunE: func(cmd *cobra.Command, args []string) error {
				cmd.SetOut(cmd.ErrOrStderr())
				return nil
			},
			SilenceErrors: true, // main() will handle it after .ExecuteContext() returns
			SilenceUsage:  true, // our FlagErrorFunc will handle it
		}
		cmd.AddCommand(userd.Command(commands.GetCommands, []userd.DaemonService{}, []trafficmgr.SessionService{}))
		cmd.AddCommand(rootd.Command())
		if err := cmd.ExecuteContext(ctx); err != nil {
			fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
			os.Exit(1)
		}
	} else {
		cfg, err := client.LoadConfig(ctx)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load config: %v", err)
			os.Exit(1)
		}
		ctx = client.WithConfig(ctx, cfg)
		cmd = cli.Command(ctx)
		if err := cmd.ExecuteContext(ctx); err != nil {
			fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
			if errcat.GetCategory(err) > errcat.NoLogs {
				summarizeLogs(ctx, cmd)
				// If the user gets here, it might be an actual bug that they found, so
				// point them to the `gather-logs` command in case they want to open an
				// issue.
				fmt.Fprintln(cmd.ErrOrStderr(), "If you think you have encountered a bug"+
					", please run `telepresence gather-logs` and attach the "+
					"telepresence_logs.zip to your github issue or create a new one: "+
					"https://github.com/telepresenceio/telepresence/issues/new?template=Bug_report.md .")
			}
			os.Exit(1)
		}
	}
}

func isDaemon() bool {
	const fg = "-foreground"
	a := os.Args
	return len(a) > 1 && strings.HasSuffix(a[1], fg) || len(a) > 2 && strings.HasSuffix(a[2], fg) && a[1] == "help"
}<commit_msg>See to that unsupported CLI flag errors are reported as user errors<commit_after>package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/spf13/cobra"

	"github.com/telepresenceio/telepresence/v2/pkg/client"
	"github.com/telepresenceio/telepresence/v2/pkg/client/cli"
	"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
	"github.com/telepresenceio/telepresence/v2/pkg/client/logging"
	"github.com/telepresenceio/telepresence/v2/pkg/client/rootd"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd/commands"
	"github.com/telepresenceio/telepresence/v2/pkg/client/userd/trafficmgr"
	"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
)

func main() {
	ctx := context.Background()
	if dir := os.Getenv("DEV_TELEPRESENCE_CONFIG_DIR"); dir != "" {
		ctx = filelocation.WithAppUserConfigDir(ctx, dir)
	}
	if dir := os.Getenv("DEV_TELEPRESENCE_LOG_DIR"); dir != "" {
		ctx = filelocation.WithAppUserLogDir(ctx, dir)
	}

	env, err := client.LoadEnv(ctx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load environment: %v", err)
		os.Exit(1)
	}
	ctx = client.WithEnv(ctx, env)

	var cmd *cobra.Command
	if isDaemon() {
		// Avoid the initialization of all subcommands except for [connector|daemon]-foreground and
		// avoids checks for legacy commands.
		cmd = &cobra.Command{
			Use:  "telepresence",
			Args: cli.OnlySubcommands,
			RunE: func(cmd *cobra.Command, args []string) error {
				cmd.SetOut(cmd.ErrOrStderr())
				return nil
			},
			SilenceErrors: true, // main() will handle it after .ExecuteContext() returns
			SilenceUsage:  true, // our FlagErrorFunc will handle it
		}
		cmd.AddCommand(userd.Command(commands.GetCommands, []userd.DaemonService{}, []trafficmgr.SessionService{}))
		cmd.AddCommand(rootd.Command())
		if err := cmd.ExecuteContext(ctx); err != nil {
			fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
			os.Exit(1)
		}
	} else {
		cfg, err := client.LoadConfig(ctx)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load config: %v", err)
			os.Exit(1)
		}
		ctx = client.WithConfig(ctx, cfg)
		cmd = cli.Command(ctx)
		cmd.SetFlagErrorFunc(func(_ *cobra.Command, err error) error {
			return errcat.User.New(err)
		})
		if err := cmd.ExecuteContext(ctx); err != nil {
			fmt.Fprintf(cmd.ErrOrStderr(), "%s: error: %v\n", cmd.CommandPath(), err)
			if errcat.GetCategory(err) > errcat.NoLogs {
				summarizeLogs(ctx, cmd)
				// If the user gets here, it might be an actual bug that they found, so
				// point them to the `gather-logs` command in case they want to open an
				// issue.
				fmt.Fprintln(cmd.ErrOrStderr(), "If you think you have encountered a bug"+
					", please run `telepresence gather-logs` and attach the "+
					"telepresence_logs.zip to your github issue or create a new one: "+
					"https://github.com/telepresenceio/telepresence/issues/new?template=Bug_report.md .")
			}
			os.Exit(1)
		}
	}
}

func isDaemon() bool {
	const fg = "-foreground"
	a := os.Args
	return len(a) > 1 && strings.HasSuffix(a[1], fg) || len(a) > 2 && strings.HasSuffix(a[2], fg) && a[1] == "help"
}
{'fraction_non_alphanumeric': 0.14934540164861806, 'fraction_numerical': 0.006465168902537579, 'mean_word_length': 3.9742765273311895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '204516', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2296, 'n_words': 480}