Skip to main content

argklass

Current PyPi Version Supported Python Versions codecov docs tests style

Inspired by Simple Parsing, simplified and extended to build extensive, extendable command line interface without much code.

pip install argklass

Features

  • Automatic cli discovery and command plugin

    # Folder structure
    project/cli/
    ├── __init__.py         <= empty
    ├── editor/
    │   ├── __init__.py     <= ParentCommand(editor)
    │   ├── cook.py         <= Command(cook)
    │   ├── client.py       <= Command(client)
    │   ├── game.py         <= Command(game)
    │   └── open.py         <= Command(open)
    └── uat/
       ├── __init__.py     <= ParentCommand(uat)
       ├── localize.py     <= Command(localize)
       └── test.py         <= Command(test)
    
    #  editor/__init__.py
    from argklass.command import ParentCommand
    
    class Editor(ParentCommand):
       name = "editor"
    
    
    COMMANDS = Editor
    
    # cook.py
    from argklass.command import Command
    
    class Cook(Command):
       name = "cook"
    
       @staticmethod
       def execute(args) -> int:
          print("cook")
    
    COMMANDS = Cook
    
    #
    cli = CommandLineInterface(project.cli)
    cli.run()
    
    # or
    cli.run(["editor", "cook", "--help"])
    
    #
    cli editor cook --help
    cli uat localize --help
  • New Argument format
    • able to show the entire command line interface with all its subparsers

    • new format mirror dataclass syntax

    editor                                           Set of commands to launch the editors in different modes
       server                                       Parameters added to the Map URL
       game                                         docstring ...
       client                                       docstring ...
       resavepackages                               docstring ...
       cook                                         docstring ...
       ml                                           Launch unreal engine with mladapter setup
       editor                                       Other arguments
       open                                         docstring ...
       localize                                     docstring ...
       worldpartition                               Convert a UE4 map using world partition
       -h, --help                                   Show help
    engine                                           Set of commands to manage engine installation/source
          add                                          docstring ...
          update                                       Update the engine source code
    format                                             docstring ...
          --profile: str                               docstring ...
          --file: str                                  docstring ...
          --fail_on_error: bool = False                docstring ...
          --col: int = 24                              docstring ...
  • Compact argparse definition

    def workdir():
       d = os.getcwd()
       if os.access(d, os.W_OK):
          return d
       return None
    
    
    @dataclass
    class MyArguments:
       a  : str                                                    # Positional
       b  : int                = 20                                # My argument
       c  : bool               = False                             # My argument
       d  : int                = choice(0, 1, 2, 3, 4, default=1)  # choices
       e  : List[int]          = argument(default=[0])             # list
       f  : Optional[int]      = None                              # Optional
       p  : Tuple[int, int]    = (1, 1)                            # help p
       g  : Color              = Color.RED                         # help g
       s  : SubArgs            = SubArgs                           # helps group
       cmd: Union[cmd1, cmd2]  = subparsers(cmd1=cmd1, cmd2=cmd2)  # Command subparser
       de : str                = deduceable(workdir)
    
    parser = ArgumentParser()
    parser.add_arguments(MyArguments)
    args = parser.parse_args()
  • Save and load arguments from configuration files

    parser = build_parser(commands)
    
    # load/save defaults before parsing
    save_defaults(parser, "config.hjson")
    apply_defaults(parser, "config.hjson")
    
    args = parser.parse_args(["editor", "editor"])
    
    # load save arguments after parsing
    save_as_config(parser, args, "dump.hjson")
    apply_config(parser, args, "dump.hjson")
  • Lower level interface, that gives you back all of argparse power

    @dataclass
    class SubArgs:
       aa: str = argument(default="123")
    
    
    @dataclass
    class cmd1:
       args: str = "str1"
    
    
    @dataclass
    class cmd2:
       args: str = "str2"
    
    
    @dataclass
    class MyArguments:
       a: str                  = argument(help="Positional")
       b: int                  = argument(default=20, help="My argument")
       c: bool                 = argument(action="store_true", help="My argument")
       d: int                  = argument(default=1, choices=[0, 1, 2, 3, 4], help="choices")
       e: List[int]            = argument(default=[0], help="list")
       f: Optional[int]        = argument(default=None, help="Optional")
       p: Tuple[int, int]      = argument(default=(1, 1), help="help p")
       g: Color                = argument(default=Color.RED, help="help g")
       s: SubArgs              = group(default=SubArgs, help="helps group")
       cmd: Union[cmd1, cmd2]  = subparsers(cmd1=cmd1, cmd2=cmd2)
    
    
    parser = ArgumentParser()
    parser.add_arguments(MyArguments)
    args = parser.parse_args()

