<commit_before>package command

import (
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
)

var (
	// global flags variables
	flagWatchEvents bool
	flagKloudAddr   string
	flagUsername    string
)

type Flag struct {
	*flag.FlagSet
	name     string
	synopsis string
	action   Actioner
}

func NewFlag(name, synopsis string) *Flag {
	flagSet := flag.NewFlagSet(name, flag.PanicOnError)
	flagSet.SetOutput(ioutil.Discard)

	// global subcommand flags
	flagSet.StringVar(&flagUsername, "user", "koding", "Respective use of the given machine id")
	flagSet.StringVar(&flagKloudAddr, "kloud-addr", "http://127.0.0.1:5500/kite",
		"Kloud addr to connect")
	flagSet.BoolVar(&flagWatchEvents, "watch", false, "Watch the events coming by.")

	f := &Flag{
		name:     name,
		synopsis: synopsis,
	}

	f.FlagSet = flagSet
	return f
}

func (f *Flag) Synopsis() string { return f.synopsis }

func (f *Flag) Help() string {
	help := fmt.Sprintf("usage: kloudctl %s [<args>]\n\n", f.name)
	help += f.synopsis + "\n\n"
	f.VisitAll(func(fl *flag.Flag) {
		format := "  -%s=%s: %s\n"
		help += fmt.Sprintf(format, fl.Name, fl.DefValue, fl.Usage)
	})

	help += "\n"

	return help
}

func (f *Flag) NumberOfFlags() int {
	count := 0
	f.VisitAll(func(fl *flag.Flag) {
		count++
	})

	return count
}

func (f *Flag) ParseArgs(args []string) error {
	// If there are some flags defined and the user didnt' provide any flag
	// let us report it
	if f.NumberOfFlags() != 0 && len(args) == 0 {
		fmt.Print(f.Help())
		return errors.New("no arguments")
	}

	err := f.Parse(args)
	if err != nil {
		DefaultUi.Error(err.Error())
		return err
	}

	return nil
}

func (f *Flag) Run(args []string) int {
	err := f.ParseArgs(args)
	if err != nil {
		return 1
	}

	err = kloudWrapper(args, f.action)
	if err != nil {
		return 1
	}

	return 0
}
<commit_msg>kloudctl: fix flag check for ping<commit_after>package command

import (
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
)

var (
	// global flags variables
	flagWatchEvents bool
	flagKloudAddr   string
	flagUsername    string
)

type Flag struct {
	*flag.FlagSet
	name     string
	synopsis string
	action   Actioner

	totalDefaultFlag int
}

func NewFlag(name, synopsis string) *Flag {
	flagSet := flag.NewFlagSet(name, flag.PanicOnError)
	flagSet.SetOutput(ioutil.Discard)

	// global subcommand flags
	flagSet.StringVar(&flagUsername, "user", "koding", "Respective use of the given machine id")
	flagSet.StringVar(&flagKloudAddr, "kloud-addr", "http://127.0.0.1:5500/kite",
		"Kloud addr to connect")
	flagSet.BoolVar(&flagWatchEvents, "watch", false, "Watch the events coming by.")

	f := &Flag{
		name:     name,
		synopsis: synopsis,
	}

	f.FlagSet = flagSet
	f.totalDefaultFlag = f.NumberOfFlags()
	return f
}

func (f *Flag) Synopsis() string { return f.synopsis }

func (f *Flag) Help() string {
	help := fmt.Sprintf("usage: kloudctl %s [<args>]\n\n", f.name)
	help += f.synopsis + "\n\n"
	f.VisitAll(func(fl *flag.Flag) {
		format := "  -%s=%s: %s\n"
		help += fmt.Sprintf(format, fl.Name, fl.DefValue, fl.Usage)
	})

	help += "\n"

	return help
}

func (f *Flag) NumberOfFlags() int {
	count := 0
	f.VisitAll(func(fl *flag.Flag) {
		count++
	})

	return count
}

func (f *Flag) ParseArgs(args []string) error {
	// If there are some flags defined and the user didnt' provide any flag
	// let us report it
	cmdFlagCount := f.NumberOfFlags() - f.totalDefaultFlag

	if cmdFlagCount > len(args) {
		fmt.Print(f.Help())
		return errors.New("no arguments")
	}

	err := f.Parse(args)
	if err != nil {
		DefaultUi.Error(err.Error())
		return err
	}

	return nil
}

func (f *Flag) Run(args []string) int {
	err := f.ParseArgs(args)
	if err != nil {
		return 1
	}

	err = kloudWrapper(args, f.action)
	if err != nil {
		return 1
	}

	return 0
}

{'fraction_non_alphanumeric': 0.14807897545357523, 'fraction_numerical': 0.0080042689434365, 'mean_word_length': 3.555285540704739, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6074478', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1415, 'n_words': 382}