Logo

Programming-Idioms

  • Rust
  • C#

Idiom #162 Execute procedures depending on options

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

void Main(string[] args)
{
  if (args.Contains("b")) bat();
  else if (args.Contains("f")) fox();
}
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");
}
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");
}
#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