Logo

Programming-Idioms

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

Idiom #162 Execute procedures depending on options

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

from sys import argv
a = dict.fromkeys(argv[1:])
for x in a.keys():
    match x:
        case 'b': bat()
        case 'f': fox()
from sys import argv
s = argv[1:]
for x, f in (('b', bat), ('f', fox)):
    if x in s: f()
import sys
if 'b' in sys.argv[1:]: bat()
if 'f' in sys.argv[1:]: fox()
options = {
	'b': bat
	'f': fox
}

for option, function in options:
	if option in sys.argv[1:]:
		function()
#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