Joff
Joff

Reputation: 12187

How does a package like fabric (written in python) make its CLI tool fab

I got curious about making command line tools in python (without having to type python tool.py)

I know fabric does this so I started looking through their github page and I couldn't track down where the fab tool actually comes from.

  1. How do they do it?
  2. What is a minimal way to get a tool like this working?

https://github.com/fabric/fabric

Upvotes: 1

Views: 82

Answers (1)

Andris
Andris

Reputation: 973

(Skipping the command line part as that's a different story and only focusing on where the fab.exe is coming from as you made me curious.)

It's from setup.py, the interesting part is:

entry_points={
        'console_scripts': [
            'fab = fabric.main:main',
        ]
},

which tells what it actually starts.

To reproduce, here's an almost empty setup.py:

#!/usr/bin/env python

from setuptools import setup

setup(
    name='consoletest',
    version='1.1',
    entry_points={
        'console_scripts': [
            'consoletest = consoletest.main:main',
        ]
    },
)

Please create a subdir consoletest and put in it an empty __init__.py (to tell it's a module) plus a main.py with the following content:

def main():
    print("hello")

then issue

python setup.py install

in the root, and if everything went well, an egg file will be created in python's Lib/site-packages directory, and a consoletest executable in the Scripts dir. (Tested only under Windows/Python 2.7 so you may not get the same result.)

Now if you start consoletest it will print hello.

Upvotes: 1

Related Questions