-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
executable file
·57 lines (43 loc) · 1.37 KB
/
hello.py
File metadata and controls
executable file
·57 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""Hello, World! A multilingual program.
This program prints out "Hello, World!" in multiple languages using the environment's variable to encoding.
Example:
Set the language by envinronment variable:
export LANG=es_ES.UTF-8
Or by CLI argument:
python hello.py --lang=es_ES
Or the user needs to choose the languag.e
./hello.py
Hello, World!
"""
__version__ = '0.1.3'
__author__ = 'Vicente Marçal'
__license__ = 'GPLv3+'
import os
import sys
arguments = {'lang': None, 'count': 1}
for arg in sys.argv[1:]:
try:
key, value = arg.split('=')
except ValueError as err:
print(f'Error: {err}')
print(f'Argument "{arg}" not recognized.')
print(f'Usage: {sys.argv[0]} --lang=es_ES --count=3')
sys.exit(1)
key = key.lstrip('-').strip()
value = value.strip()
if key not in arguments:
print(f'Argument "{key}" not recognized.')
sys.exit(1)
arguments[key] = value
current_language = arguments['lang'] or os.getenv('LANG')
if not current_language:
current_language = input('No language set. Choose a language: ')
current_language = current_language[:5]
msg = {
'pt_BR': 'Olá, Mundo! ',
'es_ES': '¡Hola, Mundo! ',
'fr_FR': 'Bonjour, le monde! ',
'it_IT': 'Ciao, mondo! ',
}.get(current_language, 'Hello, World! ')
print(msg * int(arguments['count']))