iecbyte

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 3 Imported by: 0

README

iecbyte Go Report Card GoDoc

This package provides a Flag type that can be used as a custom flag for flag and github.com/sp13/pflag as it satisifes the flag.Value and pflag.Value interfaces.

Example

package main

import (
    "flag"

    "github.com/andrewheberle/iecbyte"
)

func main() {
	size := iecbyte.NewFlag(1024 * 1024)

	flag.Var(&size, "size", "Size in IEC bytes")
	flag.Parse()

	fmt.Printf("Size is %s\n", size)
	// Output: Size is 1Mi
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag struct {
	// contains filtered or unexported fields
}

Flag satisfies the flag.Value and pflag.Value interfaces

func NewFlag

func NewFlag(n int64) Flag

NewFlag is used to initialise a new iecbyte.Flag with a default value

A value of n that is < 0 will be set to 0

Example
package main

import (
	"flag"
	"fmt"

	"github.com/andrewheberle/iecbyte"
)

func main() {
	size := iecbyte.NewFlag(1024 * 1024)

	flag.Var(&size, "size", "Size in IEC bytes")
	//
	// In this example flag.Parse() is commented out as this forms part of the tests
	// of this module, so parsing the command line flags is disabled.
	//
	// In a real program you would need to call flag.Parse()
	//
	// flag.Parse()

	fmt.Printf("Size is %s\n", size)
}
Output:
Size is 1Mi

func (Flag) Get

func (f Flag) Get() int64

Get returns the value of the Flag as an int64

func (*Flag) Set

func (f *Flag) Set(value string) error

func (Flag) String

func (f Flag) String() string

func (Flag) Type

func (f Flag) Type() string

Type returns a user facing type for this flag and is required to satisfy the pflag.Value interface

Example
package main

import (
	"os"

	"github.com/andrewheberle/iecbyte"
	"github.com/spf13/pflag"
)

func main() {
	// This example doesn't explicitly show the calling of Type(),
	// however the pflag package calls Type() as part of it's display
	// of command line flag defaults that is being manually called below.

	size := iecbyte.NewFlag(1024 * 1024)

	fs := pflag.NewFlagSet("example", pflag.ExitOnError)
	fs.SetOutput(os.Stdout)
	fs.Var(&size, "size", "Size in IEC bytes")
	fs.Parse([]string{})
	fs.PrintDefaults()
}
Output:
		--size bytes (IEC)   Size in IEC bytes (default 1Mi)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL