Logo

Programming-Idioms

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

Idiom #232 Read a command line boolean flag

Print "verbose is true" if the flag -v was passed to the program command line, "verbose is false" otherwise.

use clap::{Arg, App};
let matches = App::new("My Program")
                .arg(Arg::with_name("verbose")
                    .short("v")
                    .takes_value(false))
                .get_matches();
                   
if matches.is_present("verbose") {
    println!("verbose is true")
} else {
    println!("verbose is false")
}
print("verbose is ${args.contains("-v")}");
import "flag"
var verbose = flag.Bool("v", false, "verbose")
flag.Parse()
fmt.Println("verbose is", *verbose)
const verbose = process.argv.includes('-v');
console.log('verbose is', verbose);
import static java.lang.System.out;
public static void main(String[] args) {
    boolean b = false;
    for (String s : args)
        if (s.equals("-v")) {
            b = true;
            break;
        }
    out.printf("verbose is %s", b);
}
for i in paramcount do
begin
  if paramstr(i)='-v' then 
  begin
    verbose := true;
    break;
  end;
 writeln('verbose is ',verbose);
end.
#!/usr/bin/perl -s

use strict;
use warnings;
use vars qw($v);

$v ||= 0;

print 'verbose is ' . ($v ? 'true' : 'false') . "\n";
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', action='store_true', dest='verbose')
args = parser.parse_args()
print('verbose is', args.verbose)

New implementation...
< >
programming-idioms.org