Index
Directory Structure:
├── bin
│ └── cli_script
├── setup.py
└── some_module
├── __init__.py
└── some_module.py
Procedure: To make a Python script usable as a CLI (Command Line Interface) tool, follow these steps:
-
You need to create a setup script to package and install your Python script as a CLI tool. Create a new file named
setup.pyin the same directory as your Python script. -
In
setup.py, use thesetuptoolsmodule to define the scripts and packages that should be installed:
from setuptools import find_packages, setup
setup(
name='some_module',
version='1.0'
packages=find_packages(),
scripts=['bin/cli_script']
)-
Replace
your_scriptwith the name of your Python script (without the.pyextension), andbin/your_scriptwith the path to the script in thebindirectory of your project. -
To install your CLI tool, run the following command in the terminal:
pip install -e .Replace . with the path to your project directory if you’re not currently in the directory containing setup.py.
- Once installed, you can run your CLI tool from the terminal by typing its name followed by the required arguments:
your-cli-tool-name input_file.txt -vReplace input_file.txt with the path to your input file and add any other required arguments or flags.
By following these steps, you can create a Python script that can be used as a CLI tool. The argparse module provides a simple way to define and parse command-line arguments, and the setuptools module allows you to package and install your script as a CLI tool.