MCP Server

argklass can expose your CLI commands as MCP tools, letting AI agents call them directly.

pip install "argklass[mcp]"

Quick start

The fastest way to run an MCP server is the built-in entry point — just point it at your CLI module:

# stdio (default) — for MCP clients that spawn the process
python -m argklass.mcp mypackage.cli

# SSE — for testing or web-based clients
python -m argklass.mcp mypackage.cli --transport sse

# Streamable HTTP — the newest MCP transport
python -m argklass.mcp mypackage.cli --transport streamable-http

# Custom host/port/name
python -m argklass.mcp mypackage.cli --transport sse --host 0.0.0.0 --port 9000 --name my-tools

Programmatic usage

For more control, use create_mcp_server directly:

import mycommands
from argklass.mcp import create_mcp_server

server = create_mcp_server(mycommands, name="my-tools")

# inspect discovered tools
for tool in server.tools:
    print(tool.name, tool.schema)

# run as a stdio MCP server
server.run()

# or run with SSE
server.run(transport="sse", host="0.0.0.0", port=9000)

The server walks the parser tree, discovers every leaf command, converts its arguments to JSON Schema, and registers them as MCP tools. When a tool is invoked, the arguments are converted back to argv and the command runs normally — stdout, stderr and exit code are returned to the caller.

You can also call tools directly for testing:

output = server.call("editor_cook", {"--dry-run": True})

Configuration (sysconfig)

argklass.sysconfig lets you define configuration as dataclasses, with values resolved from environment variables, a config dict, or defaults (in that priority order).

from dataclasses import dataclass, field
from argklass.sysconfig import ConfigContext

ctx = ConfigContext(prefix="MYAPP")

@dataclass
class DatabaseConfig:
    host: str = ctx.configfield("db.host", str, "localhost")   # MYAPP_DB_HOST
    port: int = ctx.configfield("db.port", int, 5432)          # MYAPP_DB_PORT

@dataclass
class AppConfig:
    debug: bool = ctx.configfield("app.debug", bool, False)    # MYAPP_APP_DEBUG
    db: DatabaseConfig = field(default_factory=DatabaseConfig)

Each field is resolved at instantiation time. Override via environment:

export MYAPP_DB_HOST=db.prod.internal
export MYAPP_DB_PORT=5433

Or programmatically with a config dict:

ctx.set_config({"db": {"host": "db.staging.internal"}})
cfg = AppConfig()   # cfg.db.host == "db.staging.internal"

File I/O supports YAML, JSON and HJSON:

# save / load
ctx.save_config(cfg, "config.yaml")
cfg = ctx.load_config(AppConfig, "config.yaml")

# load and apply as the context's config dict
ctx.load_and_apply("overrides.yaml")

When several libraries use argklass in the same process, each one creates its own ConfigContext with a unique prefix, keeping environment variables and config dicts fully isolated.

Architecture

argklass works by building the argument parser as a tree, adding metadata to each nodes when necessary.

One of the core component is ArgumentParserIterator which traverse the parsing tree. Each features, such as argument grouping into dataclasses or saving/loading configuration, are implemented as a simple traversal.

This enable us to implement each feature independently from each other and make them optional.

Metadata

Release files for argklass 2.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argklass 2.4.0
File Size Uploaded
argklass-2.4.0.tar.gz 40.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argklass 2.4.0
File Interpreter ABI Platform
argklass-2.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 79.8 kB

Release files / argklass-2.4.0.tar.gz

Download URL argklass-2.4.0.tar.gz
Size 40.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7f5c57b60a92ffc77b0501ee83aa5e0d10cec10209fbe5d8409836bf5fbc73ec
BLAKE2b-256 checksum
How to use checksums
7d4ada26df9553800d7a365221f2992d754855ce678489bf3285afaea59bfdbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / argklass-2.4.0-py3-none-any.whl

Download URL argklass-2.4.0-py3-none-any.whl
Size 39.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8e26acb2048bcb88d5b5d9e2e867083b678ec396e95ff24c397e0939a5f8f15
BLAKE2b-256 checksum
How to use checksums
8cc43006b1ff774af63ab87edd280399606222de4e0aad359c44756c65956333
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

2.4.0 This release

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page