Skip to main content

Generate command line options and help/tips from function automatically.

Project description

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

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

~/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 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


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.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optfunc2-0.2.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file optfunc2-0.2.2.tar.gz.

File metadata

  • Download URL: optfunc2-0.2.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for optfunc2-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a47f0c496aac7f46f28601dd3bb269df6ed821cf39931f7867a978b5a1f0369e
MD5 3ea2a5462f1f8d88d6af0db940cc0199
BLAKE2b-256 70d28fe5d5fc7831a909dfbdb414beb2b31225d7bbe40b70c9defdc9b324767a

See more details on using hashes here.

File details

Details for the file optfunc2-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: optfunc2-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for optfunc2-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ec123e1ea87c5a0278bd24e31a2933efd41cf6b1de71a3ef700bf235024e44d
MD5 ee04bb03bc327f1eb160a38639b140f1
BLAKE2b-256 51ba2287b1b54ded65c8cb7372e92513f5d3d28b403a72cd9d686b75d9a5918e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page