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
|
#!/usr/bin/python3
import os
import argparse
from pyjokes import pyjokes
def create_argparser():
parser = argparse.ArgumentParser(
description='One line jokes for programmers (jokes as a service)'
)
parser.add_argument(
'-c', '--category',
dest='category',
choices=['neutral', 'adult', 'chuck', 'all'],
default='neutral',
help='Joke category.'
)
parser.add_argument(
'-l', '--language',
dest='language',
choices=['en', 'de', 'es'],
default='en',
help='Joke language.'
)
return parser
def main():
parser = create_argparser()
try:
args = parser.parse_args()
except argparse.ArgumentError as exc:
print('Error parsing arguments.')
parser.error(str(exc.message))
exit(-1)
try:
joke = pyjokes.get_joke(language=args.language, category=args.category)
except pyjokes.LanguageNotFoundError:
print('No such language %s' % args.language)
exit(-1)
except pyjokes.CategoryNotFoundError:
print('No such category %s' % args.category)
exit(-1)
print(joke)
if __name__ == '__main__':
main()
|