Giter VIP home page Giter VIP logo

cap's Introduction

Cap

Cap is a single header command-line argument parser library written in C with low memory footprint and compatible with C99 standard.

Usage

To use Cap just include cap.h file then compile and link cap.c file with your project.

Example

Use this code snippet to learn how to use Cap.

#include <stdio.h>
#include "cap.h"

int main(int argc, char **argv) {
    int err = 0;

    // Initialize cap
    Cap_t *cap = cap_init(argc, argv);
    if (cap == NULL)
        // handle error
        return 1;

    // register a new sub-command
    // say our app is a compiler and we want a `compile` sub-command
    err = cap_register_subcmd(cap, "compile", "Compile the source files");
    if (err != 0)
        // handle error
        goto exit;

    // register a new flag
    // say we want to add a new flag to `compile` sub-command
    err = cap_register_flag(cap, "compile", "op", "optimize the source code");

    // regist a new flag with no sub-command
    err = cap_register_flag(cap, NULL, "file", "file path");


    err = cap_parse_args(cap);
    if (err != 0)
        // must handle this error
        goto exit;


    // get raw arguments for `compile` sub-command
    Cap_RawArgs_t run_args = cap_subcmd_rawargs(cap, "compile");

    // a flag is present in command-line args or not (used for boolean flags)
    // result will be 0 (false) or 1 (true)
    int op = cap_flag_provided(cap, "compile", "op");

    // get a flag's value
    char *tmp = cap_flag_getval(cap, NULL, "file");


exit:
    // deinit and free all memory allocated by cap
    cap_deinit(&cap);
    return err;
}

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.