Code Monkey home page Code Monkey logo

opt-parser's Introduction

Build Status License: MIT

Small library to parse options for embedded systems like Arduino

This library provides a small class to parse option strings and allows you to get these into variables. Options strings can be in the form of h=12 b=100.0 etc. This is usefull if you want to send your device simple messages over MQTT and act on them accordingly instead of using sometimes inconvenient JSON messages.

Example

uint32_t x;
float y;
float z;
OptParser::get("x=12 y=15.5 z=-23", [&x,&y,&z](OptValue f) {
        if (std::strcmp( f.key(), "x" ) == 0) {
            x = f;
        }
        if (std::strcmp( f.key(), "y" ) == 0) {
            y = f;
        }
        if (std::strcmp( f.key(), "z" ) == 0) {
            z = f;
        }
    });

More Examples of usage can be found in the ´tests/src/test_optparser.hpp´ directory and should be fairly straight forward.

Notice that this string is modified by being broken into smaller chunks if passed as a char array.

You can also use the stack version that does not corrupt the buffer.

    int calls = 0;
    char testCase[]="0.1,100.124,-100.678,-100,200,true";
    OptParser::get<64>(testCase, ',', [&calls](OptValue f) {        
            calls++;
    });
    REQUIRE_THAT((const char*)testCase, Equals("0.1,100.124,-100.678,-100,200,true"));
    REQUIRE(calls == 6);

Explicit return types

Since v2.0.0 we do support implicit return types and the following types are supported:

  • char*
  • float
  • long
  • char
  • int16_t
  • int32_t
  • bool

Idea 1

I want to implement a more convenicent method, something like this:

OptParser::get("x=12 y=15.5 z=-23", { {"x", &x, intType}, {"y", &y, floatType}, {"z", &z, floatType} });

But didn´t had time yet..

License

This code is released under the MIT License.

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.