snitch icon indicating copy to clipboard operation
snitch copied to clipboard

Use a map to store snitch argument options

Open fabulousduck opened this issue 7 years ago • 3 comments

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.

fabulousduck avatar Sep 20 '18 09:09 fabulousduck

@fabulousduck when the PR is merged all of the TODOs it contained automatically approved. Basically, I review not only the code, but also the future issues it may introduce. And if I don't approve a particular issue I'll just simply ask to remove the TODO on the review. ;)

rexim avatar Sep 20 '18 12:09 rexim

so just for confirmation, you agree this is a good idea ?

fabulousduck avatar Sep 20 '18 15:09 fabulousduck

@fabulousduck it sounds ok to me

rexim avatar Sep 20 '18 15:09 rexim