Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #162 Execute procedures depending on options

execute bat if b is a program option and fox if f is a program option.

import "flag"
var b = flag.Bool("b", false, "Do bat")
var f = flag.Bool("f", false, "Do fox")

func main() {
	flag.Parse()
	if *b {
		bar()
	}
	if *f {
		fox()
	}
}

It is idiomatic to use package flag, and pass options as -b -f
(with a dash).
#include <unistd.h>
int main(int argc, char * argv[])
{
        int optch;
        while ((optch = getopt(argc, argv, "bf")) != -1) {
                switch (optch) {
                        case 'b': bat(); break;
                        case 'f': fox(); break;
                }
        }
        return 0;
}

New implementation...
Bzzzzzzzzzz