Skip to main content

Generate options from function.

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. Arguments of function with @optfunc_default should be optional or no argument.
  4. Not support two type of variadic arguments.

ChangeLog

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 example

from optfunc2 import *

@optfunc
def arg_test_positional_only(pos_only0, pos_only1: int, pos_only2 = 5, pos_only3: int = 6):
    """summary for the function

    Args:
        pos_only0 (_type_): This is the first positional-only argument.
        pos_only1 (int): This is the second positional-only argument.
        pos_only2 (int, optional): This is the third positional-only argument. Defaults to 5.
        pos_only3 (int, optional): This is the fourth positional-only argument. Defaults to 6.
    """
    " Argument test for positional-only arguments. "
    print(f'pos_only0: {pos_only0}, pos_only1: {pos_only1}, pos_only2: {pos_only2}, pos_only3: {pos_only3}')
    pass

@optfunc
def arg_test_positional_or_keyword(pos_or_kw, pos_or_kw1: int, pos_or_kw2 = 3, pos_or_kw3: int = 4):
    " Argument test for positional-or-keyword arguments. "
    print(f'pos_or_kw: {pos_or_kw}, pos_or_kw1: {pos_or_kw1}, pos_or_kw2: {pos_or_kw2}, pos_or_kw3: {pos_or_kw3}')
    pass

@optfunc
def arg_test_kw_only(*, kw_only0, kw_only1: int, kw_only2 = 9, kw_only3: int = 10):
    " Argument test for keyword-only arguments. "
    print(f'kw_only0: {kw_only0}, kw_only1: {kw_only1}, kw_only2: {kw_only2}, kw_only3: {kw_only3}')
    pass

if __name__ == '__main__':
    optfunc_start(globals=globals(), has_abbrev=False, header_doc='This is a test file for the module "autocall".')

Run the code

~/:$ python3 test.py -h
Usage: test.py [command] [<args>|--help]

This is a test file for the module "autocall".

commands:
    arg_test_positional_only           summary for the function
    arg_test_positional_or_keyword     Argument test for positional-or-keyword arguments.
    arg_test_kw_only                   Argument test for keyword-only arguments.

~/:$ python3 test.py arg_test_positional_only -h
Usage: test.py arg_test_positional_only [OPTIONS]

summary for the function


Arguments:
+-------------+------+---------+-------------------------------------------------------------+
|     Opt     | Type | Default |                             Desc                            |
+-------------+------+---------+-------------------------------------------------------------+
| --pos_only0 | any  |         |         This is the first positional-only argument.         |
| --pos_only1 | int  |         |         This is the second positional-only argument.        |
| --pos_only2 | any  |    5    |  This is the third positional-only argument. Defaults to 5. |
| --pos_only3 | int  |    6    | This is the fourth positional-only argument. Defaults to 6. |
+-------------+------+---------+-------------------------------------------------------------+
~/:$ python3 test.py arg_test_positional_only --pos_only0 "good day" --pos_only1 2
pos_only0: good day, pos_only1: 2, pos_only2: 5, pos_only3: 6

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.1.tar.gz (6.3 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.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: optfunc2-0.2.1.tar.gz
  • Upload date:
  • Size: 6.3 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.1.tar.gz
Algorithm Hash digest
SHA256 1f6f3da5a2c106d32ccb9e644cdc921d063dd221f373a01eddb1b33508f5fac2
MD5 4aff88c876894047f5db6e4b3ff334ea
BLAKE2b-256 0563ae071f417170b86d3695f2c36a860ad08245e18897b2c448d9c0d562d549

See more details on using hashes here.

File details

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

File metadata

  • Download URL: optfunc2-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fcaa6d221c077e41176db6859c0534bcc0964b67d76dd9bfa6a8b4b42adb31fe
MD5 f872113ee3261bc2d8b4aecb40169c95
BLAKE2b-256 63059348194bcb77d50ae07175f7277e74fc18644a96182452aea2776cdf3e54

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