getopt

S

Spartan815

Can anyone point me to a resource that explains the getopt function
besides the man pages? Im having a bit of trouble understanding it.
Currently I am trying to work it into my program, so that from the
command line you could type something like

a.out -d /tmp

where /tmp is the directory you want to switch to, and -d is the
optional argument.

Thanks!
 
E

Emmanuel Delahaye

In said:
Can anyone point me to a resource that explains the getopt function
besides the man pages? <...>

getopt() is not part of the C-language. Please repost to a newsgroup
dedicated to your platform.
 
P

Peter Nilsson

Spartan815 said:
Can anyone point me to a resource that explains the getopt function
besides the man pages?

The internet.
Im having a bit of trouble understanding it.
Currently I am trying to work it into my program, so that from the
command line you could type something like

a.out -d /tmp

where /tmp is the directory you want to switch to, and -d is the
optional argument.

You don't need getopt for that...

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

void usage() {
puts("Get it right!");
exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
int option = 0;
char *file = 0;

if (*argv++ == 0) usage();
if (strcmp(*argv, "-d") == 0) { argv++; option = 1; }
if ((file = *argv++) == 0) usage();
if (*argv) usage();

printf("option -d: %s\n", option ? "present" : "missing");
printf("file: %s\n", file);

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top