# Cliopts Library
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cliopts)
![PyPI - Downloads](https://img.shields.io/pypi/dm/cliopts)
The Cliopts Library is a Python library designed to simplify the process of parsing command-line arguments. It provides a straightforward and intuitive API, reducing the amount of code required for CLI argument parsing.
## Installation
To install the Cliopts Library, run the following command in your terminal (cmd, bash, PowerShell, etc.):
```bash
pip install cliopts
```
## Usage
To use the library in your code, follow these steps:
1. Import the `CliArguments` class from the `cliopts` module:
```python
from cliopts import CliArguments
```
2. Create an instance of `CliArguments` and pass a list of argument names or a dictionary of argument names and their shorthand notations, along with optional parameters such as `options_desc` and `version`.
Using a list of options:
```python
args = CliArguments(
options=["filename", "count", "verbose"],
options_desc={
"filename": "Specify the filename",
"count": "Specify the count",
"verbose": "Enable verbose output"
},
version="v1.0.0"
)
```
Using a dictionary of options with shorthand notations:
```python
args = CliArguments(
options={
"filename": "f",
"count": "c",
"verbose": "v"
},
options_desc={
"filename": "Specify the filename",
"count": "Specify the count",
"verbose": "Enable verbose output"
},
version="v1.0.0"
)
```
3. Access the parsed command-line arguments as a dictionary using the `to_dict()` method:
```python
print(args.to_dict())
```
The `to_dict()` method returns a dictionary containing the parsed arguments.
4. Run your Python script and pass command-line arguments using the specified options and their shorthand notations:
```bash
py test.py --filename="filename.txt" --count=5 --verbose=True
py test.py -f "filename.txt" -c 5 -v True
```
Replace `test.py` with the name of your script file and `filename.txt` with the desired value for the argument.
### Example
Let's consider an example to illustrate how to use the Cliopts Library. Suppose we are creating a Python script that takes a filename, count, and a verbose flag as input from the command line.
In `script.py` file:
```python
from cliopts import CliArguments
# Define the desired arguments: filename, count, verbose
args = CliArguments(
options={
"filename": "f",
"count": "c",
"verbose": "v"
},
options_desc={
"filename": "Specify the filename",
"count": "Specify the count",
"verbose": "Enable verbose output"
},
version="v1.0.0"
)
print(args.to_dict())
```
In the command line:
```bash
py script.py --filename='/files/filename.txt' --count=5 --verbose=True
py script.py -f '/files/filename.txt' -c 5 -v True
```
The output of `args.to_dict()` will be:
```python
{
"filename": "/files/filename.txt",
"count": 5,
"verbose": True
}
```
## CliArgument class params
### options
- **Type**: `Iterable[str] | dict[str, str]`
- **Description**: A list or dictionary of command-line options. If using a dictionary, the keys are the full option names and the values are their shorthand notations.
### options_desc
- **Type**: `dict[str, str]`
- **Optional**: Yes
- **Description**: A dictionary with descriptions for each option. Defaults to an empty dictionary.
### version
- **Type**: `str`
- **Optional**: Yes
- **Description**: The version of the program. Defaults to `None`.
### help
- **Type**: `(dict[str, str]) -> Any`
- **Optional**: Yes
- **Description**: A function to display help information. This function takes `options_desc` as its argument. If not provided, the default help function is triggered utilizing `options_desc`.
### throw_on_invalid_args
- **Type**: `bool`
- **Optional**: Yes
- **Description**: Whether to throw an error on invalid arguments. Defaults to `True`.
### name
- **Type**: `str`
- **Optional**: Yes
- **Description**: The name of the program. Defaults to `"python-program"`.
### desc
- **Type**: `str`
- **Optional**: Yes
- **Description**: The description of the program. Defaults to `None`.
## Default Help Function Output
If the `--help` flag is used, the default help function displays the following information:
```
Usage: python-program [options]
Options:
filename : Specify the filename
count : Specify the count
verbose : Enable verbose output
--version : Show version
```
## Attach Version
If you want your script to return a version number when prompted with `--version`, you can easily achieve that by passing a version string as the `version` parameter when creating an instance of `CliArguments`.
Example in `script.py` file:
```python
from cliopts import CliArguments
args = CliArguments(
options=["filename", "count", "verbose"],
version="v1.0.0"
)
```
You can now check the script version using the following command:
```bash
py script.py --version
```
The output will be `v1.0.0`, matching the version parameter.
## Contact the Developer
For any inquiries or assistance, you can contact the developer at <ssanmeet123@gmail.com>. Feel free to reach out with any questions or feedback you may have.
Raw data
{
"_id": null,
"home_page": "https://github.com/Sanmeet007/cliopts.git",
"name": "cliopts",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "python, command line, command line arguments, cli, cli args, cli opts, command line options, cliargs, argparser",
"author": "Sanmeet Singh",
"author_email": "ssanmeet123@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/85/54/67dacc7cc928fe95e5dcc3ac3e0b02c8910d79d3fd212476957484b8c5a5/cliopts-1.2.0.tar.gz",
"platform": null,
"description": "\r\n# Cliopts Library\r\n\r\n\r\n\r\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cliopts)\r\n\r\n![PyPI - Downloads](https://img.shields.io/pypi/dm/cliopts)\r\n\r\n\r\n\r\nThe Cliopts Library is a Python library designed to simplify the process of parsing command-line arguments. It provides a straightforward and intuitive API, reducing the amount of code required for CLI argument parsing.\r\n\r\n\r\n\r\n## Installation\r\n\r\n\r\n\r\nTo install the Cliopts Library, run the following command in your terminal (cmd, bash, PowerShell, etc.):\r\n\r\n\r\n\r\n```bash\r\n\r\npip install cliopts\r\n\r\n```\r\n\r\n\r\n\r\n## Usage\r\n\r\n\r\n\r\nTo use the library in your code, follow these steps:\r\n\r\n\r\n\r\n1. Import the `CliArguments` class from the `cliopts` module:\r\n\r\n\r\n\r\n ```python\r\n\r\n from cliopts import CliArguments\r\n\r\n ```\r\n\r\n\r\n\r\n2. Create an instance of `CliArguments` and pass a list of argument names or a dictionary of argument names and their shorthand notations, along with optional parameters such as `options_desc` and `version`.\r\n\r\n\r\n\r\n Using a list of options:\r\n\r\n\r\n\r\n ```python\r\n\r\n args = CliArguments(\r\n\r\n options=[\"filename\", \"count\", \"verbose\"],\r\n\r\n options_desc={\r\n\r\n \"filename\": \"Specify the filename\",\r\n\r\n \"count\": \"Specify the count\",\r\n\r\n \"verbose\": \"Enable verbose output\"\r\n\r\n },\r\n\r\n version=\"v1.0.0\"\r\n\r\n )\r\n\r\n ```\r\n\r\n\r\n\r\n Using a dictionary of options with shorthand notations:\r\n\r\n\r\n\r\n ```python\r\n\r\n args = CliArguments(\r\n\r\n options={\r\n\r\n \"filename\": \"f\",\r\n\r\n \"count\": \"c\",\r\n\r\n \"verbose\": \"v\"\r\n\r\n },\r\n\r\n options_desc={\r\n\r\n \"filename\": \"Specify the filename\",\r\n\r\n \"count\": \"Specify the count\",\r\n\r\n \"verbose\": \"Enable verbose output\"\r\n\r\n },\r\n\r\n version=\"v1.0.0\"\r\n\r\n )\r\n\r\n ```\r\n\r\n\r\n\r\n3. Access the parsed command-line arguments as a dictionary using the `to_dict()` method:\r\n\r\n\r\n\r\n ```python\r\n\r\n print(args.to_dict())\r\n\r\n ```\r\n\r\n\r\n\r\n The `to_dict()` method returns a dictionary containing the parsed arguments.\r\n\r\n\r\n\r\n4. Run your Python script and pass command-line arguments using the specified options and their shorthand notations:\r\n\r\n\r\n\r\n ```bash\r\n\r\n py test.py --filename=\"filename.txt\" --count=5 --verbose=True\r\n\r\n py test.py -f \"filename.txt\" -c 5 -v True\r\n\r\n ```\r\n\r\n\r\n\r\n Replace `test.py` with the name of your script file and `filename.txt` with the desired value for the argument.\r\n\r\n\r\n\r\n### Example\r\n\r\n\r\n\r\nLet's consider an example to illustrate how to use the Cliopts Library. Suppose we are creating a Python script that takes a filename, count, and a verbose flag as input from the command line.\r\n\r\n\r\n\r\nIn `script.py` file:\r\n\r\n\r\n\r\n```python\r\n\r\nfrom cliopts import CliArguments\r\n\r\n\r\n\r\n# Define the desired arguments: filename, count, verbose\r\n\r\nargs = CliArguments(\r\n\r\n options={\r\n\r\n \"filename\": \"f\",\r\n\r\n \"count\": \"c\",\r\n\r\n \"verbose\": \"v\"\r\n\r\n },\r\n\r\n options_desc={\r\n\r\n \"filename\": \"Specify the filename\",\r\n\r\n \"count\": \"Specify the count\",\r\n\r\n \"verbose\": \"Enable verbose output\"\r\n\r\n },\r\n\r\n version=\"v1.0.0\"\r\n\r\n)\r\n\r\n\r\n\r\nprint(args.to_dict())\r\n\r\n```\r\n\r\n\r\n\r\nIn the command line:\r\n\r\n\r\n\r\n```bash\r\n\r\npy script.py --filename='/files/filename.txt' --count=5 --verbose=True\r\n\r\npy script.py -f '/files/filename.txt' -c 5 -v True\r\n\r\n```\r\n\r\n\r\n\r\nThe output of `args.to_dict()` will be:\r\n\r\n\r\n\r\n```python\r\n\r\n{\r\n\r\n \"filename\": \"/files/filename.txt\",\r\n\r\n \"count\": 5,\r\n\r\n \"verbose\": True\r\n\r\n}\r\n\r\n```\r\n\r\n\r\n\r\n## CliArgument class params\r\n\r\n\r\n\r\n### options\r\n\r\n\r\n\r\n- **Type**: `Iterable[str] | dict[str, str]`\r\n\r\n- **Description**: A list or dictionary of command-line options. If using a dictionary, the keys are the full option names and the values are their shorthand notations.\r\n\r\n\r\n\r\n### options_desc\r\n\r\n\r\n\r\n- **Type**: `dict[str, str]`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: A dictionary with descriptions for each option. Defaults to an empty dictionary.\r\n\r\n\r\n\r\n### version\r\n\r\n\r\n\r\n- **Type**: `str`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: The version of the program. Defaults to `None`.\r\n\r\n\r\n\r\n### help\r\n\r\n\r\n\r\n- **Type**: `(dict[str, str]) -> Any`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: A function to display help information. This function takes `options_desc` as its argument. If not provided, the default help function is triggered utilizing `options_desc`.\r\n\r\n\r\n\r\n### throw_on_invalid_args\r\n\r\n\r\n\r\n- **Type**: `bool`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: Whether to throw an error on invalid arguments. Defaults to `True`.\r\n\r\n\r\n\r\n### name\r\n\r\n\r\n\r\n- **Type**: `str`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: The name of the program. Defaults to `\"python-program\"`.\r\n\r\n\r\n\r\n### desc\r\n\r\n\r\n\r\n- **Type**: `str`\r\n\r\n- **Optional**: Yes\r\n\r\n- **Description**: The description of the program. Defaults to `None`.\r\n\r\n\r\n\r\n## Default Help Function Output\r\n\r\n\r\n\r\nIf the `--help` flag is used, the default help function displays the following information:\r\n\r\n\r\n\r\n```\r\n\r\nUsage: python-program [options]\r\n\r\n\r\n\r\nOptions:\r\n\r\n filename : Specify the filename\r\n\r\n count : Specify the count\r\n\r\n verbose : Enable verbose output\r\n\r\n\r\n\r\n--version : Show version\r\n\r\n```\r\n\r\n\r\n\r\n## Attach Version\r\n\r\n\r\n\r\nIf you want your script to return a version number when prompted with `--version`, you can easily achieve that by passing a version string as the `version` parameter when creating an instance of `CliArguments`.\r\n\r\n\r\n\r\nExample in `script.py` file:\r\n\r\n\r\n\r\n```python\r\n\r\nfrom cliopts import CliArguments\r\n\r\n\r\n\r\nargs = CliArguments(\r\n\r\n options=[\"filename\", \"count\", \"verbose\"],\r\n\r\n version=\"v1.0.0\"\r\n\r\n)\r\n\r\n```\r\n\r\n\r\n\r\nYou can now check the script version using the following command:\r\n\r\n\r\n\r\n```bash\r\n\r\npy script.py --version\r\n\r\n```\r\n\r\n\r\n\r\nThe output will be `v1.0.0`, matching the version parameter.\r\n\r\n\r\n\r\n## Contact the Developer\r\n\r\n\r\n\r\nFor any inquiries or assistance, you can contact the developer at <ssanmeet123@gmail.com>. Feel free to reach out with any questions or feedback you may have.\r\n\r\n",
"bugtrack_url": null,
"license": "GPL-3.0 license",
"summary": "The cliopts package is a Python library for parsing command line arguments. It provides a simpler and more intuitive API with less code and easy cli argument parsing.",
"version": "1.2.0",
"project_urls": {
"Homepage": "https://github.com/Sanmeet007/cliopts.git"
},
"split_keywords": [
"python",
" command line",
" command line arguments",
" cli",
" cli args",
" cli opts",
" command line options",
" cliargs",
" argparser"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2704879e730d92cc8758571cee4f3239f5aa0936756a273500c8d5309db68d3d",
"md5": "651199ac96396897a20f24b32219b6e5",
"sha256": "46526323913ff2bce5550d3cbb267199e61b03bae1bcf98834636d5568a3cdd1"
},
"downloads": -1,
"filename": "cliopts-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "651199ac96396897a20f24b32219b6e5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 17567,
"upload_time": "2024-07-28T16:26:04",
"upload_time_iso_8601": "2024-07-28T16:26:04.745770Z",
"url": "https://files.pythonhosted.org/packages/27/04/879e730d92cc8758571cee4f3239f5aa0936756a273500c8d5309db68d3d/cliopts-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "855467dacc7cc928fe95e5dcc3ac3e0b02c8910d79d3fd212476957484b8c5a5",
"md5": "bd2ff5382b829a583134595bfdd87f03",
"sha256": "1af03f009a31952d93bae24b100385a51c5177afe1e6427f65933457c13a985e"
},
"downloads": -1,
"filename": "cliopts-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "bd2ff5382b829a583134595bfdd87f03",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17283,
"upload_time": "2024-07-28T16:26:06",
"upload_time_iso_8601": "2024-07-28T16:26:06.539638Z",
"url": "https://files.pythonhosted.org/packages/85/54/67dacc7cc928fe95e5dcc3ac3e0b02c8910d79d3fd212476957484b8c5a5/cliopts-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-28 16:26:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Sanmeet007",
"github_project": "cliopts",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cliopts"
}