Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
feat(help): list all accepted llama.cpp options in --help (#1003)
Richard Tweed84e3e1ae0f7f
diff --git a/llamafile/main.cpp b/llamafile/main.cppindex 898ef179..699c90d5 100644--- a/llamafile/main.cpp+++ b/llamafile/main.cpp@@ -30,6 +30,7 @@//#include "args.h"+#include "arg.h" // common_params_parse, llama_example (for --help delegation)#include "chatbot.h"#include "llamafile.h"#include "version.h"@@ -101,7 +102,62 @@ extern int server_main(int argc, char **argv,std::function<void(const std::string &)> on_ready,std::function<void(std::function<void()>)> on_shutdown_available);-static void print_general_help() {+// ---------------------------------------------------------------------------+// --help implementation+//+// llamafile's --help is built in two layers:+//+// 1. A short, hand-written intro (the print_*_help functions below) that+// describes llamafile's own execution modes and the llamafile-specific+// flags (--gpu, --chat, --cli, --server, --nologo, --ascii, --nothink)+// that llama.cpp does not know about.+//+// 2. The full, categorized catalogue of llama.cpp options (-m, -c, --temp,+// --top-k, --top-p, --repeat-penalty, -ngl, --port, ...) which is printed+// by llama.cpp's own argument parser via print_llama_cpp_usage().+//+// Delegating layer (2) to common_params_parse() — the exact same code path+// that --server --help already uses — is what guarantees --help lists every+// flag llamafile actually accepts. A hand-maintained copy would drift, which+// is the regression this design avoids.+//+// common_params_parse() prints the usage and calls exit(0) when --help/-h is+// present in argv. On a parse error (e.g. a malformed env var) it returns+// false without exiting; in that case we exit(0) too, since the user only+// asked for help.+// ---------------------------------------------------------------------------++static void print_llama_cpp_usage(lf::ProgramMode mode,+ const lf::LlamafileArgs & args) {+ common_params params;+ llama_example ex;+ switch (mode) {+ case lf::ProgramMode::CHAT:+ case lf::ProgramMode::CLI:+ // --chat and --cli feed args to the chatbot, which parses them+ // with LLAMA_EXAMPLE_CLI.+ ex = LLAMA_EXAMPLE_CLI;+ break;+ case lf::ProgramMode::SERVER:+ case lf::ProgramMode::AUTO:+ default:+ // --server and the default (combined) mode feed args to the HTTP+ // server, which parses them with LLAMA_EXAMPLE_SERVER.+ ex = LLAMA_EXAMPLE_SERVER;+ break;+ }+ // Prints usage and exits(0) on --help; returns false only on parse error.+ if (!common_params_parse(args.llama_argc, args.llama_argv, params, ex, nullptr)) {+ exit(0);+ }+ // Unreachable on the happy path: common_params_parse() exits above.+}++// show_full_list_hint is true when called from the "missing -m" error path,+// where we point the user at `llamafile --help` for the full option list. It+// is false when called from the --help path, where the full llama.cpp option+// list is appended immediately afterwards by print_llama_cpp_usage().+static void print_general_help(bool show_full_list_hint) {printf("llamafile v" LLAMAFILE_VERSION_STRING " - run LLMs locally\n""\n""usage: llamafile -m MODEL.gguf [options]\n"@@ -112,28 +168,56 @@ static void print_general_help() {" --chat TUI chat only (no server)\n"" --cli single prompt/response (requires -p)\n""\n"- "common options:\n"- " -m FILE path to GGUF model file (required)\n"- " -p TEXT system prompt (in --cli mode: user prompt)\n"+ "llamafile options:\n"" --gpu MODE GPU backend (auto, nvidia, amd, apple, disable)\n"- " -ngl N number of layers to offload to GPU (default: auto)\n"+ " --nologo suppress the startup logo\n"+ " --ascii use ASCII art instead of Unicode for logo\n"" --verbose enable verbose logging\n"" --version show version information\n"" --help show this help\n"- "\n"- "for mode-specific help and options:\n"- " llamafile --server --help\n"- " llamafile --chat --help\n"- " llamafile --cli --help\n"- "\n"- "examples:\n"+ "\n");+ if (show_full_list_hint) {+ printf("llamafile also accepts all llama.cpp options (e.g. -m, -p, -c, -t,\n"+ "--temp, --top-k, --top-p, --repeat-penalty, -ngl, --port, ...).\n"+ "Run `llamafile --help` for the full categorized list, or add --help\n"+ "to a mode for mode-specific options:\n"+ " llamafile --server --help\n"+ " llamafile --chat --help\n"+ " llamafile --cli --help\n"+ "\n");+ }+ printf("examples:\n"" llamafile -m model.gguf\n"" llamafile -m model.gguf --server --port 8080\n"" llamafile -m model.gguf --chat\n"" llamafile -m model.gguf --cli -p \"explain quantum computing\"\n");}-static void print_chat_help() {+static void print_server_help(bool show_full_list_hint) {+ printf("llamafile --server - HTTP server mode\n"+ "\n"+ "usage: llamafile -m MODEL.gguf --server [options]\n"+ "\n"+ "Start an OpenAI-compatible HTTP server (no TUI). Serves a browser\n"+ "web UI and a REST API for chat completions, embeddings, and more.\n"+ "\n"+ "server-specific options:\n"+ " --host ADDR ip address to listen on (default 127.0.0.1)\n"+ " --port N tcp port to listen on (default 8080)\n"+ " --api-key KEY require this API key for authentication\n"+ "\n");+ if (show_full_list_hint) {+ printf("all other llama.cpp options are also accepted.\n"+ "run `llamafile --server --help` to see the full list.\n"+ "\n");+ }+ printf("examples:\n"+ " llamafile -m model.gguf --server\n"+ " llamafile -m model.gguf --server --port 8080\n"+ " llamafile -m model.gguf --server --host 0.0.0.0 --port 8080\n");+}++static void print_chat_help(bool show_full_list_hint) {printf("llamafile --chat - TUI chat mode\n""\n""usage: llamafile -m MODEL.gguf --chat [options]\n"@@ -162,17 +246,19 @@ static void print_chat_help() {" /undo erase last exchange\n"" /forget erase oldest message\n"" /exit quit\n"- "\n"- "all other llama.cpp options are also accepted.\n"- "run llamafile --server --help to see the full list.\n"- "\n"- "examples:\n"+ "\n");+ if (show_full_list_hint) {+ printf("all other llama.cpp options are also accepted.\n"+ "run `llamafile --chat --help` to see the full list.\n"+ "\n");+ }+ printf("examples:\n"" llamafile -m model.gguf --chat\n"" llamafile -m model.gguf --chat -p \"You are a helpful assistant\"\n"" llamafile -m model.gguf --chat --mmproj mmproj.gguf\n");}-static void print_cli_help() {+static void print_cli_help(bool show_full_list_hint) {printf("llamafile --cli - single prompt/response mode\n""\n""usage: llamafile -m MODEL.gguf --cli -p \"prompt\" [options]\n"@@ -187,11 +273,13 @@ static void print_cli_help() {"multimodal options:\n"" --mmproj FILE path to vision model (mmproj GGUF)\n"" --image FILE image file(s) to include with prompt\n"- "\n"- "all other llama.cpp options are also accepted.\n"- "run llamafile --server --help to see the full list.\n"- "\n"- "examples:\n"+ "\n");+ if (show_full_list_hint) {+ printf("all other llama.cpp options are also accepted.\n"+ "run `llamafile --cli --help` to see the full list.\n"+ "\n");+ }+ printf("examples:\n"" llamafile -m model.gguf --cli -p \"explain quantum computing\"\n"" llamafile -m model.gguf --cli --nothink -p \"write a haiku\"\n"" llamafile -m model.gguf --cli --mmproj mm.gguf --image photo.jpg -p \"describe this\"\n");@@ -298,20 +386,30 @@ int main(int argc, char **argv) {lf::LlamafileArgs args = lf::parse_llamafile_args(argc, argv);// Handle --help for llamafile modes.- // --server --help falls through to llama.cpp's help system.+ // --server --help falls through to llama.cpp's help system (server_main).+ // All other modes print llamafile's intro (modes + llamafile-specific+ // flags) and then delegate to llama.cpp's argument parser to print the+ // full, categorized list of accepted options (common/sampling/spec/+ // example-specific). This keeps --help in sync with what llamafile+ // actually accepts instead of maintaining a hand-written copy.if (llamafile_has(argv, "--help") || llamafile_has(argv, "-h")) {switch (args.mode) {case lf::ProgramMode::SERVER:- break; // fall through to llama.cpp's help+ break; // fall through to llama.cpp's server helpcase lf::ProgramMode::AUTO:- print_general_help();- return 0;+ // Combined mode: args go to the HTTP server, so show the+ // llamafile intro followed by llama.cpp's server option list.+ print_general_help(false);+ print_llama_cpp_usage(args.mode, args);+ return 0; // unreachable; print_llama_cpp_usage() exitscase lf::ProgramMode::CHAT:- print_chat_help();- return 0;+ print_chat_help(false);+ print_llama_cpp_usage(args.mode, args);+ return 0; // unreachable; print_llama_cpp_usage() exitscase lf::ProgramMode::CLI:- print_cli_help();- return 0;+ print_cli_help(false);+ print_llama_cpp_usage(args.mode, args);+ return 0; // unreachable; print_llama_cpp_usage() exits}}@@ -323,16 +421,16 @@ int main(int argc, char **argv) {fprintf(stderr, "error: missing required -m MODEL.gguf\n\n");switch (args.mode) {case lf::ProgramMode::SERVER:- print_general_help();+ print_server_help(true);break;case lf::ProgramMode::AUTO:- print_general_help();+ print_general_help(true);break;case lf::ProgramMode::CHAT:- print_chat_help();+ print_chat_help(true);break;case lf::ProgramMode::CLI:- print_cli_help();+ print_cli_help(true);break;}return 1;diff --git a/tests/integration/pyproject.toml b/tests/integration/pyproject.tomlindex 909ff087..8b47aa39 100644--- a/tests/integration/pyproject.toml+++ b/tests/integration/pyproject.toml@@ -26,6 +26,7 @@ markers = ["tui: tests TUI mode","cli: tests CLI mode","combined: tests combined (TUI+server) mode",+ "help: tests --help output (no model required)","ssl: tests HTTPS/TLS support (serving and model download)","online: tests that need network access (deselect with '-m \"not online\"')",]diff --git a/tests/integration/tests/test_help.py b/tests/integration/tests/test_help.pynew file mode 100644index 00000000..5f540634--- /dev/null+++ b/tests/integration/tests/test_help.py@@ -0,0 +1,166 @@+"""--help output integration tests.++These tests do not need a model: they only invoke ``llamafile --help`` (and+mode-specific variants) and assert that the documented llama.cpp options+(e.g. ``--top-k``, ``--temperature``, ``--ctx-size``) actually appear.++This guards against the regression where --help showed a hand-written,+incomplete option list that drifted from what llamafile accepts. The fix+delegates the full option list to llama.cpp's own argument parser, so these+tests pin that delegation in place.+"""++import platform+import subprocess++import pytest+++HELP_TIMEOUT = 30.0+++def _run_help(executable, *extra_args):+ """Run `llamafile [extra_args...] --help` and return CompletedProcess.++ On non-Windows we prepend `sh` so the cosmopolitan APE polyglot header+ is interpreted correctly (matches LlamafileRunner._base_args in+ utils/llamafile.py). Plain subprocess execve of an APE on macOS fails+ with ENOEXEC.+ """+ if platform.system() == "Windows":+ cmd = [executable, *extra_args, "--help"]+ else:+ cmd = ["sh", executable, *extra_args, "--help"]+ return subprocess.run(+ cmd,+ stdin=subprocess.DEVNULL,+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ text=True,+ timeout=HELP_TIMEOUT,+ )+++# Sampling / common llama.cpp options that must appear in every --help variant.+# These are the args users historically complained were missing from --help.+REQUIRED_LLAMACPP_ARGS = [+ "--top-k",+ "--top-p",+ "--temperature",+ "--ctx-size",+ "--repeat-penalty",+ "-ngl",+]++# llamafile-specific flags that llama.cpp does not know about. Only the+# general (no-mode) --help lists ALL of them; mode-specific help focuses on+# that mode's own flags. --server --help falls through to llama.cpp's native+# server help, which does not mention llamafile at all (pre-existing behavior).+MODE_LLAMAFILE_FLAGS = {+ (): ["--gpu", "--chat", "--cli", "--server"], # general / AUTO+ ("--server",): [], # llama.cpp native help+ ("--chat",): ["--chat", "--nologo", "--ascii"],+ ("--cli",): ["--cli", "--nothink"],+}++# Modes whose --help output identifies itself as "llamafile" (i.e. prints+# llamafile's own intro). --server --help is pure llama.cpp help.+MODES_WITH_LLAMAFILE_INTRO = [(), ("--chat",), ("--cli",)]+++class TestHelpBasic:+ """Basic --help behavior shared by all modes."""++ @pytest.mark.parametrize("mode_args", [+ [],+ ["--server"],+ ["--chat"],+ ["--cli"],+ ])+ def test_help_exits_zero(self, executable, mode_args):+ """--help must exit 0 in every mode (no model required)."""+ result = _run_help(executable, *mode_args)+ assert result.returncode == 0, (+ f"{' '.join(mode_args)} --help exited {result.returncode}\n"+ f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"+ )++ @pytest.mark.parametrize("mode_args", MODES_WITH_LLAMAFILE_INTRO)+ def test_help_mentions_llamafile(self, executable, mode_args):+ """Modes that print llamafile's intro must identify themselves as llamafile."""+ result = _run_help(executable, *mode_args)+ assert "llamafile" in result.stdout, (+ f"{' '.join(mode_args)} --help stdout does not mention llamafile:\n{result.stdout}"+ )++ @pytest.mark.parametrize("mode_args", [+ [],+ ["--server"],+ ["--chat"],+ ["--cli"],+ ])+ def test_help_lists_llamafile_flags(self, executable, mode_args):+ """Each --help variant must document the llamafile-specific flags+ relevant to that mode (general help lists all of them)."""+ expected = MODE_LLAMAFILE_FLAGS[tuple(mode_args)]+ result = _run_help(executable, *mode_args)+ for flag in expected:+ assert flag in result.stdout, (+ f"{' '.join(mode_args)} --help is missing llamafile flag {flag!r}\n"+ f"stdout:\n{result.stdout}"+ )+++class TestHelpListsLlamaCppArgs:+ """The regression this project fixed: --help must list llama.cpp options+ such as --top-k, --temperature, --ctx-size, etc., not just a sparse+ hand-written subset."""++ @pytest.mark.parametrize("mode_args", [+ [],+ ["--server"],+ ["--chat"],+ ["--cli"],+ ])+ def test_help_lists_sampling_and_common_args(self, executable, mode_args):+ """Sampling/common llama.cpp options must appear in every --help variant."""+ result = _run_help(executable, *mode_args)+ missing = [a for a in REQUIRED_LLAMACPP_ARGS if a not in result.stdout]+ assert not missing, (+ f"{' '.join(mode_args)} --help is missing llama.cpp options {missing}\n"+ f"stdout:\n{result.stdout}"+ )+++class TestHelpMissingModel:+ """The missing-model error path should still print a helpful intro and+ point the user at --help, without requiring the full option list."""++ def _run_no_model(self, executable):+ if platform.system() == "Windows":+ cmd = [executable]+ else:+ cmd = ["sh", executable]+ return subprocess.run(+ cmd,+ stdin=subprocess.DEVNULL,+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ text=True,+ timeout=HELP_TIMEOUT,+ )++ def test_missing_model_exits_nonzero(self, executable):+ """Running with no -m (and no --help) must exit non-zero."""+ result = self._run_no_model(executable)+ assert result.returncode != 0, "missing -m should not exit 0"++ def test_missing_model_mention_help(self, executable):+ """The missing-model error should mention -m and --help."""+ result = self._run_no_model(executable)+ combined = result.stdout + result.stderr+ assert "-m" in combined+ assert "--help" in combined