CLI Frameworks for C++: How to Build a Console Application from Scratch

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI Frameworks for C++: From Argument Parsing to Full-Fledged Console Utilities



Developing console utilities in C++ is one of the most common tasks programmers face. Whether it's a data processing tool, a compiler, or a DevOps script — proper command-line argument parsing is essential. Manual handling of argc/argv quickly turns into a mess of strcmp and atoi, and supporting options, flags, and subcommands becomes a nightmare. This is where CLI frameworks come to the rescue — libraries that handle all the routine work: declarative interface description, help generation, type validation, and even shell autocompletion.



In this article, we will look at three of the most popular solutions for C++: CLI11, cxxopts, and docopt.cpp. You will learn how to quickly create a professional console application with minimal effort.



1. CLI11 — The Modern Standard



CLI11 (GitHub: CLIUtils/CLI11) is a header-only library for C++11/14/17. It is considered the gold standard due to its concise API and powerful capabilities: support for nested subcommands, range validation, multi-value options, flags, positional arguments, and automatic --help generation.



Installation

Simply copy the header file CLI11.hpp into your project or include it via CMake FetchContent.



Example: Archiving Utility with Options

#include <CLI/CLI11.hpp>#include <iostream>#include <string>

int main(int argc, char** argv) { CLI::App app("Simple Archiver");

std::string input_file; std::string output_file = "archive.zip"; int compression_level = 5; bool verbose = false;

app.add_option("-i,--input", input_file, "Input file")->required(); app.add_option("-o,--output", output_file, "Output file"); app.add_option("-c,--compression", compression_level, "Compression level (1-9)") ->check(CLI::Range(1, 9)); app.add_flag("-v,--verbose", verbose, "Verbose output");

CLI11_PARSE(app, argc, argv);

std::cout << "Archiving: " << input_file << ""; std::cout << "Output: " << output_file << ""; std::cout << "Level: " << compression_level << ""; if (verbose) std::cout << "Debug mode enabled"; return 0;}


Run: ./archiver -i data.txt -o backup.zip -c 9 -v
On error or --help, CLI11 will automatically display a nicely formatted description.



2. cxxopts — Lightweight and Simple



cxxopts (GitHub: jarro2783/cxxopts) is another header-only library. It is simpler than CLI11, but covers 90% of needs: short/long options, default values, lists, and help. Ideal for small utilities.



Example: Reading Configuration

#include <cxxopts.hpp>#include <iostream>

int main(int argc, char* argv[]) { cxxopts::Options options("greeter", "Greeting Program");

options.add_options() ("n,name", "User name", cxxopts::value<std::string>()) ("a,age", "Age", cxxopts::value<int>()->default_value("0")) ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false")) ("h,help", "Help");

auto result = options.parse(argc, argv);

if (result.count("help")) { std::cout << options.help() << ""; return 0; }

std::string name = result["name"].as<std::string>(); int age = result["age"].as<int>(); bool verbose = result["verbose"].as<bool>();

std::cout << "Hello, " << name << "!"; if (verbose) std::cout << "Age

Recommendations