![]() |
CLI11 2.6.2
C++11 Command Line Interface Parser
|
This example uses the single file edition of CLI11. You can download CLI11.hpp from the latest release and put it into the same folder as your source code, then compile this with C++ enabled. For a larger project, you can just put this in an include folder and you are set. This is the simplest and most straightforward means of including CLI11 with a project.
If you want to use CLI11 in its full form, you can also use the original multiple file edition. This has an extra utility (Timer), and is does not require that you use a release. The only change to your code would be the include shown above.
If you use CMake 3.14+ for your project (highly recommended), CLI11 comes with a powerful CMakeLists.txt file that was designed to also be used with add_subdirectory. You can add the repository to your code (preferably as a git submodule), then add the following line to your project (assuming your folder is called CLI11):
Then, you will have a target CLI11::CLI11 that you can link to with target_link_libraries. It will provide the include paths you need for the library. This is the way GooFit uses CLI11, for example.
You can also configure and optionally install CLI11, and CMake will create the necessary lib/cmake/CLI11/CLI11Config.cmake files, so find_package(CLI11 CONFIG REQUIRED) also works.
If you use conan.io, CLI11 supports that too. CLI11 also supports Meson and pkg-config if you are not using CMake.
If the CMake option CLI11_PRECOMPILED is set then the library is compiled into a static library. This can be used to improve compile times if CLI11 is included in many different parts of a project.
Use CLI/*.hpp files stored in a shared folder. You could check out the git repository to a system-wide folder, for example /opt/. With CMake, you could add to the include path via:
And then in the source code (adding several headers might be needed to prevent linker errors):
Configuring and installing the project is required for linking CLI11 to your project in the same way as you would do with any other external library. With CMake, this step allows using find_package(CLI11 CONFIG REQUIRED) and then using the CLI11::CLI11 target when linking. If CMAKE_INSTALL_PREFIX was changed during install to a specific folder like /opt/CLI11, then you have to pass -DCLI11_DIR=/opt/CLI11 when building your current project. You can also use Conan.io or Hunter. (These are just conveniences to allow you to use your favorite method of managing packages; it's just header only so including the correct path and using C++11 is all you really need.)
When using modules, you must be using C++20 or later. CMake supports modules with the Ninja generator, and with the Visual Studio generator in CMake 3.28+ (Makefiles won't work). When enabling CLI11_MODULES, you will have a target CLI11::Modules for linking.
If you do not want to add cmake as a submodule or include it with your code the project can be added using FetchContent. This capability requires CMake 3.14+ (or 3.11+ with more work).
An example CMake file would include:
And use
in your project. It is highly recommended that you use the git hash for GIT_TAG instead of a tag or branch, as that will both be more secure, as well as faster to reconfigure - CMake will not have to reach out to the internet to see if the tag moved. You can also download just the single header file from the releases using file(DOWNLOAD).
CLI11 has examples and tests that can be accessed using a CMake build on any platform. Simply build and run ctest to run the 200+ tests to ensure CLI11 works on your system.
The test build uses Catch2. If Catch2 is not available as a CMake package, CLI11 will download the required Catch2 header during configuration. On systems without internet access, or where TLS connections to GitHub releases are blocked, install Catch2 separately or configure with CLI11_BUILD_TESTS=OFF until the dependency is available.
As an example of the build system, the following commands will download and test CLI11 in a simple Alpine Linux docker container. (Docker is being used to create a pristine disposable environment; there is nothing special about this container. Alpine is being used because it is small, modern, and fast. Commands are similar on any other platform.)
For the curious, the CMake options and defaults are listed below. Most options default to off if CLI11 is used as a subdirectory in another project.
| Option | Description |
|---|---|
| CLI11_SINGLE_FILE=OFF | Build the CLI11.hpp file from the sources. Requires Python. |
| CLI11_PRECOMPILED=OFF | Generate a precompiled library instead of header-only |
| CLI11_MODULES=OFF | Build CLI11 as a module (requires C++20 or later) |
| CLI11_INSTALL_PACKAGE_TESTS=OFF | Run tests checking the installation |
| CLI11_MODULE_TESTS=OFF | Run a test checking that CLI11 works with modules |
| CLI11_SINGLE_FILE_TESTS=OFF | Run the tests on the generated single file version as well |
| CLI11_BUILD_DOCS=ON | Build CLI11 documentation and book |
| CLI11_BUILD_EXAMPLES=ON | Build the example programs. |
| CLI11_BUILD_EXAMPLES_JSON=ON | Build some additional example using json libraries |
| CLI11_INSTALL=ON | Install CLI11 to the install folder during the install process |
| CLI11_FULL_INSTALL=OFF | Install all CLI11 headers/libraries regardless of other settings |
| CLI11_FORCE_LIBCXX=OFF | Use libc++ instead of libstdc++ if building with clang on linux |
| CLI11_DISABLE_IMPL_HEADERS_INSTALL | Don't install the impl headers if the CLI11_PRECOMPILED is ON |
| CLI11_CUDA_TESTS=OFF | Build the tests with NVCC |
| CLI11_BUILD_TESTS=ON | Build the tests. |
If CLI11 is installed globally, then nothing more than dependency('CLI11') is required. If it installed in a non-default search path, then setting the PKG_CONFIG_PATH environment variable of the --pkg-config-path option to meson setup is all that's required.
Meson has a system called wraps, which allow Meson to fetch sources, configure, and build dependencies as part of a main project. This is the mechanism that Meson recommends for projects to use, as it allows updating the dependency transparently, and allows packagers to have fine grained control on the use of subprojects vs system provided dependencies. Simply run meson wrap install cli11 to install the cli11.wrap file, and commit it, if desired.
It is also possible to use git submodules. This is generally discouraged by Meson upstream, but may be appropriate if a project needs to build with multiple build systems and wishes to share subprojects between them. As long as the submodule is in the parent project's subproject directory nothing additional is needed.
You can download and install cli11 using the vcpkg dependency manager:
The cli11 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
You can install pre-built binaries for CLI11 or build it from source using Conan. Use the following command:
The CLI11 Conan recipe is kept up to date by Conan maintainers and community contributors. If the version is out of date, please create an issue or pull request on the ConanCenterIndex repository.
If you are using GCC 8 and using it in C++17 mode with CLI11. CLI11 makes use of the <filesystem> header if available, but specifically for this compiler, the filesystem library is separate from the standard library and needs to be linked separately. So it is available but CLI11 doesn't use it by default.
Specifically libstdc++fs needs to be added to the linking list and CLI11_HAS_FILESYSTEM=1 has to be defined. Then the filesystem variant of the Validators could be used on GCC 8. GCC 9+ does not have this issue so the <filesystem> is used by default.
There may also be other cases where a specific library needs to be linked.
Defining CLI11_HAS_FILESYSTEM=0 which will remove the usage and hence any linking issue.
In some cases certain clang compilations may require linking against libc++fs. These situations have not been encountered so the specific situations requiring them are unknown yet.
If building with WASI it is necessary to add the flag -lc-printscan-long-double to the build to allow long double support. See #841 for more details.
If you are not worried about latest features or recent bug fixes, you can install a stable version of CLI11 using:
sudo apt install libcli11-dev for Ubuntu, or: sudo dnf install cli11-devel on Fedora/Almalinux.
Then, in your CMake project, just call:
and in your C++ file: