Last active
March 6, 2019 05:38
-
-
Save rishabh-ink/6bef8dccaf06867afd880c8f4531fd82 to your computer and use it in GitHub Desktop.
A boilerplate example of a Python CLI program
This file contains 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
# -*- coding: utf-8 -*- | |
import argparse | |
import logging | |
import sys | |
def main(argv=None): | |
argparser = argparse.ArgumentParser( | |
description='A boilerplate greeting program!', | |
) | |
argparser.add_argument( | |
'--version', | |
action='version', | |
version='%(prog)s 0.1.0', | |
) | |
argparser.add_argument( | |
'-v', '--verbose', | |
help='Increase output verbosity (add more flags for more verbosity). E.g. "--verbose" or "-vvv"', | |
action='count', | |
default=0, | |
) | |
argparser.add_argument( | |
'greetings', | |
help='A space separated list of greetings in quotes. E.g. "Good morning" "Have a nice day"', | |
nargs='+', | |
) | |
args = argparser.parse_args(argv) | |
logging.basicConfig( | |
level=logging.DEBUG if args.verbose else logging.INFO, | |
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', | |
) | |
logger = logging.getLogger(__name__) | |
greetings = args.greetings | |
for greeting in greetings: | |
logger.debug(f'Printing greeting, {greeting}…') | |
print(greeting) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment