Code Monkey home page Code Monkey logo

cli's Introduction

CLI

Library for creating command-line applications in C++.

Prerequisites :vertical_traffic_light:

Getting started :runner:

This example will use the following project structure

:project/
├─ CMakeLists.tsx
├─ lib/
│  ├─ cli/
├─ src/
│  ├─ main.cpp

1. Clone the library source code

cd ./lib
git clone https://github.com/taardal/cli

2. Add the library to the CMake project

# ./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)

3. Create the app

// ./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;
}

4. Build the app binary

# Generate project files to "build" folder
cmake -S . -B build

# Build binary using generated files
cmake --build build

4. Run the app

./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

cli's People

Contributors

taardal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.