Logo

Programming-Idioms

Print the value of the flag -country passed to the program command line, or the default value "Canada" if no such flag was passed.
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
import "flag"
var country = flag.String("country", "Canada", "user home country")
flag.Parse()
fmt.Println("country is", *country)
import static java.lang.System.out;
public static void main(String[] args) {
    String s;
    int i, n = args.length;
    for (i = 0; i < n; ++i)
        if (args[i].equals("-country"))
            break;
    s = ++i < n ? args[i] : "Canada";
    out.println(s);
}
uses CustApp;
S := Application.GetOptionValue('country');
if S = '' then S := 'Canada';
writeln('Country = ',S);
use Getopt::Long;
my $country = 'Canada';
GetOptions("country=s" => \$country) or die("Error in command line args\n");
print "Country is $country\n";
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-country', default='Canada', dest='country')
args = parser.parse_args()
print('country is', args.country)
from sys import argv
try:
    i = argv.index('-country')
    print(argv[i + 1])
except:
    print('Canada')