Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

likle/cargs

Repository files navigation

Build PipelineBuild PipelineBuild PipelineBuild Pipelinecodecov

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

This is a lighweight C command line argument library which does not require any malloc. It is currentlycompiled and tested underLinux,FreeBSD,macOS andWindows.

Features

Please have a look at thereference for detailedinformation. 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
  • no malloc needed - for situations where that's not available

Building

Building,embedding andtesting instructions areavailable in the documentation (it's very easy).

Docs

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

Example

First define the options:

staticstructcag_optionoptions[]= {{.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"}};

And then just iterateargv:

intmain(intargc,char*argv[]){boolsimple_flag= false,multiple_flag= false,long_flag= false;constchar*value=NULL;intparam_index;cag_option_contextcontext;cag_option_init(&context,options,CAG_ARRAY_SIZE(options),argc,argv);while (cag_option_fetch(&context)) {switch (cag_option_get_identifier(&context)) {case's':simple_flag= true;break;case'm':multiple_flag= true;break;case'l':long_flag= true;break;case'k':value=cag_option_get_value(&context);break;case'h':printf("Usage: cargsdemo [OPTION]...\n");printf("Demonstrates the cargs library.\n\n");cag_option_print(options,CAG_ARRAY_SIZE(options),stdout);returnEXIT_SUCCESS;case'?':cag_option_print_error(&context,stdout);break;    }  }printf("simple_flag: %i, multiple_flag: %i, long_flag: %i, key: %s\n",simple_flag,multiple_flag,long_flag,value ?value :"-");returnEXIT_SUCCESS;}

Example output

foo@bar:~$./cargsdemosimple_flag: 0, multiple_flag: 0, long_flag: 0, key: -
foo@bar:~$./cargsdemo -k=test -sm --longsimple_flag: 1, multiple_flag: 1, long_flag: 1, key: test
foo@bar:~$./cargsdemo --helpUsage: 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.

Example code

The complete example can be found in the./demo/ folder of this repository.

About

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

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp