Search
 
SCRIPT & CODE EXAMPLE
 

C

c command line arguments parser

#include <argp.h>
#include <stdbool.h>

const char *argp_program_version = "programname programversion";
const char *argp_program_bug_address = "<your@email.address>";
static char doc[] = "Your program description.";
static char args_doc[] = "[FILENAME]...";
static struct argp_option options[] = { 
    { "line", 'l', 0, 0, "Compare lines instead of characters."},
    { "word", 'w', 0, 0, "Compare words instead of characters."},
    { "nocase", 'i', 0, 0, "Compare case insensitive instead of case sensitive."},
    { 0 } 
};

struct arguments {
    enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode;
    bool isCaseInsensitive;
};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    struct arguments *arguments = state->input;
    switch (key) {
    case 'l': arguments->mode = LINE_MODE; break;
    case 'w': arguments->mode = WORD_MODE; break;
    case 'i': arguments->isCaseInsensitive = true; break;
    case ARGP_KEY_ARG: return 0;
    default: return ARGP_ERR_UNKNOWN;
    }   
    return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };

int main(int argc, char *argv[])
{
    struct arguments arguments;

    arguments.mode = CHARACTER_MODE;
    arguments.isCaseInsensitive = false;

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

    // ...
}
Comment

PREVIOUS NEXT
Code Example
C :: c malloc array 
C :: c unused variable 
C :: round c 
C :: passing pointer to function 
C :: C Input and Output Array Elements 
C :: c defined value sum 
C :: pointer c 
C :: Install valet-linux 
C :: what is %d in C 
C :: ecto where is not nil 
C :: len of str vbs 
C :: XAudio2 C 
C :: c program for calculating product of array 
C :: c hello word 
C :: convert c code to assembly language 
C :: grep C hello world 
C :: print integer to stdout using write or putchar? 
C :: until command lldb 
C :: arma 3 key pressed 
C :: injection 
C :: pdo crud 
C :: c ausgabe von variablen 
C :: wap in c to input n numbers in an array, find out the sum of odd nos. and even nos. display the numbers whose sum is high. 
C :: program to merge two strings in c 
C :: too many arg 
C :: how to write 2d array from bin file in c 
C :: c %s 
C :: how to get value of multidimensional array in c 
C :: unox reclame harmonica tabs 
Dart :: text overflow ellipsis flutter 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =