GithubHelp home page GithubHelp logo

jwerle / cargs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from likle/cargs

1.0 0.0 0.0 83 KB

A lightweight cross-platform getopt alternative that works on Linux, Windows and macOS. Command line argument parser library for C/C++. Can be used to parse argv and argc parameters.

Home Page: https://likle.github.io/cargs/

License: MIT License

CMake 12.50% C 86.45% Meson 1.05%

cargs's Introduction

Travis Build Appveyor Build codecov Language Grade: C/C++

libcargs - command line argument library for C/C++

This is a lighweight C command line argument library. It is currently compiled and tested under Windows, MacOS and Linux.

Features

Please have a look at the reference for detailed information. Some features this library includes:

  • cross-platform on windows, linux and macOS
  • simple interface - just one header
  • one simple loop - to iterate over the arguments
  • automatic help output - showing all options available
  • long and short options - giving users alternatives
  • option values - for options which are more than just flags

Building

Building, embedding and testing instructions are available in the documentation (it's very easy).

Docs

All the documentation is available in the the github page of this repository.

Example

#include <cargs.h>
#include <stdbool.h>
#include <stdlib.h>

/**
* This is the main configuration of all options available.
*/
static struct cag_option options[] = {
 {.identifier = 's',
  .access_letters = "s",
  .access_name = NULL,
  .value_name = NULL,
  .description = "Simple flag"},

 {.identifier = 'm',
   .access_letters = "mMoO",
   .access_name = NULL,
   .value_name = NULL,
   .description = "Multiple access letters"},

 {.identifier = 'l',
   .access_letters = NULL,
   .access_name = "long",
   .value_name = NULL,
   .description = "Long parameter name"},

 {.identifier = 'k',
   .access_letters = "k",
   .access_name = "key",
   .value_name = "VALUE",
   .description = "Parameter value"},

 {.identifier = 'h',
   .access_letters = "h",
   .access_name = "help",
   .description = "Shows the command help"}};

/**
* This is a custom project configuration structure where you can store the
* parsed information.
*/
struct demo_configuration
{
 bool simple_flag;
 bool multiple_flag;
 bool long_flag;
 const char *key;
};

int main(int argc, char *argv[])
{
 char identifier;
 const char *value;
 cag_option_context context;
 struct demo_configuration config = {false, false, false, NULL};

 /**
  * Now we just prepare the context and iterate over all options. Simple!
  */
 cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
 while (cag_option_fetch(&context)) {
   identifier = cag_option_get(&context);
   switch (identifier) {
   case 's':
     config.simple_flag = true;
     break;
   case 'm':
     config.multiple_flag = true;
     break;
   case 'l':
     config.long_flag = true;
     break;
   case 'k':
     value = cag_option_get_value(&context);
     config.key = value;
     break;
   case 'h':
     printf("Usage: cargsdemo [OPTION]...\n");
     printf("Demonstrates the cargs library.\n\n");
     cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
     printf("\nNote that all formatting is done by cargs.\n");
     return EXIT_SUCCESS;
   }
 }

 printf("simple_flag: %i, multiple_flag: %i, long_flag: %i, key: %s\n",
   config.simple_flag, config.multiple_flag, config.long_flag,
   config.key ? config.key : "-");

 return EXIT_SUCCESS;
}

Example output

foo@bar:~$ ./cargsdemo 
simple_flag: 0, multiple_flag: 0, long_flag: 0, key: -
foo@bar:~$ ./cargsdemo -k=test -sm --long
simple_flag: 1, multiple_flag: 1, long_flag: 1, key: test
foo@bar:~$ ./cargsdemo --help
Usage: cargsdemo [OPTION]...
Demonstrates the cargs library.

  -s                   Simple flag
  -m, -M, -o, -O       Multiple access letters
  --long               Long parameter name
  -k, --key=VALUE      Parameter value
  -h, --help           Shows the command help

Note that all formatting is done by cargs.

cargs's People

Contributors

jwerle avatar likle avatar mo7sen avatar

Stargazers

 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.