Name | optfunc2 JSON |
Version |
0.2.3
JSON |
| download |
home_page | https://github.com/yjloong/optfunc2 |
Summary | Generate command line options and help/tips from function automatically. |
upload_time | 2025-02-16 10:13:40 |
maintainer | None |
docs_url | None |
author | bajeer |
requires_python | <4.0,>=3.8 |
license | PyPA |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Call function directly in cmd line
### Features
1. Allow user call functions directly in command line.
2. Generate help tips automatically.
3. Add default called functions if not function was specific.
### Notice
1. It's better to add argument type for each autocall functions.
2. Function with @optfunc_default has @optfunc implicitly.
3. Not support two type of variadic arguments.
### ChangeLog
### 0.2.2 (2025-2-16)
1. Support single argument in bool type.
2. Don't need user to pass globals() in cmdline_start().
3. Support pytest to run test.
4. Support omitting called function name who is default.
5. pyproject.toml format change for poetry version 2.1.0.
#### 0.2.1 (2025-2-14)
1. Fix installing dependencies automatically.
2. Add function 'called_directly' used to check if the function is called as entry point.
This function can be used in function development.
#### 0.1.2 (2023-05-06)
1. Add support for default called functions.
2. Fix README.md.
3. Add ChangeLog in README.md.
### Code example1 -- calculator
``` python
from optfunc2 import cmdline, cmdline_default, cmdline_start
@cmdline_default
def add(a: float, b: float):
"""add two numbers
Args:
a (float): The First number
b (float): The Second number
"""
print(f"{a} + {b} = {a + b}")
@cmdline
def multiply(x: int, y: int = 5):
"""multiply two numbers. The second number is optional.
Args:
x (int): The First number
y (int, optional): The Second number. Defaults to 5.
"""
print(f"{x} × {y} = {x * y}")
@cmdline
def stats(numbers: list):
"""statistics of numbers in list
Args:
numbers (list): Target List.
"""
print(f"sum: {sum(numbers)}")
print(f"average: {sum(numbers)/len(numbers):.2f}")
if __name__ == "__main__":
cmdline_start(header_doc="✨ calc CLI", has_abbrev=True)
```
### Generate help tips automatically
``` bash
~/optfunc2$ python src/example_calc.py help
Usage: src/example_calc.py [command] [<args>|--help]
✨ calc CLI
commands:
add [default] add two numbers
multiply multiply two numbers. The second number is optional.
stats statistics of numbers in list
~/optfunc2$ python src/example_calc.py add -h
Usage: src/example_calc.py add [OPTIONS]
add two numbers
Arguments:
+-----+--------+-------+---------+-------------------+
| Opt | Abbrev | Type | Default | Desc |
+-----+--------+-------+---------+-------------------+
| --a | -a | float | | The First number |
| --b | -b | float | | The Second number |
+-----+--------+-------+---------+-------------------+
~/optfunc2$ python src/example_calc.py stats -h
Usage: src/example_calc.py stats [OPTIONS]
statistics of numbers in list
Arguments:
+-----------+--------+------+---------+--------------+
| Opt | Abbrev | Type | Default | Desc |
+-----------+--------+------+---------+--------------+
| --numbers | -n | list | | Target List. |
+-----------+--------+------+---------+--------------+
```
### Usage
``` bash
~/optfunc2$ python src/example_calc.py add -a 2.3 -b 3
2.3 + 3.0 = 5.3
~/optfunc2$ python src/example_calc.py -a 2.3 -b 3
2.3 + 3.0 = 5.3
~/optfunc2$ python src/example_calc.py multiply -x 3
3 × 5 = 15
~/optfunc2$ python src/example_calc.py stats --numbers '[1, 2, 3, 4, 5]'
sum: 15
average: 3.00
```
### Code example2 -- list files
``` python
from optfunc2 import cmdline, cmdline_default, cmdline_start
import os
@cmdline_default
def list_files(directory: str = ".", show_size: bool = False):
"""List files in a directory.
Args:
directory (str, optional): Target directory. Defaults to ".".
show_size (bool, optional): Whether to show size of file. Defaults to False.
"""
for f in os.listdir(directory):
path = os.path.join(directory, f)
if show_size and os.path.isfile(path):
print(f"{f} ({os.path.getsize(path)} bytes)")
else:
print(f)
if __name__ == "__main__":
cmdline_start(header_doc="📁 file manager", has_abbrev=True)
```
### Usage
``` bash
$ python src/example_file_operator.py -h
Usage: src/example_file_operator.py [command] [<args>|--help]
📁 file manager
commands:
list_files [default] List files in a directory.
$ python src/example_file_operator.py list_files -h
Usage: src/example_file_operator.py list_files [OPTIONS]
List files in a directory.
Arguments:
+-------------+--------+------+---------+--------------------------------------------------+
| Opt | Abbrev | Type | Default | Desc |
+-------------+--------+------+---------+--------------------------------------------------+
| --directory | -d | str | '.' | Target directory. Defaults to ".". |
| --show_size | -s | bool | False | Whether to show size of file. Defaults to False. |
+-------------+--------+------+---------+--------------------------------------------------+
$ python src/example_file_operator.py
LICENSE.txt
.gitignore
pyproject.toml
$ python src/example_file_operator.py -s
LICENSE.txt (1081 bytes)
.gitignore (132 bytes)
pyproject.toml (719 bytes)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/yjloong/optfunc2",
"name": "optfunc2",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": "bajeer",
"author_email": "z-bajeer@yeah.net",
"download_url": "https://files.pythonhosted.org/packages/0c/bd/9a32c31ae630e4e27836dfb02325f10930974109665027ed1f8565078960/optfunc2-0.2.3.tar.gz",
"platform": null,
"description": "# Call function directly in cmd line\n\n### Features\n1. Allow user call functions directly in command line.\n2. Generate help tips automatically.\n3. Add default called functions if not function was specific.\n\n### Notice\n1. It's better to add argument type for each autocall functions.\n2. Function with @optfunc_default has @optfunc implicitly.\n3. Not support two type of variadic arguments.\n\n### ChangeLog\n### 0.2.2 (2025-2-16)\n1. Support single argument in bool type.\n2. Don't need user to pass globals() in cmdline_start().\n3. Support pytest to run test.\n4. Support omitting called function name who is default.\n5. pyproject.toml format change for poetry version 2.1.0.\n\n#### 0.2.1 (2025-2-14)\n1. Fix installing dependencies automatically.\n2. Add function 'called_directly' used to check if the function is called as entry point.\n This function can be used in function development.\n \n#### 0.1.2 (2023-05-06)\n1. Add support for default called functions.\n2. Fix README.md.\n3. Add ChangeLog in README.md.\n\n### Code example1 -- calculator\n``` python\nfrom optfunc2 import cmdline, cmdline_default, cmdline_start\n\n@cmdline_default\ndef add(a: float, b: float):\n \"\"\"add two numbers\n\n Args:\n a (float): The First number\n b (float): The Second number\n \"\"\"\n print(f\"{a} + {b} = {a + b}\")\n\n@cmdline\ndef multiply(x: int, y: int = 5):\n \"\"\"multiply two numbers. The second number is optional.\n\n Args:\n x (int): The First number\n y (int, optional): The Second number. Defaults to 5.\n \"\"\"\n print(f\"{x} \u00d7 {y} = {x * y}\")\n\n@cmdline\ndef stats(numbers: list):\n \"\"\"statistics of numbers in list\n\n Args:\n numbers (list): Target List.\n \"\"\" \n print(f\"sum: {sum(numbers)}\")\n print(f\"average: {sum(numbers)/len(numbers):.2f}\")\n\nif __name__ == \"__main__\":\n cmdline_start(header_doc=\"\u2728 calc CLI\", has_abbrev=True)\n```\n\n### Generate help tips automatically\n``` bash\n~/optfunc2$ python src/example_calc.py help\nUsage: src/example_calc.py [command] [<args>|--help]\n\n\u2728 calc CLI\n\ncommands:\n add [default] add two numbers\n multiply multiply two numbers. The second number is optional.\n stats statistics of numbers in list\n\n~/optfunc2$ python src/example_calc.py add -h\nUsage: src/example_calc.py add [OPTIONS]\n\nadd two numbers\n\n\nArguments:\n+-----+--------+-------+---------+-------------------+\n| Opt | Abbrev | Type | Default | Desc |\n+-----+--------+-------+---------+-------------------+\n| --a | -a | float | | The First number |\n| --b | -b | float | | The Second number |\n+-----+--------+-------+---------+-------------------+\n\n\n~/optfunc2$ python src/example_calc.py stats -h\nUsage: src/example_calc.py stats [OPTIONS]\n\nstatistics of numbers in list\n\n\nArguments:\n+-----------+--------+------+---------+--------------+\n| Opt | Abbrev | Type | Default | Desc |\n+-----------+--------+------+---------+--------------+\n| --numbers | -n | list | | Target List. |\n+-----------+--------+------+---------+--------------+\n```\n\n### Usage\n``` bash\n~/optfunc2$ python src/example_calc.py add -a 2.3 -b 3\n2.3 + 3.0 = 5.3\n~/optfunc2$ python src/example_calc.py -a 2.3 -b 3\n2.3 + 3.0 = 5.3\n~/optfunc2$ python src/example_calc.py multiply -x 3\n3 \u00d7 5 = 15\n~/optfunc2$ python src/example_calc.py stats --numbers '[1, 2, 3, 4, 5]'\nsum: 15\naverage: 3.00\n```\n\n### Code example2 -- list files\n``` python\nfrom optfunc2 import cmdline, cmdline_default, cmdline_start\nimport os\n\n@cmdline_default\ndef list_files(directory: str = \".\", show_size: bool = False):\n \"\"\"List files in a directory.\n\n Args:\n directory (str, optional): Target directory. Defaults to \".\".\n show_size (bool, optional): Whether to show size of file. Defaults to False.\n \"\"\"\n for f in os.listdir(directory):\n path = os.path.join(directory, f)\n if show_size and os.path.isfile(path):\n print(f\"{f} ({os.path.getsize(path)} bytes)\")\n else:\n print(f)\n\nif __name__ == \"__main__\":\n cmdline_start(header_doc=\"\ud83d\udcc1 file manager\", has_abbrev=True)\n```\n\n### Usage\n``` bash\n$ python src/example_file_operator.py -h\nUsage: src/example_file_operator.py [command] [<args>|--help]\n\n\ud83d\udcc1 file manager\n\ncommands:\n list_files [default] List files in a directory.\n\n$ python src/example_file_operator.py list_files -h\nUsage: src/example_file_operator.py list_files [OPTIONS]\n\nList files in a directory.\n\n\nArguments:\n+-------------+--------+------+---------+--------------------------------------------------+\n| Opt | Abbrev | Type | Default | Desc |\n+-------------+--------+------+---------+--------------------------------------------------+\n| --directory | -d | str | '.' | Target directory. Defaults to \".\". |\n| --show_size | -s | bool | False | Whether to show size of file. Defaults to False. |\n+-------------+--------+------+---------+--------------------------------------------------+\n$ python src/example_file_operator.py\nLICENSE.txt\n.gitignore\npyproject.toml\n$ python src/example_file_operator.py -s\nLICENSE.txt (1081 bytes)\n.gitignore (132 bytes)\npyproject.toml (719 bytes)\n```",
"bugtrack_url": null,
"license": "PyPA",
"summary": "Generate command line options and help/tips from function automatically.",
"version": "0.2.3",
"project_urls": {
"Bug Tracker": "https://github.com/yjloong/optfunc2/issues",
"Homepage": "https://github.com/yjloong/optfunc2"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "67b60e0328fd3bfc81721c0be217dac77e047b0012a131917bda3809c2ea1b15",
"md5": "1853fbb3f850998c08dc0dd40299e38b",
"sha256": "f422fea745b9838799d8d8c69e56225f1291b77be3bbed7eafdbf38b784e4875"
},
"downloads": -1,
"filename": "optfunc2-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1853fbb3f850998c08dc0dd40299e38b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 7275,
"upload_time": "2025-02-16T10:13:36",
"upload_time_iso_8601": "2025-02-16T10:13:36.649614Z",
"url": "https://files.pythonhosted.org/packages/67/b6/0e0328fd3bfc81721c0be217dac77e047b0012a131917bda3809c2ea1b15/optfunc2-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0cbd9a32c31ae630e4e27836dfb02325f10930974109665027ed1f8565078960",
"md5": "3fa562ee03edf5045cef2e831c74cf59",
"sha256": "9cd4dbf0c7272e2d7a5f67cc257f279db3b8e5dfa52f940414c96109018981ad"
},
"downloads": -1,
"filename": "optfunc2-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "3fa562ee03edf5045cef2e831c74cf59",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 6503,
"upload_time": "2025-02-16T10:13:40",
"upload_time_iso_8601": "2025-02-16T10:13:40.085724Z",
"url": "https://files.pythonhosted.org/packages/0c/bd/9a32c31ae630e4e27836dfb02325f10930974109665027ed1f8565078960/optfunc2-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-16 10:13:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yjloong",
"github_project": "optfunc2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "optfunc2"
}