Code Monkey home page Code Monkey logo

simple_options's Introduction

Simple Options

Build: Workflow

Simple Options Logo

Simple Options is a minimal command line arguments parsing library designed to provide minimal argument parsing in a single function call - no need to iterate over the arguments/getopt. It is designed for simple command line utilities (or in-built CLI-style commands) with standard command line behaviour.

It supports both single-character short, and multi-character long style arguments, ie: -f filename or --file filename.

It also provides some utility functions for automatically breaking down a single line of text into the whitespace separate arguments of a typical command line application. A simple --help style feature is built in, displaying standard usage behaviour.

It works with standard argc/argv values from main, but does not assume any stdio access. It makes no stdio printf/fprintf calls, and as such is suitable for embedded systems with different command interfaces, or when interacting with a non stdin/stdout/stderr system.

Example Usage

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

int main(int argc, const char *argv[]) {
	const char *file = NULL;
	int64_t val = 5;
	struct option_entry entries[] = {
		{"file", 'f', "File to load", OPTION_FLAG_string, .string = &file},
		{"number", 'n', "Number", OPTION_FLAG_int, .integer = &val},
		{"desc", 0, NULL, OPTION_FLAG_required},
		{NULL, 0},
	};
	if (opt_parse(argc, argv, entries) < 0) {
		opt_parse_usage(printf, argv[0], entries);
		return -1;
	}
	if (opt_parse_present('f', entries))
		printf("file: %s\n", file);
	printf("number: %lld\n", val);
	return 0;
}
% ./demo_app
Usage: ./demo_app [options]
	-h, --help	Display this help message
	-f, --file	File to load
	-n, --number	Number [default: 5]
	    --desc	[required]
% ./demo_app -f foo
Usage: ./demo_app [options]
	-h, --help	Display this help message
	-f, --file	File to load [default: "foo"]
	-n, --number	Number [default: 5]
	    --desc	[required]
% ./demo_app -f foo --desc wibble
file: foo
number: 5
Extra arg 4: wibble

simple_options's People

Contributors

andrerenaud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.