Browse Source

Refactor of the flags

pull/646/head
Augusto 5 years ago committed by xpetit
parent
commit
d6194a2a33
  1. 76
      go/tests/rc/rc.go

76
go/tests/rc/rc.go

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"go/ast" "go/ast"
"go/parser" "go/parser"
@ -16,6 +17,62 @@ const (
identation = " " identation = " "
) )
type strBoolMap map[string]bool
func (a *strBoolMap) String() string {
var res string
for k, _ := range *a {
res += k
}
return res
}
func (a *strBoolMap) Set(str string) error {
if *a == nil {
*a = make(map[string]bool)
}
s := strings.Split(str, " ")
for _, v := range s {
(*a)[v] = true
}
return nil
}
type arrFlag struct {
active bool
content []string
}
func (a *arrFlag) String() string {
return strings.Join(a.content, " ")
}
func (a *arrFlag) Set(s string) error {
a.active = true
a.content = strings.Split(s, " ")
return nil
}
// flag that groups a boolean value and a regular expression
type regexpFlag struct {
active bool
reg *regexp.Regexp
}
func (r *regexpFlag) String() string {
if r.reg != nil {
return r.reg.String()
}
return ""
}
func (r *regexpFlag) Set(s string) error {
re := regexp.MustCompile(s)
r.active = true
r.reg = re
return nil
}
var ( var (
allowedImp map[string]map[string]bool // Map of the allowed imports allowedImp map[string]map[string]bool // Map of the allowed imports
allowedFun map[string]bool // Map of the allowed built-in functions allowedFun map[string]bool // Map of the allowed built-in functions
@ -41,6 +98,13 @@ var (
allImports map[string]bool allImports map[string]bool
openImports []string openImports []string
funcOccurrences map[string]int funcOccurrences map[string]int
// Flags
noArrays bool
noRelativeImports bool
noTheseArrays strBoolMap
casting bool
noFor bool
noLit regexpFlag
) )
// pkgFunc for all the functions of a given package // pkgFunc for all the functions of a given package
@ -229,6 +293,18 @@ func allowedAmount(occurrences map[string]int, allowedImports []string) {
} }
} }
func init() {
flag.Var(&noTheseArrays, "no-these-arrays", "unallowes the array types passed in the flag")
flag.Var(&noLit, "no-lit",
`The use of string literals matching the pattern --no-lit="{PATTERN}"`+
`passed to the program would not be allowed`,
)
flag.BoolVar(&noRelativeImports, "no-relative-imports", false, `No disallowes the use of relative imports`)
flag.BoolVar(&noFor, "no-for", false, `The "for" instruction is not allowed`)
flag.BoolVar(&casting, "cast", false, "allowes casting")
flag.BoolVar(&noArrays, "no-arrays", false, "unallowes the array types passed in the flag")
}
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Println("No file or directory") fmt.Println("No file or directory")

Loading…
Cancel
Save