Git Product home page Git Product logo

cli_args's Introduction

cli_args

A simple library to parse command-line options in C. This module is inspired by command-line-args.

Getting Started

Before using the library (cli_args.a), you need to create it with:

$ make

Synopsis

You can set options using the standards. All the commands below are equivalent, setting the same values:

$ ./example --help --timeout 1000 --file one.js two.js
$ ./example -ht 1000 --file one.js two.js
$ ./example -h -t 1000 -f one.js two.js

To access the values, first initialize and create a list of option definitions describing the options your application accepts (name, alias, type and description):

size = 3;
option_defs = init_option_defs(size);
option_defs[0] = add_def("help", "h", OPT_BOOLEAN, NULL);
option_defs[1] = add_def("file", "f", OPT_STRING, NULL);
option_defs[1].multiple = 1;
option_defs[2] = add_def("timeout", "t", OPT_INTEGER, NULL);

Next, parse the options by initializing and by using command_line_args:

args = init_args(argv, option_defs, size);
options = command_line_args(args);

options now looks like this by using:

print_options(&options);
{ help: true, timeout: 1000, file: [ 'one.js', 'two.js' ] }

Finally don't forget to free the options variable when you don't need it anymore:

free_options(&options);

Advanced usage

Parsing is strict by default - an exception is thrown if the user sets a singular option more than once or sets an unknown value or option (one without a valid definition). To be more permissive, enabling partial mode will return known options in the usual manner while collecting unknown arguments in a separate _unknown property.

args = init_args(argv, option_defs, size);
args->partial = 1;
options = command_line_args(args);

Usage guide generation

A usage guide (typically printed when --help is set) can be generated using command-line-usage. See the examples below and read the documentation for instructions how to create them.

A typical usage guide example.

usage

License

This project is licensed under the MIT License - see the LICENSE.md file for details

cli_args's People

Contributors

wbeuil avatar

Watchers

 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.