GithubHelp home page GithubHelp logo

clappp's Introduction

Clap++

Low quality header-only C++ command line argument parser library inspired by clap.

Usage Example

#include "clapp.hpp"

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    clapp::app app("FooBar");

    auto result = app.version("1.0")
                      .description("Example Program")
                      .arg(app.new_arg("first_number").required(true).value_name("FIRST"))
                      .arg(app.new_arg("second_number").required(true).value_name("SECOND"))
                      .arg(app.new_arg("operator")
                               .short_flag('o')
                               .long_flag("operator")
                               .takes_value(true)
                               .default_value("+")
                               .description("Valid operators: +, -, *, /"))
                      .arg(app.new_arg("prefix").short_flag('p').long_flag("prefix").takes_value(true))
                      .arg(app.new_arg("suffix").short_flag('s').long_flag("suffix").takes_value(true))
                      .parse(argc, argv);

    if (!result) {
        std::cout << "error: " << result.error_message() << std::endl;
        return -1;
    }

    if (result.is_present("help")) {
        std::cout << app.help() << std::endl;
        return 0;
    }

    if (result.is_present("version")) {
        std::cout << app.version() << std::endl;
        return 0;
    }

    auto first_number = std::stof(result.value_of("first_number"));
    auto second_number = std::stof(result.value_of("second_number"));
    auto operation = result.value_of("operation");
    auto prefix = result.value_of("prefix");
    auto suffix = result.value_of("suffix");
    float operation_result = 0;

    if (operation == "+") {
        operation_result = first_number + second_number;
    } else if (operation == "-") {
        operation_result = first_number - second_number;
    } else if (operation == "*") {
        operation_result = first_number * second_number;
    } else if (operation == "/") {
        operation_result = first_number / second_number;
    } else {
        std::cout << "Invalid operation" << std::endl;
        return -1;
    }

    std::cout << prefix << operation_result << suffix << std::endl;

    return 0;
}

TO-DO

  • Tests
  • Subcommands

clappp's People

Contributors

waldson avatar

Watchers

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