Coder Social home page Coder Social logo

parse_args's Introduction

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 arguments

Does 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

#include <stdio.h>
#include <stdlib.h>

#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;
}

parse_args's People

Contributors

eriknyquist avatar

Stargazers

Vedqiibyol avatar  avatar

Watchers

James Cloos 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.