A tiny Python library to easily create CLI applications with sub commands
Project description
clidir
Create CLI applications with sub commands easily.
Description
Even simple CLI applications usually require sub commands. For example, git has git remote add, where remote and add are sub commands. Doing this with argparse or even with more developer friendly libraries can be challenging.
Keeping the commands source code organized in the project can also be complicated.
clidir helps you easily implement commands and sub commands by organizing your Python files in sub directories, so that you can use argparse in the simplest way, without the need of add_subparsers().
For example, to create a git remote add command, the project structure would be:
├── main.py
├── commands/
│ ├── remote/
│ ├── add.py
And the implementation of the add.py command would be like this:
import argparse
def run(args: list[str]) -> None:
parser = argparse.ArgumentParser(prog='git remote add')
parser.add_argument("name")
parser.add_argument("url")
# the rest of the implementation
Installation
Install this tool using pip:
pip install clidir
Examples
Usage
- Create a
main.pyfile with the following code:
import sys
import clidir
def main() -> int:
args = sys.argv
clidir.run(args)
return 0
if __name__ == "__main__":
exit(main())
- Create a
commandsfolder. - Within the
commandsfolder, create sub folders for your commands. For example:
├── commands/
│ ├── remote/
│ ├── add.py
- Implement the command. For example, to implement the
addcommand, use this boilerplate:
import argparse
description = "what the command does"
def run(args: list[str]) -> None:
parser = argparse.ArgumentParser(prog='your-app remote add')
print('running the add command...')
- Test the execution of the command:
% python3 main.py remote add
running the add command...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clidir-0.1.2.tar.gz.
File metadata
- Download URL: clidir-0.1.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca9ae4c6626f54b742ede55ebf20c0027bf81891daf8ad665515572480cde1d1
|
|
| MD5 |
8d3dee9474fd5d897ebb58818e9ba9d0
|
|
| BLAKE2b-256 |
3b0a5047790c28bfed91ea49189b1396152c8879c99fd2292d27330c7c0c5efe
|
File details
Details for the file clidir-0.1.2-py3-none-any.whl.
File metadata
- Download URL: clidir-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eef49019e86db7316c79b8e4e7dbf314ae3987dafe4d92eb0272e744da6d0905
|
|
| MD5 |
37d42d56f7c84837ed00f9fc08ba8b99
|
|
| BLAKE2b-256 |
185ba7cf63da55b9645067cc0143bfb8a483019b83182822f87d1f76a3ef105e
|