https://github.com/eriknyquist/parse_args
Portable command-line argument parser for C programs
https://github.com/eriknyquist/parse_args
Last synced: 4 months ago
JSON representation
Portable command-line argument parser for C programs
- Host: GitHub
- URL: https://github.com/eriknyquist/parse_args
- Owner: eriknyquist
- License: apache-2.0
- Created: 2019-06-09T19:15:01.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-25T01:58:47.000Z (over 2 years ago)
- Last Synced: 2025-01-11T05:28:23.423Z (6 months ago)
- Language: C
- Homepage:
- Size: 20.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
Portable command-line argument parser for C programs
----------------------------------------------------* Handles optional arguments with data (e.g. ``--file out.txt``)
* Converts optional argument data to desired data type
* Handles optional arguments without data, AKA flags (e.g. ``--verbose``)
* Handles positional argumentsDoes argument *parsing* only-- no docs generation.
How to use
----------Just add the file "parse_args.h" to your project and include it. The whole implementation
is inside this file.Example main.c
---------------.. code:: c
#include
#include#include "parse_args.h"
int int_value = 0;
long long_value = 0;
float float_value = 0.0;
double double_value = 0.0;
char *str_value = NULL;
long hex_value = 0;
unsigned uint_value = 0;
unsigned long ulong_value = 0;
int flag1;
int flag2;
char *positional_1 = NULL;
int positional_2 = 0;args_option_t options[] = {
ARGS_POSITIONAL_ARG(ARGTYPE_STRING, &positional_1),
ARGS_POSITIONAL_ARG(ARGTYPE_INT, &positional_2),
ARGS_OPTION("-i", "--int", ARGTYPE_INT, &int_value),
ARGS_OPTION("-l", "--long", ARGTYPE_LONG, &long_value),
ARGS_OPTION("-f", "--float", ARGTYPE_FLOAT, &float_value),
ARGS_OPTION("-d", "--double", ARGTYPE_DOUBLE, &double_value),
ARGS_OPTION("-u", "--unsigned", ARGTYPE_UINT, &uint_value),
ARGS_OPTION("-g", "--unsigned-long", ARGTYPE_ULONG, &ulong_value),
ARGS_OPTION("-x", "--hex", ARGTYPE_HEX, &hex_value),
ARGS_OPTION("-s", "--string", ARGTYPE_STRING, &str_value),
ARGS_FLAG("-q", NULL, &flag1),
ARGS_FLAG("-w", "--whatever", &flag2),
ARGS_END_OF_OPTIONS
};int main(int argc, char *argv[])
{
// Parse all arguments and convert to target types
if (parse_arguments(argc, argv, options) < 0)
{
printf("Error parsing arguments:\n%s\n", parse_arguments_error_string());
return -1;
}// Print all values received by command-line arguments
printf("Flags:\nq=%d, w=%d\n\n", flag1, flag2);printf("Options:\ni=%d, l=%ld, g=%lu, f=%.2f, d=%.2f, u=%u, x=%ld, s=%s\n\n",
int_value, long_value, ulong_value, float_value, double_value,
uint_value, hex_value, str_value);printf("Positional arguments:\n1=%s, 2=%d\n\n", positional_1, positional_2);
return 0;
}