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

Idiom #162 Execute procedures depending on options

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

  do i=1, command_argument_count ()
     call get_command_argument (i, length=length)
     if (length > len(opt)) then
        deallocate (opt)
        allocate (character(length) :: opt)
     end if
     call get_command_argument (i, opt)
     if (opt(1:1) /= '-') exit
     do j=2, length
        select case (opt(j:j))
        case ('b')
           print *,"bat"
        case ('f')
           print *,"fox"
        end select
     end do
  end do
#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;
}
void Main(string[] args)
{
  if (args.Contains("b")) bat();
  else if (args.Contains("f")) fox();
}
import std.getopt;
void main(string[] args)
{
    void bat(){} void fox(){}
    getopt(args, "b", &bat, "f", &fox);
}
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()
	}
}
function HasOption(c: char): Boolean;
var
  i: integer;
begin
  Result := False;
  for i := 1 to ParamCount do
    if (ParamStr(i) = ('-' + c)) then Exit(True);
end;  

begin
  if HasOption('b') then Bat;
  if HasOption('f') then Fox;
end.
for (@ARGV) {
    use experimental 'switch';
    bat when 'b';
    fox when '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()
bat if ARGV.include?("b")
fox if ARGV.include?("f")
if let Some(arg) = ::std::env::args().nth(1) {
    if &arg == "f" {
        fox();
    } else if &arg = "b" {
        bat();
    } else {
	eprintln!("invalid argument: {}", arg),
    }
} else {
    eprintln!("missing argument");
}

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Bzzzzzzzzzz