getopt - help

Reply

Join Date: Sep 2008
Posts: 3
Reputation: TooMpa is an unknown quantity at this point 
Solved Threads: 1
TooMpa TooMpa is offline Offline
Newbie Poster

getopt - help

 
0
  #1
Sep 24th, 2008
Hi like to write a program that can take arguments like %program -t <type> <path> <path> <name>

where -t follow by type is the file type I like to look for in a find program, and path's is where I want to look, followed by the name of the file I like to search for....

like

%program -t f (file) ~testfolder1 ~testfolder2 help

The problem is I don't know how to get getopt to read -t and then take next char as an argument to look for S_ISREG(m) for example..

Would be great with a lite help, will try to update the thread later on with code if someone have same problem..

  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main (int argc, char *argv[]){
  5.  
  6. int c;
  7. char *path, filetype;
  8. extern char *optarg;
  9. extern int optind, optopt, opterr;
  10.  
  11. while ((c = getopt(argc, argv, "t")) != -1){
  12.  
  13. switch(c){
  14.  
  15. case 't':
  16. filetype = ????; // What the *** should be here? :D
  17. printf("Type you are looking for is:
  18. %c\n", filetype);
  19. printf("option: %c\n",c);
  20. break;
  21.  
  22. case '?':
  23. fprintf(stderr, "Unrecognised option:
  24. -%c\n",optopt);
  25. }
  26. }
  27. // This is just for listing witch arguments there is..
  28. for(i=1;i<argc;i++){
  29. printf("argv: %s\n",argv[i]);
  30. }
  31.  
  32. return 0;
  33.  
  34. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: getopt - help

 
0
  #2
Sep 24th, 2008
As far as I know you need "t:" getopt option. May be I'm wrong (I don't like getopt ).
See http://www.gnu.org/software/libtool/...l#Using-Getopt
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: getopt - help

 
0
  #3
Sep 25th, 2008
Yeah it should be "t:" and if t is an optional argument it should be "t::"

why don't you like getopt ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: getopt - help

 
0
  #4
Sep 27th, 2008
Nothing serious. I don't like a rigid and unclear (in my opinion) getopt loop (and option string too ). It's a matter of personal preferences only.
Probably the getopt is the best solution in C, but in C++ it looks like a steam-engine in Tokkaido Line train... But it's the other story...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: getopt - help

 
0
  #5
Sep 28th, 2008
filetype = ????;
So if the flag matches, you can get the argument by the global variable optarg. That should hold pointer to the argument which you are looking for!

  1. filetype = optarg;

ssharish
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC