Implementation:Huggingface Optimum BaseOptimumCLICommand
| Knowledge Sources | |
|---|---|
| Domains | CLI, Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for defining and registering CLI commands and subcommands provided by the Huggingface Optimum library.
Description
The BaseOptimumCLICommand class is the abstract base class for all `optimum-cli` commands. It provides a framework for declaratively defining CLI commands using CommandInfo dataclasses and supports hierarchical subcommand registration via argparse. RootOptimumCLICommand serves as the top-level CLI entry point.
CommandInfo is a frozen dataclass that holds a command's name, help text, optional subcommand class, and formatter class. BaseOptimumCLICommand uses these to automatically create argparse parsers and wire up command dispatch through `parser.set_defaults(func=...)`.
Usage
Import and subclass BaseOptimumCLICommand when creating a new `optimum-cli` subcommand. Define a `COMMAND` class attribute as a CommandInfo instance, implement `parse_args()` to add arguments, and implement `run()` to execute the command logic.
Code Reference
Source Location
- Repository: Huggingface_Optimum
- File: optimum/commands/base.py
- Lines: 1-148
Signature
@dataclass(frozen=True)
class CommandInfo:
name: str
help: str
subcommand_class: Optional[Type["BaseOptimumCLICommand"]] = None
formatter_class: Type = RawTextHelpFormatter
class BaseOptimumCLICommand(ABC):
COMMAND: CommandInfo
SUBCOMMANDS: Tuple[CommandInfo, ...] = ()
def __init__(
self,
subparsers: Optional["_SubParsersAction"],
args: Optional["Namespace"] = None,
command: Optional[CommandInfo] = None,
from_defaults_factory: bool = False,
parser: Optional["ArgumentParser"] = None,
):
"""
Args:
subparsers: The parent subparsers this command will create its parser on.
args: The parsed arguments for self.run().
command: The command info for this instance.
from_defaults_factory: Skip parser creation when True.
parser: Existing parser instance (required when from_defaults_factory=True).
"""
@staticmethod
def parse_args(parser: "ArgumentParser"):
pass
def register_subcommand(self, command_info: CommandInfo):
...
def run(self):
...
class RootOptimumCLICommand(BaseOptimumCLICommand):
COMMAND = CommandInfo(name="root", help="optimum-cli root command")
def __init__(self, cli_name: str, usage: Optional[str] = None, args: Optional["Namespace"] = None):
...
Import
from optimum.commands.base import CommandInfo, BaseOptimumCLICommand, RootOptimumCLICommand
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| subparsers | _SubParsersAction | Yes | Parent argparse subparsers action to attach this command |
| args | Namespace | No | Parsed CLI arguments for execution |
| command | CommandInfo | No | Command metadata (name, help text, subcommand class) |
| from_defaults_factory | bool | No | When True, skips parser creation (used internally) |
| parser | ArgumentParser | No | Required when from_defaults_factory is True |
Outputs
| Name | Type | Description |
|---|---|---|
| parser | ArgumentParser | The argparse parser for this command |
| run() | None | Executes the command logic (or prints help for non-leaf commands) |
Usage Examples
Defining a Custom Subcommand
from optimum.commands.base import BaseOptimumCLICommand, CommandInfo
class MyCommand(BaseOptimumCLICommand):
COMMAND = CommandInfo(name="mycommand", help="My custom optimum-cli command")
@staticmethod
def parse_args(parser):
parser.add_argument("--model", type=str, required=True, help="Model name or path")
parser.add_argument("--output", type=str, required=True, help="Output directory")
def run(self):
print(f"Running with model={self.args.model}, output={self.args.output}")