Giter Club home page Giter Club logo

skipper's Introduction

skipper

skipper is a small C++ library that could help you create a simple interactive command-line like applications.

The library itself is header-only so essentially to use it you just have to clone the repository and setup the right include paths in the project where you would like to use it.

The library uses a few c++11 features, so you need a relatively recent compiler to use it.

To create a meaningfull program you need to create the program and add some commands to it and run it.

#include <skipper/program.hpp>

skipper::program p("Some text describing what the program does");
p.add_command<std::string>("a", "Some helpful text", some_function, skipper::any<std::string>());

return p.run();

here some_function takes a std::string as input. In the resulting executable.

  • pressing 'a' and 'enter' will promt for more input in this case a string and some_function will be called with it, provided the user managed to type a valid string.
  • pressing 'h' and 'enter' will display some helpful text, if you provided it.
  • pressing 'q' and 'enter' will exit the program (p.run() will return an int).

You can also add functions that take some other input types

p.add_command<int>("b", "Some helpfuller text", int_function, skipper::any<int>());
p.add_command<float>("c", "The helpfullest text", float_function, skipper::any<float>());

Or functions that take no input

p.add_command("p", "this function prints something", print_function);

Usually it is meaningful to validate the input provided by the user. To this end three options are at your disposal any, range and set.

Any accepts any input

p.add_command<std::string>("d", "text", int_function, skipper::any<std::string>());

Range accepts all inputs in a specified range, here [0,10]

p.add_command<float>("e", "text", float_function, skipper::range<float>(0,10));

Range accepts all inputs in a specified set, here {-3,1,42}

p.add_command<int>("f", "text", int_function, skipper::set<int>({-3,1,42}));

You can of course also provide you own Predicate as long as you also provide an overload for the corresponding << operator which is used to provide help output.

template<typename Type>
struct conservative
{
    bool operator()(const Type value) const
    {
        return false;
    }

    friend std::ostream& operator<<(std::ostream& os, const conservative &c)
    {
        return os << "some friendly help text telling the user no";
    }
};

p.add_command<std::string>("no", "this function is picky with the input", picky_function,
                           conservative<std::string>());

If your function happens to take inputs of a type which the default converter function cannot handle you can provide your own.

const Magic default_convert(std::istream& in)
{
    Magic number = 42;
    return number;
}

p.add_command<Magic>("?", "Some helpful text", magic_function, skipper::any<Magic>());

You can also provide alternative input and output streams for the program, which can be useful for testing.

std::istringstream test_in;
std::ostringstream test_out;
skipper::program p("test program", test_in, test_out);

test_in.str("q\n");

p.run();

assert(test_out.str().compare("") == 0);

skipper's People

Contributors

petya2164 avatar jpihl avatar janusheide 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.