Introduction:
In C programming, command-line arguments provide a straightforward way to customize program behavior at runtime. But what if we want to combine multiple options or flags to perform different actions? This is where combined command-line arguments come into play. In this blog post, we’ll explore the concept of combining command-line arguments, how to parse and handle them, and provide practical examples to illustrate their usage in creating more flexible and feature-rich C programs.

Understanding Combined Command-line Arguments:

  1. What are Combined Command-line Arguments?
  1. Parsing Combined Arguments:
   ./program_name -abc -d value --option1 --option2=value
  1. Accessing Combined Arguments:

Example: File Operations with Combined Flags

Let’s create a file operations program that accepts combined command-line flags to perform various file-related tasks such as create, read, write, and delete.

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

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s [options]\n", argv[0]);
        printf("Options:\n");
        printf("  -c <filename>: Create a new file\n");
        printf("  -r <filename>: Read file contents\n");
        printf("  -w <filename> <content>: Write content to file\n");
        printf("  -d <filename>: Delete a file\n");
        return 1;
    }

    char *createFile = NULL;
    char *readFile = NULL;
    char *writeFile = NULL;
    char *writeContent = NULL;
    char *deleteFile = NULL;

    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
            createFile = argv[i + 1];
            i++; // Skip next argument
        } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) {
            readFile = argv[i + 1];
            i++; // Skip next argument
        } else if (strcmp(argv[i], "-w") == 0 && i + 2 < argc) {
            writeFile = argv[i + 1];
            writeContent = argv[i + 2];
            i += 2; // Skip next two arguments
        } else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
            deleteFile = argv[i + 1];
            i++; // Skip next argument
        }
    }

    // Perform file operations based on flags
    if (createFile) {
        FILE *file = fopen(createFile, "w");
        if (file) {
            printf("File '%s' created successfully.\n", createFile);
            fclose(file);
        } else {
            printf("Error creating file '%s'.\n", createFile);
        }
    }

    if (readFile) {
        FILE *file = fopen(readFile, "r");
        if (file) {
            char buffer[100];
            printf("Contents of '%s':\n", readFile);
            while (fgets(buffer, sizeof(buffer), file) != NULL) {
                printf("%s", buffer);
            }
            fclose(file);
        } else {
            printf("Error reading file '%s'.\n", readFile);
        }
    }

    if (writeFile && writeContent) {
        FILE *file = fopen(writeFile, "w");
        if (file) {
            fprintf(file, "%s\n", writeContent);
            printf("Content written to '%s'.\n", writeFile);
            fclose(file);
        } else {
            printf("Error writing to file '%s'.\n", writeFile);
        }
    }

    if (deleteFile) {
        if (remove(deleteFile) == 0) {
            printf("File '%s' deleted successfully.\n", deleteFile);
        } else {
            printf("Error deleting file '%s'.\n", deleteFile);
        }
    }

    return 0;
}

Executing the Program:

  ./file_operations -c newfile.txt -w newfile.txt "Hello, World!" -r newfile.txt -d newfile.txt

Conclusion:
Combined command-line arguments in C programming allow for creating more flexible and feature-rich command-line interfaces. By parsing and handling combined flags, developers can create programs that perform various tasks based on user inputs. The practical example of a file operations program demonstrates how combined flags can be used to create, read, write, and delete files with a single command. Experiment with different scenarios, explore additional functionalities, and apply these concepts to your programming projects. Mastering combined command-line arguments will enable you to create command-line tools that offer efficient and intuitive interactions for users.

Leave a Reply