Skip to content

clibs/flag

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

flag

Go-style flag parsing for C programs.

Installation

Install with clib:

$ clib install flag

Example

Much like the Go flag package you have the choice of using a lower level flagset_t, or relying on the less flexible singleton. The following example uses the singleton, which handles the --version and --help output for you, as well as reporting errors.

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

#define VERSION "v1.0.0"

int
main(int argc, const char **argv) {
  int requests = 5000;
  int concurrency = 10;
  const char *url = ":3000";

  flag_int(&requests, "requests", "Number of total requests");
  flag_int(&concurrency, "concurrency", "Number of concurrent requests");
  flag_str(&url, "url", "Target url");
  flag_parse(argc, argv, VERSION);

  puts("");
  printf("     requests: %d\n", requests);
  printf("  concurrency: %d\n", concurrency);
  printf("          url: %s\n", url);
  puts("");

  return 0;
}

Default help output:

Usage: ./example [options] [arguments]

Options:
  --requests     Number of total requests (5000)
  --concurrency  Number of concurrent requests (10)
  --url          Target url (:3000)
  --version      Output version
  --help         Output help

License

MIT

About

Go-style flag parsing for C

Resources

License

Stars

Watchers

Forks

Packages

No packages published