Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building CLIs that Click

409 views

Published on

PyNash presentation on building better CLIs

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Building CLIs that Click

  1. 1. BUILDING CLIS THAT CLICK Created by /Jason A Myers @jasonamyers
  2. 2. BUILDING GOOD COMMAND LINE APPLICATIONS IS HARD
  3. 3. IMPORTANT PARTS NAME ARGUMENT PARSING AND VALIDATION * HELP GENERATION * COMMAND STRUCTURE * AUTOCOMPLETION NICE OUTPUT PACKAGING *
  4. 4. ARGUMENTS AND HELP
  5. 5. THREE DIFFERENT PARSERS IN THE STDLIB getopt optparse argparse
  6. 6. I MEAN ARGPARSE IS THE NEW HOTNESS???
  7. 7. SERIOUSLY WHO KNOWS HOW *!@%PARSE WORKS ANYWAY
  8. 8. NO REALLY HAVE YOU LOOKED AT THE DOCS...
  9. 9. SERIOUSLY BRAIN CELLS EXPLODE
  10. 10. HOW BAD IS IT? docopt Plac Cliff Clint
  11. 11. import sys if __name__ == "__main__": main(sys.argv)
  12. 12. DEMO
  13. 13. LOGGING
  14. 14. GOOD LOGGING MESSAGES Time Module Level Parseable Messages
  15. 15. 2015-05-28 09:25:18,711 - complex.logger - DEBUG - Creating composit e: cookies 2015-05-28 09:25:18,711 - complex.logger - DEBUG - Created composite : cookies
  16. 16. JAM'S LOGGING STYLE
  17. 17. import logging logger = logging.getLogger(__name__) logger.setLevel(logging.ERROR)
  18. 18. file_log_handler = logging.FileHandler('complex-cli.log') logger.addHandler(file_log_handler) stderr_log_handler = logging.StreamHandler() logger.addHandler(stderr_log_handler)
  19. 19. format_string = '%(asctime)s - %(name)s - ' '%(levelname)s - %(message)s' formatter = logging.Formatter(format_string) file_log_handler.setFormatter(formatter) stderr_log_handler.setFormatter(formatter)
  20. 20. PACKAGING
  21. 21. FIND OUR MODULE from setuptools import setup, find_packages setup( name='complex', version='0.1.2', packages=find_packages(), include_package_data=True,
  22. 22. install_requires=[ 'Click==3.3', ],
  23. 23. description='A description', classifiers=[ 'License :: OSI Approved :: BSD License', 'Programming Language :: Python', 'Programming Language :: Python :: 3', ],
  24. 24. entry_points=''' [console_scripts] complex=complex.command:cli ''' )
  25. 25. COMPLEX DEMO
  26. 26. QUESTIONS @JASONAMYERS

×