func (o *Options) ParseError() error {
	if !strIn(o.Encoding, FlagValues.Encodings) {
		return fmt.Errorf("encoding '%s' not %s", o.Encoding, FlagValues.Encodings)
	}
	if !strIn(o.Algorithm, FlagValues.Algorithms) {
		return fmt.Errorf("algorithm '%s' not %s", o.Algorithm, FlagValues.Algorithms)
	}
	var found bool
	o.AlgorithmCode, found = mh.Names[o.Algorithm]
	if !found {
		return fmt.Errorf("algorithm '%s' not found (lib error, pls report)", o.Algorithm)
	}
	if o.Length >= 0 {
		if o.Length%8 != 0 {
			return fmt.Errorf("length must be multiple of 8")
		}
		o.Length = o.Length / 8
		h, _ := mh.GetHasher(o.AlgorithmCode)
		hsize := 0
		if h != nil {
			hsize = h.Size()
		}
		if o.Length > hsize {
			o.Length = hsize
		}
	}
	return nil
}
ParseError checks the parsed options for errors.
package opts
import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"io"
	"sort"
	"strings"
	mh "github.com/multiformats/go-multihash"
)
var (
	ErrMatch = errors.New("multihash checksums did not match")
)
type Options struct {
	Encoding      string
	Algorithm     string
	AlgorithmCode uint64
	Length        int
	fs *flag.FlagSet
}
var FlagValues = struct {
	Encodings  []string
	Algorithms []string
}{
	Encodings: []string{"raw", "hex", "base58", "base64"},
	Algorithms: func() []string {
		names := make([]string, 0, len(mh.Names))
		for n := range mh.Names {
			if strings.HasPrefix(n, "blake2") {
				switch n {
				case "blake2s-256":
				case "blake2b-128":
				case "blake2b-224":
				case "blake2b-256":
				case "blake2b-384":
				case "blake2b-512":
				default:
					continue
				}
			}
			names = append(names, n)
		}
		sort.Strings(names)
		return names
	}(),
}
func SetupFlags(f *flag.FlagSet) *Options {
	o := new(Options)
	algoStr := "one of: " + strings.Join(FlagValues.Algorithms, ", ")
	f.StringVar(&o.Algorithm, "algorithm", "sha2-256", algoStr)
	f.StringVar(&o.Algorithm, "a", "sha2-256", algoStr+" (shorthand)")
	encStr := "one of: " + strings.Join(FlagValues.Encodings, ", ")
	f.StringVar(&o.Encoding, "encoding", "base58", encStr)
	f.StringVar(&o.Encoding, "e", "base58", encStr+" (shorthand)")
	lengthStr := "checksums length in bits (truncate). -1 is default"
	f.IntVar(&o.Length, "length", -1, lengthStr)
	f.IntVar(&o.Length, "l", -1, lengthStr+" (shorthand)")
	return o
}
func (o *Options) Parse(args []string) error {
	if err := o.fs.Parse(args); err != nil {
		return err
	}
	return o.ParseError()
}