Skip to main content

Command line Wizard - Transform your python functions into CLI tools

Project description

CW: Command-line Wrapper Utilities

CW is a Python package that provides utilities for wrapping functions to work seamlessly with command-line interfaces (CLIs). It specializes in resolving string-based function specifications into actual callable functions, making it easy to pass complex objects like functions as command-line parameters.

Why CW?

When building command-line interfaces with great "dispatch to CLI" tools like argh, click, docopt, or typer, all parameter values are fundamentally strings. This creates a challenge when your Python functions need to accept complex objects like other functions as parameters.

Consider this example using argh:

import argh
from cw.resolution import resource_inputs, parse_ast_spec

def process_data(data, transform_func, multiplier=1):
    """Process data using a transformation function."""
    transformed = transform_func(data)
    return transformed * multiplier

# Without CW: This won't work from CLI because transform_func is a string
@argh.dispatch_command
def cli_process(data, transform_func, multiplier=1):
    return process_data(data, transform_func, multiplier)  # ERROR: transform_func is a string!

# With CW: This works seamlessly
function_store = {
    'double': lambda x: x * 2,
    'square': lambda x: x ** 2,
    'upper': str.upper
}

wrapped_process = resource_inputs(
    process_data,
    resource={
        'transform_func': {
            'func_key_and_kwargs': parse_ast_spec,
            'get_func': function_store.get
        }
    }
)

@argh.dispatch_command  
def cli_process_working(data, transform_func, multiplier=1):
    return wrapped_process(data, transform_func, multiplier)

Now you can call from the command line:

python script.py "hello" "upper()" --multiplier 3
# Results in: "HELLOHELLOHELLO"

python script.py 5 "double()" --multiplier 2  
# Results in: 20 (5 * 2 * 2)

Core Components

Function Resolution (cw.resolution)

The resolution module provides utilities to convert string specifications into callable functions:

  • resolve_to_function: Main resolution function supporting multiple spec formats
  • resolve_func_from_dot_path: Import and resolve functions from dot notation paths
  • parse_json_spec: Parse JSON-formatted function specifications
  • parse_ast_spec: Parse AST-formatted function call specifications

Resource Inputs Decorator

The resource_inputs decorator wraps functions to automatically resolve specified string parameters into actual objects:

from cw.resolution import resource_inputs, parse_ast_spec

def func(apple, banana, carrot):
    return f"{apple=}, {banana=}, {carrot=}"

function_store = {'a': lambda: 1, 'b': lambda: 2}

wrapped_func = resource_inputs(
    func,
    resource=dict(
        apple=None,  # Use default resolve_to_function
        carrot=dict(
            func_key_and_kwargs=parse_ast_spec,
            get_func=function_store.get
        )
    )
)

Examples

Basic Function Resolution

>>> from cw.resolution import resolve_to_function, parse_json_spec, parse_ast_spec

# Direct callable (returned as-is)
>>> resolve_to_function(len)  # doctest: +ELLIPSIS
<built-in function len>

# Simple string (dot path)
>>> length_func = resolve_to_function('builtins.len')
>>> length_func([1, 2, 3])
3

# JSON format
>>> json_spec = '{"func": "len", "params": {}}'
>>> func = resolve_to_function(json_spec, parse_json_spec)
>>> func([1, 2, 3])
3

# Dot path format
>>> func = resolve_to_function("builtins.len")
>>> func([1, 2, 3])
3

Advanced Function Resolution with AST

>>> # AST format
>>> ast_func = resolve_to_function('str.upper()', parse_ast_spec)
>>> ast_func('hello')
'HELLO'

Resource Inputs in Action

>>> def func(apple, banana, carrot):
...     return f"{apple=}, {banana=}, {carrot=}"
>>>
>>> from cw.resolution import parse_ast_spec
>>> function_store = {'a': lambda: 1, 'b': lambda: 2}
>>>
>>> wrapped_func = resource_inputs(
...     func,
...     resource=dict(
...         apple=None,  # Use default resolve_to_function
...         carrot=dict(
...             func_key_and_kwargs=parse_ast_spec,
...             get_func=function_store.get
...         )
...     )
... )

# Now apple will be resolved via resolve_to_function
# carrot will be resolved via resolve_to_function with custom params  
# banana remains unchanged (passed through as-is)

>>> # apple='builtins.len' -> resolve_to_function('builtins.len') -> len function
>>> # banana='test' -> unchanged (no resource specified)
>>> # carrot='a()' -> parsed as AST, resolved via function_store
>>> result = wrapped_func('builtins.len', 'test', 'a()')
>>> 'apple=<built-in function len>' in result
True
>>> "banana='test'" in result
True
>>> 'carrot=<function' in result and 'lambda' in result
True

Parser Functions

Dot Path Parser

>>> from cw.resolution import parse_spec_with_dot_path
>>> parse_spec_with_dot_path('os.path.join')
('os.path.join', {})

>>> parse_spec_with_dot_path('len')  
('len', {})

JSON Parser

>>> from cw.resolution import parse_json_spec
>>> parse_json_spec('{"func": "len", "params": {}}')
('len', {})

>>> parse_json_spec('{"func": "str.replace", "params": {"old": "a", "new": "b"}}')
('str.replace', {'old': 'a', 'new': 'b'})

AST Parser

>>> from cw.resolution import parse_ast_spec
>>> parse_ast_spec('len()')
('len', {})

>>> parse_ast_spec('str.replace(old="a", new="b")')
('str.replace', {'old': 'a', 'new': 'b'})

>>> parse_ast_spec('range(start=0, stop=10)')
('range', {'start': 0, 'stop': 10})

Installation

pip install cw

Key Features

  • CLI-First Design: Built specifically for command-line interface needs
  • Multiple Resolution Formats: Support for dot paths, JSON, and AST expressions
  • Flexible Resource Specifications: Configure resolvers per parameter
  • Function Store Integration: Use custom mappings for function resolution
  • Parameter Binding: Automatic parameter binding with functools.partial
  • Type Safety: Comprehensive validation and clear error messages

Use Cases

  • CLI Tools: Convert string parameters to functions in command-line applications
  • Configuration Systems: Resolve function references from config files
  • Plugin Systems: Dynamically load and configure functions
  • Data Processing Pipelines: Specify transformation functions as strings
  • API Endpoints: Accept function specifications in REST APIs

CW bridges the gap between the string-based world of command-line interfaces and the rich object model of Python, making it easy to build powerful, flexible CLI tools.

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

cw-0.0.15.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

cw-0.0.15-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file cw-0.0.15.tar.gz.

File metadata

  • Download URL: cw-0.0.15.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cw-0.0.15.tar.gz
Algorithm Hash digest
SHA256 2d752d86e6307f2e5f6b129203557580ab54c2d05aa977a3663b5fe460a5085c
MD5 b0003955a568bd024e2dae94dac5cf46
BLAKE2b-256 4c832ebf11474268dbac5412da2df4e486cea9d84590204eb1b0ff94e8ba9a0d

See more details on using hashes here.

File details

Details for the file cw-0.0.15-py3-none-any.whl.

File metadata

  • Download URL: cw-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cw-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 041c3d924bf385a6a4b375d3f66450a7c92f2589e59cdf64516627f3e0f5421d
MD5 0fd11db36af2c14f30f09fa63d326849
BLAKE2b-256 714ad7397c4cb212c129a176a92623c3032b1a09cc5406e630b882f124f78dfe

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