npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

parg.c

v1.0.3

Published

Parser for argv that works similarly to getopt; Jørgen Ibsen (2015).

Downloads

103

Readme

parg CI codecov

About

Most command-line programs have to parse options, so there are a lot of different solutions to this problem. Some offer many features, while others are more basic.

One of the simpler solutions for C is the getopt function, and its extension getopt_long. They iterate over the options in argv, returning them one at a time on successive calls.

One nice thing about them is that they are available on most Unix-like operating systems (and usually accompany GCC elsewhere, like Windows). Unfortunately, some implementation details vary between platforms.

A potential question is what license the version you get when you include them is available under. Some are GPL, others LGPL. There are also ports of getopt that use more liberal licenses.

parg is a parser for argv that works similarly to getopt, but does not aim to be a direct replacement. It attempts to make some choices about how to handle the extensions and idiosyncrasies of other getopt implementations, and document them.

It consists of a single source and include file, written in portable ANSI C. It is made available under the MIT No Attribution License (MIT-0). It is written by Jørgen Ibsen.

Installation

Run:

$ npm i parg.c

And then include parg.h as follows:

#include "node_modules/parg.c/parg.h"

You may also want to include parg.c as follows:

#ifndef __PARG_C__
#define __PARG_C__
#include "node_modules/parg.c/parg.c"
#endif

This will include both the function declaration and their definitions into a single file.

#include "node_modules/parg.c/parg.h"

Usage

The include file parg.h contains documentation in the form of doxygen comments. A configuration file is included, run doxygen to generate documentation in HTML format.

You can include parg.c and parg.h in your own projects.

For CI, parg uses CMake to provide an easy way to build and test across various platforms and toolsets. To create a build system for the tools on your platform, and build parg, use something along the lines of:

mkdir build
cd build
cmake ..
cmake --build .

Example

Here is an example that parses command-line options using parg_getopt():

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

#include "parg.h"

int main(int argc, char *argv[])
{
	struct parg_state ps;
	int c;

	parg_init(&ps);

	while ((c = parg_getopt(&ps, argc, argv, "hs:v")) != -1) {
		switch (c) {
		case 1:
			printf("nonoption '%s'\n", ps.optarg);
			break;
		case 'h':
			printf("Usage: testparg [-h] [-v] [-s STRING]\n");
			return EXIT_SUCCESS;
			break;
		case 's':
			printf("option -s with argument '%s'\n", ps.optarg);
			break;
		case 'v':
			printf("testparg 1.0.0\n");
			return EXIT_SUCCESS;
			break;
		case '?':
			if (ps.optopt == 's') {
				printf("option -s requires an argument\n");
			}
			else {
				printf("unknown option -%c\n", ps.optopt);
			}
			return EXIT_FAILURE;
			break;
		default:
			printf("error: unhandled option -%c\n", c);
			return EXIT_FAILURE;
			break;
		}
	}

	for (c = ps.optind; c < argc; ++c) {
		printf("nonoption '%s'\n", argv[c]);
	}

	return EXIT_SUCCESS;
}

Comparison to getopt

Use of global variables

getopt uses global variables to store its state between calls. parg uses a struct parg_state, which you must pass with each call.

Handling of nonoptions

POSIX and BSD getopt return -1 on the first nonoption argument. GNU getopt by default reorders argv (even though it is passed as const), so all options come first.

parg does not change argv, and returns each nonoption as the option argument of an option with value 1 (like GNU getopt, if optstring were prefixed by '-').

If you wish to process all options first, and have the nonoptions ordered at the end of argv, you can use parg_reorder():

	optend = parg_reorder(argc, argv, optstring, NULL);

	while ((c = parg_getopt(&ps, optend, argv, optstring)) != -1) {
		/* ... */
	}

	/* elements of argv[] from optend to argc are nonoptions */

Value of optind on error

When there are multiple short options in one argument, getopt does not increment optind until the last one is processed. This makes it harder to tell which argument an unknown option came from (if a is an unknown option, -a and -ab will return '?' with different values in optind).

parg always increments the optind value in it's state so it points to the next argv element to be processed. So when parg returns '?' (or ':'), the element that contains the error is argv[optind - 1].

Value of optopt on error

With getopt_long, it varies what the values of optopt and longindex are when an error is found with option arguments of long options. Sometimes these values are not documented.

parg sets optopt to val if flag is NULL, and 0 otherwise (which equals the return value on successful match), and longindex is set to the index of the entry in longopts that matched.

Return value on option argument error

When the first character of optstring is ':', it varies what getopt returns on extraneous option arguments.

In this case, parg returns '?' if no option match is found, and ':' if a match is found, but is missing a required argument, or has an extraneous argument.

Alternatives

Some ports of getopt:

Other command-line parsing libraries that support C:

A few C++ command-line parsing libraries:

ORG