Skip to content

Add a simple --version flag#5516

Merged
rolandtannous merged 6 commits into
unslothai:mainfrom
melroy89:add_version_flag
May 18, 2026
Merged

Add a simple --version flag#5516
rolandtannous merged 6 commits into
unslothai:mainfrom
melroy89:add_version_flag

Conversation

@melroy89

@melroy89 melroy89 commented May 17, 2026

Copy link
Copy Markdown
Contributor

Add a --version flag to the CLI tool, which will try to use the built-in package_version and exit (exit-code zero).

Fixes: #5515

See also: https://typer.tiangolo.com/tutorial/options/version/#first-version-of-version


Example:

python3 cli.py --version                    
unsloth 2026.5.2

Help output:

python3 cli.py --help   
                                                                                                                             
 Usage: cli.py [OPTIONS] COMMAND [ARGS]...                                                                                   
                                                                                                                             
 Command-line interface for Unsloth training, inference, and export.                                                         
                                                                                                                             
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --version             -V        Show version and exit.                                                                    │
│ --install-completion            Install completion for the current shell.                                                 │
│ --show-completion               Show completion for the current shell, to copy it or customize the installation.          │
│ --help                -h        Show this message and exit.                                                               │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ train             Launch training using the existing Unsloth training backend.                                            │
│ inference         Run a single inference using the specified model.                                                       │
│ export            Export a checkpoint to various formats (merged, GGUF, LoRA adapter).                                    │
│ list-checkpoints  List checkpoints detected in the outputs directory.                                                     │
│ run               Alias for `unsloth studio run`.                                                                         │
│ studio            Unsloth Studio commands.                                                                                │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a version flag for the Unsloth CLI, utilizing importlib.metadata to retrieve the package version. The review feedback suggests optimizing the version retrieval by moving it into the callback function to prevent unnecessary execution on every import and adjusting the formatting of the Typer option to match the project's established coding standards.

Comment thread unsloth_cli/__init__.py Outdated
Comment thread unsloth_cli/__init__.py
@melroy89

melroy89 commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

If you do not like the pass.. you can actually drop the help and use the docstring there:

@app.callback()
def main(
    version: bool = typer.Option(
        None,
        "--version",
        "-V",
        callback = show_version,
        is_eager = True,
        help = "Show version and exit.",
    ),
):
    """Command-line interface for Unsloth training, inference, and export."""

Did you know?

This still works then:

python3 cli.py --help
                                                                                                                             
 Usage: cli.py [OPTIONS] COMMAND [ARGS]...                                                                                   
                                                                                                                             
 Command-line interface for Unsloth training, inference, and export.      

@rolandtannous

Copy link
Copy Markdown

checks fine
Screenshot 2026-05-18 at 1 35 06 AM

@rolandtannous rolandtannous merged commit f7bd05a into unslothai:main May 18, 2026
28 checks passed
@melroy89 melroy89 deleted the add_version_flag branch May 18, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add simple --version option

2 participants