Skip to content

Use a map to store snitch argument options #9

@fabulousduck

Description

@fabulousduck

This is purely a nice to have as it makes it a lot easier to access options from one general map, and error handling of arguments a lot easier. For example, with a map, checking if an option exists would be something like this:

func doA(a string) {
    fmt.Println(a)
}

func doB(b string) {
    fmt.Println(b)
}	
	
func main() {
	m := map[string]interface{}{
		"a": doA,
		"b": doB,
	}
	
	if val, ok := m[as.Args[1]]; ok {
            val.(func(string))("henlo")
	} else {
            fmt.Println("snitch")
            for k, v := range m { 
                fmt.Printf("\tkey[%s]\n", k, v)
            }
        }
}

This seems like a nice solution, but does bring interfaces and type assertion with it seen in this example.

Let me know what you think, if you like it, i will implement it for all command functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions