Skip to content
Snippets Groups Projects
Commit 7d4d5612 authored by Hans Moog's avatar Hans Moog
Browse files

Feat: added bool parameters to the parameter package

parent 7548e4bc
No related branches found
No related tags found
No related merge requests found
...@@ -5,16 +5,23 @@ import ( ...@@ -5,16 +5,23 @@ import (
) )
var Events = struct { var Events = struct {
AddBool *events.Event
AddInt *events.Event AddInt *events.Event
AddString *events.Event AddString *events.Event
}{ }{
events.NewEvent(intParameterCaller), AddBool: events.NewEvent(boolParameterCaller),
events.NewEvent(stringParameterCaller), AddInt: events.NewEvent(intParameterCaller),
AddString: events.NewEvent(stringParameterCaller),
}
func boolParameterCaller(handler interface{}, params ...interface{}) {
handler.(func(*BoolParameter))(params[0].(*BoolParameter))
} }
func intParameterCaller(handler interface{}, params ...interface{}) { func intParameterCaller(handler interface{}, params ...interface{}) {
handler.(func(*IntParameter))(params[0].(*IntParameter)) handler.(func(*IntParameter))(params[0].(*IntParameter))
} }
func stringParameterCaller(handler interface{}, params ...interface{}) { func stringParameterCaller(handler interface{}, params ...interface{}) {
handler.(func(*StringParameter))(params[0].(*StringParameter)) handler.(func(*StringParameter))(params[0].(*StringParameter))
} }
package parameter package parameter
var boolParameters = make(map[string]*BoolParameter)
func AddBool(name string, defaultValue bool, description string) *BoolParameter {
if boolParameters[name] != nil {
panic("duplicate parameter - \"" + name + "\" was defined already")
}
newParameter := &BoolParameter{
Name: name,
DefaultValue: defaultValue,
Value: &defaultValue,
Description: description,
}
boolParameters[name] = newParameter
Events.AddBool.Trigger(newParameter)
return newParameter
}
func GetBool(name string) *BoolParameter {
return boolParameters[name]
}
func GetBools() map[string]*BoolParameter {
return boolParameters
}
var intParameters = make(map[string]*IntParameter) var intParameters = make(map[string]*IntParameter)
func AddInt(name string, defaultValue int, description string) *IntParameter { func AddInt(name string, defaultValue int, description string) *IntParameter {
...@@ -32,7 +61,7 @@ func GetInts() map[string]*IntParameter { ...@@ -32,7 +61,7 @@ func GetInts() map[string]*IntParameter {
var stringParameters = make(map[string]*StringParameter) var stringParameters = make(map[string]*StringParameter)
func AddString(name string, defaultValue string, description string) *StringParameter { func AddString(name string, defaultValue string, description string) *StringParameter {
if intParameters[name] != nil { if stringParameters[name] != nil {
panic("duplicate parameter - \"" + name + "\" was defined already") panic("duplicate parameter - \"" + name + "\" was defined already")
} }
......
package parameter package parameter
type BoolParameter struct {
Name string
Value *bool
DefaultValue bool
Description string
}
type IntParameter struct { type IntParameter struct {
Name string Name string
Value *int Value *int
......
...@@ -7,6 +7,10 @@ import ( ...@@ -7,6 +7,10 @@ import (
"path/filepath" "path/filepath"
) )
func AddBoolParameter(p *bool, name string, usage string) {
flag.BoolVar(p, name, *p, usage)
}
func AddIntParameter(p *int, name string, usage string) { func AddIntParameter(p *int, name string, usage string) {
flag.IntVar(p, name, *p, usage) flag.IntVar(p, name, *p, usage)
} }
......
...@@ -10,6 +10,12 @@ import ( ...@@ -10,6 +10,12 @@ import (
"github.com/iotaledger/goshimmer/packages/parameter" "github.com/iotaledger/goshimmer/packages/parameter"
) )
func onAddBoolParameter(param *parameter.BoolParameter) {
flagName := strings.Replace(strings.Replace(strings.ToLower(param.Name), "/", "-", 1), "_", "-", -1)
AddBoolParameter(param.Value, flagName, param.Description)
}
func onAddIntParameter(param *parameter.IntParameter) { func onAddIntParameter(param *parameter.IntParameter) {
flagName := strings.Replace(strings.Replace(strings.ToLower(param.Name), "/", "-", 1), "_", "-", -1) flagName := strings.Replace(strings.Replace(strings.ToLower(param.Name), "/", "-", 1), "_", "-", -1)
...@@ -23,6 +29,10 @@ func onAddStringParameter(param *parameter.StringParameter) { ...@@ -23,6 +29,10 @@ func onAddStringParameter(param *parameter.StringParameter) {
} }
func init() { func init() {
for _, param := range parameter.GetBools() {
onAddBoolParameter(param)
}
for _, param := range parameter.GetInts() { for _, param := range parameter.GetInts() {
onAddIntParameter(param) onAddIntParameter(param)
} }
...@@ -31,6 +41,7 @@ func init() { ...@@ -31,6 +41,7 @@ func init() {
onAddStringParameter(param) onAddStringParameter(param)
} }
parameter.Events.AddBool.Attach(events.NewClosure(onAddBoolParameter))
parameter.Events.AddInt.Attach(events.NewClosure(onAddIntParameter)) parameter.Events.AddInt.Attach(events.NewClosure(onAddIntParameter))
parameter.Events.AddString.Attach(events.NewClosure(onAddStringParameter)) parameter.Events.AddString.Attach(events.NewClosure(onAddStringParameter))
...@@ -49,7 +60,7 @@ func configure(ctx *node.Plugin) { ...@@ -49,7 +60,7 @@ func configure(ctx *node.Plugin) {
fmt.Println(" \\ `--.| |_| | | | | . . || . . || |__ | |_/ /") fmt.Println(" \\ `--.| |_| | | | | . . || . . || |__ | |_/ /")
fmt.Println(" `--. \\ _ | | | | |\\/| || |\\/| || __|| / ") fmt.Println(" `--. \\ _ | | | | |\\/| || |\\/| || __|| / ")
fmt.Println(" /\\__/ / | | |_| |_| | | || | | || |___| |\\ \\ ") fmt.Println(" /\\__/ / | | |_| |_| | | || | | || |___| |\\ \\ ")
fmt.Println(" \\____/\\_| |_/\\___/\\_| |_/\\_| |_/\\____/\\_| \\_| fullnode 1.0") fmt.Println(" \\____/\\_| |_/\\___/\\_| |_/\\_| |_/\\____/\\_| \\_| fullnode 0.0.1")
fmt.Println() fmt.Println()
ctx.Node.LogInfo("Node", "Loading plugins ...") ctx.Node.LogInfo("Node", "Loading plugins ...")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment