Git Product home page Git Product logo

cflags's Introduction

c-flags

A simple one-header-file library for parsing command-line flags.
This library is heavily inspired by tsodings version: https://github.com/tsoding/flag.h
which is inspired by golang's flag module.

Installation

Just include flags.h in your project and you're good to go.

Usage

Creating a new flag:

// name: name of the flag
// desc: short description of the flag
// def: default value of the flag
bool *flag = cflag_bool(const char *name, const char *desc, const bool def);

char **flag2 = cflag_string(const char *name, const char *desc, const char *def);

...

Setting bounds for the flags:

cflag_int_minmax   (int      *flag, int      min, int      max);
cflag_uint64_minmax(uint64_t *flag, uint64_t min, uint64_t max);
cflag_float_minmax (float    *flag, float    min, float    max);

Parse the flags:

// Checks if incomming flags match any of the created flags. Returns false on error
if (!cflag_parse(argc, argv)) {
    // Logs a useful error message to the standard out stream
    cflag_log_error(stdout);

    // Alternatively you can handle the errors by yourself
    cflag_error err = cflag_get_error();

    // All existing errors are inside the enum cflag_errors and all
    // start with the prefix CFLAG_ERROR
    switch (err.error) {
        case CFLAG_ERROR_UNKNOWN:
            ...
        break;
    
        ...
    }
}

Log all available options (to view output see example.c):

// You can also print a useful message, where all the names, descriptions and optionally
// default values and bounds of the created flags are printed
//               stream   printdefault  printminmax
cflag_log_options(stdout, true,         true);

For an example program checkout example.c.
Compile and run it like so:


./build_example.sh

./example -h

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.