Ejemplo n.º 1
0
// license that can be found in the LICENSE file.

// These examples demonstrate more intricate uses of the flag package.
package flag_test

import (
	"errors"
	"fmt"
	"strings"
	"time"

	"github.com/EricLagergren/go-coreutils/internal/flag"
)

// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")

// Example 2: A flag with a shorthand letter.
var gopherType = flag.StringP("gopher_type", "g", "pocket", "the variety of gopher")

// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration

// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
	return fmt.Sprint(*i)
}

func (i *interval) Type() string {
	return "interval"
Ejemplo n.º 2
0
	// For pretty printing.
	numberWidth int
	printOne    bool

	// For getFileStatus.
	errNoStat = errors.New("no stat")

	// Our cli args
	printLines      = flag.BoolP("lines", "l", false, "print the newline counts")
	printWords      = flag.BoolP("words", "w", false, "print the word counts")
	printChars      = flag.BoolP("chars", "m", false, "print the character counts")
	printBytes      = flag.BoolP("bytes", "c", false, "print the byte counts")
	printLineLength = flag.BoolP("max-line-length", "L", false, "print the length of the longest line")
	filesFrom       = flag.String("files0-from", "", `read input from the files specified by
                             NUL-terminated names in file F;
                             If F is - then read names from standard input`)
	tabWidth     = flag.Int64P("tab", "t", 8, "change the tab width")
	constVersion = flag.BoolP("unicode-version", "u", false, "display unicode version and exit")

	logger = log.New(os.Stderr, "", 0)
)

func fatal(format string, v ...interface{}) {
	logger.Fatalf("%s: %s\n", flag.Program, fmt.Sprintf(format, v...))
}

func print(format string, v ...interface{}) {
	logger.Printf("%s: %s\n", flag.Program, fmt.Sprintf(format, v...))
}