Giter VIP home page Giter VIP logo

arg's Introduction

arg

A minimalist command line parser written in C

Why

I cought myself writing command line utilities quite often and having to rely on positional arguments.

How to use

There are only 2 public functions.

ARGCmdLine* argCmdLineParse(int argc, wchar_t** argv);

and

void argCmdLineFree(ARGCmdLine* cmdLine);

Just forward you main arguments to argCmdLineParse and it will return an ARGCmdLine structure filled with parsed information about your command line.

Features

  • Options starts with '-'
  • Options can be followed by a value
    • supported values are:
      • integers
      • floats
      • booleans (true or false)
      • strings (with or without quotes)
  • Options can be followed by multiple values of different types
  • The parser ignores arguments at the start that does not begin with a '-'. This allows programs to require positional arguments.

Example

int wmain(int argc, wchar_t** argv)
{
  // Parse the command line
	ARGCmdLine* cmdLine = argCmdLineParse(argc, argv);

	// Do stuff with the parsed command line
	for (int i = 0; i < cmdLine->numOptions; i++)
	{
		wprintf(L"Option: %s\n", cmdLine->options[i].option);
    ARGOption* option = &cmdLine->options[i];

		for (int j = 0; j < option->numValues; j++)
		{
			switch (cmdLine->options[i].values[j].type)
			{
				case INTEGER:
					printf("Value %d: %d (Integer)\n", j + 1, option->values[j].value.intValue);
					break;
				case FLOAT:
					printf("Value %d: %f (Float)\n", j + 1, option->values[j].value.floatValue);
					break;
				case BOOL:
					printf("Value %d: %s (Boolean)\n", j + 1, option->values[j].value.boolValue ? "true" : "false");
					break;
				case STRING:
					wprintf(L"Value %d: %s (String)\n", j + 1, option->values[j].value.stringValue);
					break;
				default:
					printf("Value %d: None\n", j + 1);
					break;
			}
		}
	}

	// Free command line object
	argCmdLineFree(cmdLine);

	return 0;
}

Example command line

A programm called cmdlineparse.exe using the code above would display the following output based on the provided arguments:

cmdlineparse "I'm a positional argument and will be ignored!"  -foo bar -number 100 -someFloat 32.9374 -multi "Hello, Sailor!" /usr/bin/foo 42 --whatever true
Option: foo
Value 1: bar (String)
Option: number
Value 1: 100 (Integer)
Option: someFloat
Value 1: 32.937401 (Float)
Option: muli
Value 1: Hello, Sailor! (String)
Value 2: /usr/bin/foo (String)
Value 3: 42 (Integer)
Option: -whatever
Value 1: true (Boolean)

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.