CLI frameworks for C++: writing console applications quickly and conveniently

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++: writing console applications quickly and conveniently



Creating a high-quality command-line interface (CLI) is an important part of C++ development. Manually parsing arguments via argc/argv and getopt quickly becomes cumbersome and error-prone, especially with a large number of options. Specialized libraries — CLI frameworks — come to the rescue. They automate parsing, help generation, type checking, and help you write cleaner, more maintainable code.



In this article, we will look at three of the most popular and modern CLI frameworks for C++: CLI11, cxxopts, and argparse. You will learn their strengths, see code examples, and be able to choose the right tool for your project.



1. CLI11 — the modern standard



CLI11 is a header-only library that is rightfully considered one of the best solutions for C++11 and newer. It offers an intuitive API, powerful validation, and automatic help generation.



Key features:

  • Header-only: no build required — just include the header file.
  • Automatic help: the -h and --help flags work immediately.
  • Validation: built-in and custom validators for arguments (range, regular expression, file existence).
  • Subcommand support: ideal for complex utilities (like git or docker).
  • Formatting: customizable help output.


Example of using CLI11:

A simple application that accepts a name and an optional greeting flag:

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

int main(int argc, char** argv) { CLI::App app{"CLI11 Demo"};

std::string name; bool greet = false;

app.add_option("-n,--name", name, "Your name")->required(); app.add_flag("-g,--greet", greet, "Print a greeting");

CLI11_PARSE(app, argc, argv);

std::cout << "Hello, " << name << "!" << std::endl; if (greet) { std::cout << "Have a nice day!" << std::endl; } return 0;}

How it works: The library itself parses the arguments, checks the mandatory --name option, and generates help when calling ./app --help.



2. cxxopts — simplicity and lightweight



cxxopts is another header-only library focused on minimalism and simplicity. It is great for small utilities where subcommands are not needed, but code readability is important.



Key features:

  • Simple API: adding options via method chaining.
  • Automatic type conversion: int, double, string, and others.
  • Positional argument support.
  • Customizable prefixes: you can use - or --.


Example of using cxxopts:

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

int main(int argc, char** argv) { cxxopts::Options options("myapp", "cxxopts example");

options.add_options() ("f,file", "File name", cxxopts::value<std::string>()) ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false")) ("h,help", "Show help");

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

if (result.count("help")) { std::cout << options.help() << std::endl;

Recommendations