Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Start, build & configure

Start a project and run your first app

C++

Compile the next complete example with a C++23-capable compiler. The standard library/toolchain must support the APIs used by later entries.

Fragment

Shell/compiler syntax shown for a Unix-like environment. On Windows, use your installed toolchain and executable naming. Larger projects commonly use CMake or another build system.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
mkdir hello-cli
cd hello-cli
# Save the CLI example as main.cpp, then:
c++ -std=c++23 -Wall -Wextra main.cpp -o hello
./hello Ada

A complete small CLI app

C++

argc/argv supplies command-line arguments. Returning from main performs ordinary local destruction.

Complete program

Save as main.cpp. ./hello Ada prints the greeting.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <iostream>
#include <string>

int main(int argc, char* argv[]) {
    std::string name = argc > 1 ? argv[1] : "world";
    std::cout << "Hello, " << name << "!\n";
}

Read and validate an environment setting

C++

from_chars lets you check both conversion errors and complete input consumption. A pointer from getenv is absent only when null.

Fragment

Include <cstdlib>, <string_view>, <charconv>, <system_error> and <stdexcept>.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <charconv>
#include <cstdlib>
#include <stdexcept>
#include <string_view>
#include <system_error>

const char* setting = std::getenv("PORT");
std::string_view raw = setting ? setting : "3000";
int port = 0;
auto [end, error] = std::from_chars(raw.data(), raw.data() + raw.size(), port);
if (error != std::errc{} || end != raw.data() + raw.size() ||
    port < 1 || port > 65535) {
    throw std::runtime_error("PORT must be 1..65535");
}

Parse named command-line options

C++

C++23 has argc/argv but no standard named-option parser. This minimal manual parser supports only --verbose and rejects other options.

Fragment

Inside main(int argc, char* argv[]), with <string_view>/<stdexcept>. Choose a documented argument library for help, values, short flags and subcommands.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <stdexcept>
#include <string_view>

bool verbose = false;
for (int i = 1; i < argc; ++i) {
    std::string_view option(argv[i]);
    if (option == "--verbose") verbose = true;
    else throw std::runtime_error("Unknown option");
}

Start another program with an argument list

C++

C++23 has no portable standard subprocess API with an argument array, captured streams and cancellation. Select a documented library or platform process API for this task.

std::system executes a command through the host environment’s command processor; it is not interchangeable with a structured argv API.

Recognize source, build and output

C++

-std selects the language mode; -o names the executable. ./app runs that output. The shown command selects C++23; choose a compiler and standard-library implementation that supports the APIs you use.

Fragment

Separate shell commands; C++23 support varies by compiler (GCC, Clang, MSVC) and standard library (libstdc++, libc++, MSVC STL). The output name is chosen by this command.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
c++ -std=c++23 main.cpp -o app
./app

Read command-line arguments

C++

The ordinary main signature exposes narrow argument strings in argv. Encoding depends on the platform/startup environment.

Fragment

Do not assume char* command arguments are UTF-8 on every platform.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <iostream>
int main(int argc, char* argv[]) {
    for (int i = 1; i < argc; ++i) {
        std::cout << argv[i] << '\n';
    }
}

Distinguish an unset environment variable from an empty one

C++

getenv returns nullptr when the name is absent; an empty value is a non-null pointer to an empty string.

Fragment

The returned storage belongs to the environment; later environment changes can invalidate it.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <cstdlib>
#include <iostream>

const char* mode = std::getenv("APP_MODE");
if (mode == nullptr) std::cout << "unset\n";
else std::cout << mode << '\n';

Return a process status without skipping required cleanup

C++

Returning from main destroys its automatic local objects and reports the status. std::exit has different stack-cleanup behavior.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <cstdlib>
int main() {
    return EXIT_FAILURE;
}