Logo

Programming-Idioms

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

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#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;
}
(defn -main [arg & _]
  (case arg
    "b" (bat)
    "f" (fox)
    (nothing)))
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);
}
if (args.contains("b")) bat();
if (args.contains("f")) fox();
  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
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()
	}
}
import System.Environment (getArgs)
import Control.Monad (when)
do
  args <- getArgs
  when ("b" `elem` args) bat
  when ("f" `elem` args) fox
const args = process.argv.slice(2)
if (args.includes('b')) bat()
else if (args.includes('f')) fox()
import java.util.Set;
var argsSet = Set.of(args);
if (argsSet.contains("b")) bat();
if (argsSet.contains("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");
}
if let Some(arg) = ::std::env::args().nth(1) {
    match arg.as_str() {
        "f" => fox(),
        "b" => box(),
        _ => eprintln!("invalid argument: {}", arg),
    };
} else {
    eprintln!("missing argument");
}