• Stars
    star
    176
  • Rank 209,597 (Top 5 %)
  • Language
    C
  • License
    MIT No Attribution
  • Created over 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parser for argv that works similarly to getopt

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).

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 add the source files parg.c and parg.h to 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:

More Repositories

1

tinf

Tiny inflate library (inflate, gzip, zlib)
C
161
star
2

brieflz

Small fast Lempel-Ziv compression library
C
100
star
3

scv

ANSI C implementation of dynamic array, with interface similar to C++ std::vector
C
38
star
4

blz4

Example of LZ4 compression with optimal parsing using BriefLZ algorithms
C
27
star
5

lzdatagen

LZ data generator
C
21
star
6

bcrush

Example of CRUSH compression with optimal parsing using BriefLZ algorithms
C
14
star
7

galib

Copy of GAlib (http://lancet.mit.edu/ga/) for use in pngwolf-zopfli
C
12
star
8

spooky

SpookyHash in C
C
10
star
9

tmcolorconv

Convert tmTheme to sRGB color space
Python
9
star
10

pngquant-winbuild

Setup for building pngquant on Windows
Makefile
7
star
11

bdftools

Command line tools modified for converting the Dina programming font between FON and BDF format
C
7
star
12

yamltotm

YAML to tmTheme
Python
7
star
13

vscode-solarized-minimal

Solarized Minimal color theme for Visual Studio Code
6
star
14

ExportSnapshot

Lightroom plug-in to create snapshot on export
Lua
6
star
15

wideint

C++ implementation of wide exact-width integer types
C++
4
star
16

blog

Basic logging
C
3
star
17

oksolar-minimal

OKSolar Minimal color scheme generator
Python
3
star
18

BackupHelper

Lightroom plug-in to add menu item for catalog backup
Lua
2
star
19

ulz

An ultra-fast LZ77 compressor/data compression library
C++
2
star
20

aocpp2019

Advent of Code 2019 solutions in C++17
C++
2
star
21

aocpp2018

Advent of Code 2018 solutions in C++17
C++
2
star
22

com2text

MS-DOS COM to Text Converters
Assembly
2
star
23

hardtoc

Hard to C blog
HTML
1
star
24

citest

Test repository for playing around with GitHub Actions
Meson
1
star
25

atom-solarized-minimal-dark

Solarized Minimal Dark color theme for Atom
CSS
1
star
26

solarized_minimal

Solarized Minimal color scheme for Sublime Text
XML
1
star
27

solarized-minimal

Website for Solarized Minimal
HTML
1
star
28

lzeg

1
star