DEV Community

Cover image for Parsing command-line arguments in C
Talles L
Talles L

Posted on

Parsing command-line arguments in C

#c

Parsing on my own:

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

typedef struct {
    char* filepath;
    bool myflag;
    int myvalue;
} CliArgs;

static inline void error(const char* message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

CliArgs parse(int argc, char** argv) {
    CliArgs cli_args = { .filepath = 0, .myflag = false, .myvalue = 0 };

    if (argc == 1)
        error("No argument was passed.");

    for (int i = 1; i < argc; ++i) {
        if (!strcmp(argv[i], "--myflag")) {
            cli_args.myflag = true;
            continue;
        }

        if (!strcmp(argv[i], "--myvalue")) {
            ++i;

            if (i == argc)
                error("No value passed for --myvalue.");

            char* str_end;
            cli_args.myvalue = strtol(argv[i], &str_end, 10);

            if (*str_end)
                error("Invalid value for --myvalue.");

            continue;
        }

        if (cli_args.filepath)
            error("Only a single filepath can be given.");
        else
            cli_args.filepath = argv[i];
    }

    if (!cli_args.filepath)
        error("No filepath was passed.");

    return cli_args;
}

int main(int argc, char** argv) {
    CliArgs cli_args = parse(argc, argv);

    printf("%s was called:\n", argv[0]);

    printf(
        "%s is the filepath, myflag is %s and myvalue is %i.\n",
        cli_args.filepath,
        cli_args.myflag ? "true" : "false",
        cli_args.myvalue
    );

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Running it:

$ gcc cli_args.c -o cli_args

$ ./cli_args 
No argument was passed.

$ ./cli_args foo.txt
./cli_args was called:
foo.txt is the filepath, myflag is false and myvalue is 0.

$ ./cli_args foo.txt --myvalue
No value passed for --myvalue.

$ ./cli_args foo.txt --myvalue bar
Invalid value for --myvalue.

$ ./cli_args foo.txt --myvalue 123
./cli_args was called:
foo.txt is the filepath, myflag is false and myvalue is 123.

$ ./cli_args foo.txt --myvalue 123 --myflag
./cli_args was called:
foo.txt is the filepath, myflag is true and myvalue is 123.
Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay