Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Last active May 13, 2018 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igniteflow/96a5a7d8bf138ec1b6340a101cb6a920 to your computer and use it in GitHub Desktop.
Save igniteflow/96a5a7d8bf138ec1b6340a101cb6a920 to your computer and use it in GitHub Desktop.
Creating a command-line app with Python
  1. Create a setup.py:
  #!/usr/bin/env python

  from distutils.core import setup

  setup(
      name='My Thing',
      version='1.0',
      description='Does these things',
      scripts=['scripts/thing'],
  )
  1. Create the file ./scripts/thing and add:
  #!/usr/bin/env python
  import argparse
  
  if __name__ == "__main__":
      parser = argparse.ArgumentParser(description='Thing helper')
      parser.add_argument('command', type=str, choices=['install'])
      args = parser.parse_args()

      if args.command == 'install':
        # do something.  
        
      # add additional choices and handle here for positional arguments
  1. Install with pip editable -e .
  2. Now call anywhere with thing install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment