iecbyte

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 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.

Command line flag values may be as either a plain, non-negative integer or as a positive fixed-point number using one of these quantity suffixes: Ei, Pi, Ti, Gi, Mi, Ki.

For example the following all represent the same value:

  • 2147483648
  • 2097152Ki
  • 2048Mi
  • 2Gi

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

View Source
const (
	Byte     uint64
	Kilobyte = uint64(1 << (10 * (iota - 1)))
	Megabyte
	Gigabyte
	Terabyte
	Petabyte
	Exabyte
)

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 uint64) Flag

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

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() uint64

Get returns the value of the Flag as an int64

Example
package main

import (
	"fmt"

	"github.com/andrewheberle/iecbyte"
)

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

	fmt.Println(size)
	fmt.Println(size.Get())
}
Output:
1Mi
1048576

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