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.py
in the same directory as your Python script. -
In
setup.py
, use thesetuptools
module to define the scripts and packages that should be installed:
-
Replace
your_script
with the name of your Python script (without the.py
extension), andbin/your_script
with the path to the script in thebin
directory of your project. -
To install your CLI tool, run the following command in the terminal:
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:
Replace 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.