1 / 10

C Command Line Arguments

C Command Line Arguments. Command Line Arguments. Functions have arguments Caller specify arguments when calling the function main() function also have arguments Who specifies arguments for main()? Examples: Most UNIX tools take command line arguments cp – r dir1 dir2. main().

ruggeri
Download Presentation

C Command Line Arguments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Command Line Arguments

  2. Command Line Arguments • Functions have arguments • Caller specify arguments when calling the function • main() function also have arguments • Who specifies arguments for main()? • Examples: • Most UNIX tools take command line arguments • cp –r dir1 dir2 Csc352-Summer03, Stanley Yao

  3. main() int main(int argc, char *argv[]); • argc: argument count • argv: argument vector • Each element is a “char *” pointing to a null-terminated char array (string) • argv[0] contains the name of the command • argv[i] contains the ith argument (0<i<argc) • All command line arguments are represented as strings (e.g. “123”) Csc352-Summer03, Stanley Yao

  4. Converting Arguments • int atoi(const char *str); • long int atol(const char *str); • int sscanf(const char *str, const char *format, ...); • int getopt (int argc, char * const argv[], const char *optstring); Csc352-Summer03, Stanley Yao

  5. Example: echo % echo hello, world argc argv echo\0 hello,\0 world\0 Csc352-Summer03, Stanley Yao

  6. ““ on the command line • Group multiple words into one single argument • Prevent the shell from interpreting special characters like “*” argv argv echo\0 echo\0 hello,\0 hello, world\0 world\0 % echo hello, world % echo “hello, world” Csc352-Summer03, Stanley Yao

  7. Process Options • strstr(): find substring • strcmp(): string compare • etc. • getopt(): more standardized option processing routine • getopt_long() • getopt_long_only() Csc352-Summer03, Stanley Yao

  8. getopt() #include <unistd.h> int getopt (int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; extern void getoptreset (void); • argc: argument count • argv: argument vector • sptstring: acepted arguments spec • Option followed by “:” in the spec has option argument • optarg: point to the option argument of the current option we are processing • opterr: disable getopt error message • optopt: value of the char that caused the error Csc352-Summer03, Stanley Yao

  9. getopt() (cont.) • Successful: the next option character • “:”: a missing option argument; optstring begin with “:” • “?”: encounters an option character not in optstring; or a missing option argument, but sptstring does not begin with “:” • -1: otherwise • We can use this to handle non-option command line arguments Csc352-Summer03, Stanley Yao

  10. Acknowledgement • John H. Hartman, Classnotes for Csc352-Spring03, CS Dept., University of Arizona, 2003 • Brian W. Kernighan, Dennis M. Ritchie, The C Programming Language (2nd Ed.), Prentice Hall, 1988 Csc352-Summer03, Stanley Yao

More Related