• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    C
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Commander option parser ported to C - simple API, auto-generated --help

commander

Commander option parser ported to C.

Installation

Install with clib:

$ clib install clibs/commander

Automated --help

The example below would produce the following --help:


Usage: example [options]

Options:

  -V, --version                 output program version
  -h, --help                    output help information
  -v, --verbose                 enable verbose stuff
  -r, --required <arg>          required arg
  -o, --optional [arg]          optional arg

Example

#include <stdio.h>
#include "commander.h"

static void
verbose(command_t *self) {
  printf("verbose: enabled\n");
}

static void
required(command_t *self) {
  printf("required: %s\n", self->arg);
}

static void
optional(command_t *self) {
  printf("optional: %s\n", self->arg);
}

int
main(int argc, const char **argv){
  command_t cmd;
  command_init(&cmd, argv[0], "0.0.1");
  command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose);
  command_option(&cmd, "-r", "--required <arg>", "required arg", required);
  command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional);
  command_parse(&cmd, argc, argv);
  printf("additional args:\n");
  for (int i = 0; i < cmd.argc; ++i) {
    printf("  - '%s'\n", cmd.argv[i]);
  }
  command_free(&cmd);
  return 0;
}

Closure

cmd.data is a void * so pass along a struct to the callbacks if you want.

Usage

cmd.usage defaults to "[options]".

Links

  • Used by the mon(1) process monitor

Short flags

Compound short flags are automatically expanded to their canonical form. For example -vLO would become -v -L -O.

License

(The MIT License)

Copyright (c) 2012 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

clib

Package manager for the C programming language.
C
4,863
star
2

entr

A utility for running arbitrary commands when files change. Uses kqueue(2) or inotify(7) to avoid polling. entr responds to file system events by executing command line arguments or by writing to a FIFO. entr was written to provide to make rapid feedback and automated testing natural and completely ordinary.
C
604
star
3

list

C doubly linked list
C
441
star
4

cmocka

An elegant unit testing framework for C with support for mock objects.
C
232
star
5

buffer

Tiny C string library
C
198
star
6

sha1

sha1 hash alogrithm
C
172
star
7

hash

C hash implementation based on khash
C
168
star
8

coro

Coroutines in C
C
121
star
9

flag

Go-style flag parsing for C
C
119
star
10

lmdb

Symas Lightning Memory-Mapped Database (LMDB)
77
star
11

red-black-tree

Generic red-black tree library (by Julienne Walker).
C
55
star
12

uv

A clib pointer to libuv: Cross-platform asychronous I/O http://libuv.org/
46
star
13

net

extends libuv to support tls/ssl in a simpler API for clibs
C
34
star
14

amp

Abstract Message Protocol C implementation
C
30
star
15

timer

Timer with microsecond resolution
C
30
star
16

logfmt

Fast C logfmt parser.
C
29
star
17

term

Terminal goodies
C
27
star
18

mt19937ar

Mersenne Twister random number generator
C
25
star
19

dumpasn1

Display the contents of ASN.1 encoded data
C
23
star
20

int

Binary integer representation utilities.
C
23
star
21

http-get.c

Simple HTTP GET requests backed by libcurl
C
23
star
22

rle

Run-length encoding.
C
21
star
23

rgba

RGB / RGBA parsing / formatting
C
20
star
24

container_of

Obtain a pointer to the struct that contains the struct member
C
20
star
25

wildcardcmp

Simple wildcard string comparison for C
C
19
star
26

readline

Tiny C readline library, note: this is not used in CLI-readline's one.
C
19
star
27

inih

Simple .INI file parser
C
19
star
28

ms

C str to us / ms utilities
C
18
star
29

debug

Conditional debug logging for C
C
17
star
30

zerodbg

Useful macros for debugging purposes.
C
16
star
31

unlikely

gcc branch prediction macros
C
16
star
32

bytes

byte <> string conversion utilities
C
14
star
33

which

Lookup executable via PATH or given path string
C
13
star
34

check

Super basic CHECK(), DCHECK(), and NOTREACHED() macro implementations.
Shell
12
star
35

strdup

Drop-in replacement for strdup(3) from libc
C
12
star
36

clib-uninstall

clib(1) plugin for uninstalling executables
C
11
star
37

trigger

Simple event handling library.
C
11
star
38

wiki-registry.c

C
11
star
39

timestamp

C timestamp util
C
11
star
40

leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
C++
11
star
41

module

A set of macros and functions to make defining a C module easier
C
10
star
42

sqlite

Embedded SQL database
C
9
star
43

file

File utilities
C
9
star
44

bench

get wall and cpu time for benchmarking
C
9
star
45

clib-validate

clib(1) plugin for validating a package.json
C
9
star
46

strndup

Safe version of strndup
C
8
star
47

clib-makefile

Interactively generate a `Makefile' for your C lib project with clibs in mind
Shell
8
star
48

graph

Simple (directed) graph library.
C
7
star
49

logger

Logger used internally by clib(1)
C
7
star
50

clib-package

Internal bits of clib-install(1) for fetching packages
C
5
star
51

build_assert

Routines for build-time assertions
3
star
52

website

The source code for clibs.org.
TypeScript
3
star
53

tig

A clib pointer to https://github.com/jonas/tig
2
star
54

jsmn

A clib pointer to https://github.com/zserge/jsmn: Jsmn is a world fastest JSON parser/tokenizer.
1
star
55

buffet

A clib pointer to https://github.com/alcover/buffet
1
star
56

sodium

A clib pointer to libsodium: A modern, portable, easy to use crypto library.
1
star
57

discount

A clib (clib@next) pointer to https://github.com/Orc/discount
Shell
1
star
58

semver

A clib (clib@next) pointer to https://github.com/EddyLuten/lib-semver
JavaScript
1
star
59

packcc

A clib pointer for https://github.com/arithy/packcc
Shell
1
star