github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/flag/bitflags.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package flag 16 17 import ( 18 "errors" 19 "fmt" 20 21 "github.com/hashicorp/errwrap" 22 ) 23 24 // bitFlags is a flag value type supporting a csv list of options stored as bits 25 type bitFlags struct { 26 *OptionList 27 flags int 28 flagMap map[string]int 29 } 30 31 func newBitFlags(permissibleOptions []string, defaultOptions string, flagMap map[string]int) (*bitFlags, error) { 32 ol, err := NewOptionList(permissibleOptions, defaultOptions) 33 if err != nil { 34 return nil, err 35 } 36 37 bf := &bitFlags{ 38 OptionList: ol, 39 flagMap: flagMap, 40 } 41 bf.typeName = "bitFlags" 42 43 if err := bf.Set(defaultOptions); err != nil { 44 return nil, errwrap.Wrap(errors.New("problem setting defaults"), err) 45 } 46 47 return bf, nil 48 } 49 50 func (bf *bitFlags) Set(s string) error { 51 if err := bf.OptionList.Set(s); err != nil { 52 return err 53 } 54 bf.flags = 0 55 for _, o := range bf.Options { 56 if b, ok := bf.flagMap[o]; ok { 57 bf.flags |= b 58 } else { 59 return fmt.Errorf("couldn't find flag for %v", o) 60 } 61 } 62 return nil 63 } 64 65 func (bf *bitFlags) hasFlag(f int) bool { 66 return (bf.flags & f) == f 67 }