Inspiration

I like to code in C. Everytime I wanted to build a CLI I had to parse the command line arguements, parse flags, and options, check for edge cases and so much. 20% of the time was spent setting up the CLI itself. It was very frustrating, so I decided to do something about it. So I built "rim", my own c argument_parser which is easy to use and very functional.

What it does

It takes in argc and argv as arguments and takes care of the all the parsing of the command line arguments for you.

A sample program using rim

#include <stdio.h>
#include <stdlib.h>
#include "rim.h"

int main(int argc, char** argv){
    app_config_t config = {
        .name = "rim_test",
        .desc = "test program for rim arguement parser",
        .creator = "Japroz Saini<sainijaproz@gmail.com>",
        .license = "MIT",
        .version = "0.0.1"
    };
    app_t* app = new_app(config);
    add_flag(app, "--list", "-L", "lists something...");
    add_value(app, "--name", "-N", "name value");
    add_cmd(app, "echo", "echoes something...");
    app_run(app, argc, argv);

    if(rim_passed(app, "--list")){
        printf("list option passed\n");
    }
    if(rim_passed(app, "echo")){
        printf("echo option passed\n");
    }
    if(rim_passed(app, "--name")){
        printf("value of option name :: \"%s\"\n", rim_value(app, "--name"));
    }

    return EXIT_SUCCESS;
}

on compilation, you can use the program as follows:

$ ./out/main --list
list option passed
$ ./out/main --list --name=John
list option passed
value of option name :: "John"
$ ./out/main --list --name John
list option passed
value of option name :: "John"
$ ./out/main echo
echo option passed
$

How we built it

I built it using the C standard library and used no external libraries whatsoever to build the project.

Challenges we ran into

The edge cases and various standards that are there to pass arguments to a CLI in bash are tricky and I had to rewrite the parsing algorithm 3 times to make it compliant with the standard.

Accomplishments that we're proud of

The CLI generates help messages and more information on its own based on the configuration passed to it

What we learned

String parsing in C is not easy at all.

What's next for rim

Next, we would like users to create their custom help messages if they want to and also add options for commands and subcommands.

Github Repo

Built With

Share this project:

Updates