Generate command line options and help/tips from function automatically.
Project description
Call function directly in cmd line
Features
- Allow user call functions directly in command line.
- Generate help tips automatically.
- Add default called functions if not function was specific.
Notice
- It's better to add argument type for each autocall functions.
- Function with @optfunc_default has @optfunc implicitly.
- Not support two type of variadic arguments.
Code example1 -- calculator
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|float, 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
~/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
~/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 multiply -x 2.3
2.3 × 5 = 11.5
~/optfunc2$ python src/example_calc.py stats --numbers '[1, 2, 3, 4, 5]'
sum: 15
average: 3.00
Code example2 -- list files
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
$ 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)
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
optfunc2-0.2.5.tar.gz
(6.5 kB
view details)
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 optfunc2-0.2.5.tar.gz.
File metadata
- Download URL: optfunc2-0.2.5.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.0 CPython/3.9.21 Linux/6.8.0-52-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0db993033a986316444b3fe242ff8a430e50a341e06a6825197e39e7226e973d
|
|
| MD5 |
9466d4cc88148fc93f610e6f99b5a142
|
|
| BLAKE2b-256 |
75ada537cea376028bbbafd6d6982f9b2ce3e7e5965ab6fc169c4d1909d06c82
|
File details
Details for the file optfunc2-0.2.5-py3-none-any.whl.
File metadata
- Download URL: optfunc2-0.2.5-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.0 CPython/3.9.21 Linux/6.8.0-52-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c0015178a301e62e7d300dc1d3588f72120916deeb120e1651fad9ad3d7bbe5
|
|
| MD5 |
6054181642ca70bbdd19a1822689f038
|
|
| BLAKE2b-256 |
7121f0d7bc39b0fd678e213975662de4d0e7c6e37a3a15244b22c3fea426029e
|