Skip to content

t-m-e/Argparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Argparse

Purpose

Argparse is used to parse arguments! Easy enough right? Argparse is a header-only implementation made to be easily integrated into any project. Both declaration and implementation exist inside of the include/argparse.h file.

Rule Strings

Rule strings are strings that define names of flags and how many arguments they consume. Rule strings take the following format: : consumes an argument, whitespace delimits different flag names, and anything else is parsed as a flag. Good thing to note, the Argparse parsing takes argument names literally, so if your argument is input as -a then the parser will interpret it as it is input.

Usage

Use with argc/argv:

/* $ ./argparse -a -b argb -c argc1 argc2 */

#include "include/argparse.h"

int main(
    int argc, 
    char** argv
) {
    struct Argparse argparse = Argparse_new("-a -b: -c::", ++argv, --argc);

    /* use your args */
    if (Argparse_exists(&argparse, "-a")) {
        // do something
    }

    char* option  = Argparse_get(&argparse, "-b");
    char* option1 = Argparse_getArg(&argparse, "-c", 0);
    char* option2 = Argparse_getArg(&argparse, "-c", 1);
}

Use with self defined array:

#include "include/argparse.h"
#include <stdio.h>

int main() {
    char* argv[] = {
        "-a", "-b", "some", "other", "thing"
    };
    int argc = 5;

    struct Argparse argparse = Argparse_new("-a -b", argv, argc);

    if (Argparse_exists(&argparse, "-a")) { /* ... */ }
    if (Argparse_exists(&argparse, "-b")) { /* ... */ }
    
    char* param = Argparse_getParam(&argparse, 0);
    
    /* macro defined in argparse.h
     * item is a user defined name for the parameter value (a char*) inside of the loop scope
     * argparse is the struct Argparse variable defined above
     */
    FOREACH_ARGPARSE_PARAM(item, argparse) {
        printf("%s", item);
    }
}

About

C header-only implementation of an argument parser.

Topics

Resources

Stars

Watchers

Forks

Languages