Skip to content

Instantly share code, notes, and snippets.

@rishabh-ink
Last active March 6, 2019 05:38
Show Gist options
  • Save rishabh-ink/6bef8dccaf06867afd880c8f4531fd82 to your computer and use it in GitHub Desktop.
Save rishabh-ink/6bef8dccaf06867afd880c8f4531fd82 to your computer and use it in GitHub Desktop.
A boilerplate example of a Python CLI program
# -*- 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