Last active
August 29, 2015 14:23
-
-
Save inky/10a14ceecb1c41d7c7c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import string | |
import sys | |
CHARS = ("๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", | |
"๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ") | |
OFFSET = ord('A') | |
def flag(code): | |
""" | |
Return the Unicode flag emoji for a two-letter country code. | |
""" | |
if len(code) != 2: | |
raise ValueError('country code must be two letters long') | |
if not all(c in string.ascii_letters for c in code): | |
raise ValueError('country code must only contain letters A-Z') | |
return ''.join(CHARS[ord(c) - OFFSET] for c in code.upper()) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('country_code', nargs='*', | |
help='a two-letter country code (ISO 3166-1)') | |
parser.add_argument('-s', '--sep', help='separator') | |
args = parser.parse_args() | |
if args.country_code: | |
flags = (flag(code) for code in args.country_code) | |
else: | |
parser.print_help() | |
return 1 | |
sep = ' ' if args.sep is None else args.sep | |
print(sep.join(flags)) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment