Can I check command line arguments one by one in a loop?
You might want to make something slightly faster and more maintainable, like this (regard this as pseudo code because I haven't compiled nor tested):
typedef struct
{
const char* command;
void (*action)(void)
} arg_command_t;
const arg_command_t commands[] = //sorted in alphabetic order!
{
{ "copy", copyFile },
{ "create", createFile },
...
};
...
for(size_t i=1; i<argc; i++)
{
const arg_command_t key = { argv[i], NULL };
const arg_command_t* result;
result = bsearch(&key,
commands,
sizeof commands / sizeof *commands,
sizeof *commands,
comp_func);
if(result != NULL)
{
result->action();
}
}
Then implement comp_func
like a wrapper around strcmp
checking the string member of each struct item.