-
Notifications
You must be signed in to change notification settings - Fork 13
/
flags.go
33 lines (27 loc) · 846 Bytes
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package importas
import (
"errors"
"flag"
"fmt"
"strings"
)
var errWrongAlias = errors.New("import flag must be of form path:alias")
func flags(config *Config) flag.FlagSet {
fs := flag.FlagSet{}
fs.Var(&config.RequiredAlias, "alias", "required import alias in form path:alias")
fs.BoolVar(&config.DisallowUnaliased, "no-unaliased", false, "do not allow unaliased imports of aliased packages")
fs.BoolVar(&config.DisallowExtraAliases, "no-extra-aliases", false, "do not allow non-required aliases")
return fs
}
type aliasList [][]string
func (v *aliasList) Set(val string) error {
lastColon := strings.LastIndex(val, ":")
if lastColon <= 1 {
return errWrongAlias
}
*v = append(*v, []string{val[:lastColon], val[lastColon+1:]})
return nil
}
func (v *aliasList) String() string {
return fmt.Sprintf("%v", ([][]string)(*v))
}