Giter Club home page Giter Club logo

argoat's Introduction

Argoat

Argoat is a lightweight library for command-line options parsing. This was created because most of the existing solutions rely heavily on macros, and all of them expect you to write a giant switch to handle the given options.

Argoat allows you to deal with arguments using function pointers. It does not use any macro, switch, or dynamic memory allocation.

Argoat supports the following syntaxes:

  • simple options test -a -b
  • compound options test -ab
  • assigned options test -c=4 -d 2
  • long options test --code 4 --den 2
  • lone dash test --oki - --den 2
  • lone double-dash test --oki -- --doki
  • unflagged options test 0 -c=4 1 -d=2 3
  • limited params test 0 -c 4 1 -d 2 3

Argoat does not support the following syntaxes on purpose:

  • simple neighbours test -a4
  • custom symbols test +a 4

All of that in around 200 lines of code (getopt has approximately 700). Don't be shy, sneak a goat in your code.

Cloning

Clone with --recurse-submodules to get the required submodules.

Testing

Run make to compile the testing suite, and make run to perform the tests.

Using

TL;DR

#include "argoat.h"
#include <stdbool.h>
#include <stdio.h>

void handle_main(void* data, char** pars, const int pars_count)
{
}

void handle_bool(void* data, char** pars, const int pars_count)
{
	*((bool*) data) = true;
}

int main(int argc, char** argv)
{
	bool data1 = false;
	char* unflagged[23];

	const struct argoat_sprig sprigs[2] =
	{
		{NULL, 0, NULL, handle_main},
		{"t", 0, (void*) &data1, handle_bool}
	};

	struct argoat args = {sprigs, 2, unflagged, 0, 23};
	argoat_graze(&args, argc, argv);
	printf("%c\n", data1 ? '1' : '0');

	return 0;
}

Details

Include argoat.h and compile argoat.c with your code.

Write the functions that will handle your parameters. They will be called during the parsing process, in the order given by the user

void handle_main(void* data, char** pars, const int pars_count)
{
}

void handle_bool(void* data, char** pars, const int pars_count)
{
	*((bool*) data) = true;
}

In your int main(int argc, char** argv), declare the variables to configure. They will be passed to the corresponding functions as void* data

bool data1 = false;

Also declare an array of strings to store the unflagged arguments. Just choose a size corresponding to the maximum number of unflagged arguments your program supports, or create a null pointer if it does not use them.

char* unflagged[UNFLAGGED_MAX];

Then, declare an array of flag structures. The first entry only has to contain the unflagged-arguments handling function. The others must specify:

  • the name of the flag (one char for '-' prefix, multiple chars for '--')
  • the maximum number of arguments supported by this flag
  • a pointer to the data that has to be configured by the handling function
  • a pointer to the handling function
const struct argoat_sprig sprigs[2] =
{
	{NULL, 0, NULL, handle_main},
	{"t", 0, (void*) &data1, handle_bool}
};

Then, create the main argoat structure given:

  • the flags array
  • its size,
  • the unflagged string buffer
  • the initial number of unflagged arguments
  • the maximum possible
struct argoat args = {sprigs, 2, unflagged, 0, UNFLAGGED_MAX};

All that remains to do is calling the parsing function

argoat_graze(&args, argc, argv);

And using the configured data

printf("%c\n", data1 ? '1' : '0');

argoat's People

Contributors

nullgemm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

argoat's Issues

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.