Library for creating command-line applications in C++.
- Git
- CMake
- C++ compiler
- Windows: MSVC (Bundled with Visual Studio)
- MacOS: Clang (Bundled with Xcode)
- Linux: GCC
This example will use the following project structure
:project/
├─ CMakeLists.tsx
├─ lib/
│ ├─ cli/
├─ src/
│ ├─ main.cpp
cd ./lib
git clone https://github.com/taardal/cli
# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 20)
project(cli-app)
add_executable(${PROJECT_NAME} src/main.cpp)
set(CLI_DIR lib/cli)
add_subdirectory(${CLI_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC ${CLI_DIR}/include)
target_link_libraries(${PROJECT_NAME} cli)
// ./src/main.cpp
#include <cli.h>
#include <iostream>
int main(int argc, char* argv[]) {
CLI::Command helloCommand;
helloCommand.Name = "hello";
helloCommand.Usage = "Say hello";
helloCommand.Action = [](const CLI::Context& context) -> void {
std::cout << "Hello World";
};
CLI::App app;
app.Name = "app";
app.Usage = "CLI App";
app.Commands = {
helloCommand
};
app.Run(argc, argv);
return 0;
}
# Generate project files to "build" folder
cmake -S . -B build
# Build binary using generated files
cmake --build build
./build/cli-app
Output:
NAME:
app - CLI App
USAGE:
app [global options] command [command options] [arguments...]
COMMANDS:
hello Say hello
help, h Show available commands and options
GLOBAL OPTIONS:
--help, -h Show help