This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- C
- Clojure
- C#
- D
- Dart
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Pascal
- Perl
- Python
- Python
- Python
- Python
- Ruby
- Rust
- Rust
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)
{
void bat(){} void fox(){}
getopt(args, "b", &bat, "f", &fox);
}
std.getopt allows to associate functions to arguments.
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
This implements standard Unix conventions, so -bf is equivalent to -b -f. i, j and length are integers, opt is a character(len=:), allocatable .
do
args <- getArgs
when ("b" `elem` args) bat
when ("f" `elem` args) fox
This assumes that bat and fox are of type IO ().
const args = process.argv.slice(2)
if (args.includes('b')) bat()
else if (args.includes('f')) fox()
import static java.util.List.of;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class X {
void bat() {}
void fox() {}
public static void main(String[] args) {
List<String> s = of(args);
interface F { void set(); }
Map<String, F> m
= new LinkedHashMap<>();
X x = new X();
m.put("b", x::bat);
m.put("f", x::fox);
for (Entry e : m.entrySet())
if (s.contains(e.getKey()))
((F) e.getValue()).set();
}
}
"... The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program."
—https://docs.oracle.com/javase/tutorial/getStarted/application/index.html#MAIN
—https://docs.oracle.com/javase/tutorial/getStarted/application/index.html#MAIN
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");
}