customtypes

command
v0.0.0-...-56cfde8 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT, MIT Imports: 4 Imported by: 0

README

customtypes example

<tmpl,code=go:cat customtypes.go>

package main

import (
	"fmt"
	"strconv"
	"time"

	"github.com/jpillora/opts"
)

//custom types are allowed if they implement the flag.Value interface
type MagicInt int

func (b MagicInt) String() string {
	return "{" + strconv.Itoa(int(b)) + "}"
}

func (b *MagicInt) Set(s string) error {
	n, err := strconv.Atoi(s)
	if err != nil {
		return err
	}
	*b = MagicInt(n + 42)
	return nil
}

type Config struct {
	Foo  time.Duration
	Bar  MagicInt
	Bazz int
}

func main() {

	c := Config{}

	opts.Parse(&c)

	fmt.Printf("%3.0f %s %d\n", c.Foo.Seconds(), c.Bar, c.Bazz)
}
``` $ customtypes --foo 2m --bar 5 --bazz 5 ``` ``` plain 120 {47} 5 ``` ``` $ customtypes --help ``` ``` plain

Usage: customtypes [options]

Options: --foo, -f --bar, -b --bazz --help, -h

</tmpl>

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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