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.

for i in paramcount do
begin
  if paramstr(i)='-v' then 
  begin
    verbose := true;
    break;
  end;
 writeln('verbose is ',verbose);
end.
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);
#!/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)
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")
}

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