C/Development/Command Line Parameters
Содержание
- 1 A program to list the command line arguments
- 2 Check the command line input
- 3 Check the command line parameter and use it
- 4 Check the command line parameter: if less than required exit
- 5 Check the command line parameters
- 6 Command line parameter: display all of them
- 7 Parse Arguements
- 8 Process the command line input
- 9 Use the command line parameter
- 10 Verify the user input and display file content
A program to list the command line arguments
<source lang="cpp">
- include <stdio.h>
void main(int argc, char *argv[]) {
int i = 0; printf("Program name: %s\n", argv[0]); for(i = 1 ; i<argc ; i++) printf("\nArgument %d: %s", i, argv[i]); return 1;
}
</source>
Check the command line input
<source lang="cpp">
- include <stdio.h>
int main(int argc, char *argv) {
if ( argc > 1 ) printf ( "You have initiated execution with arguments.");
}
</source>
Check the command line parameter and use it
<source lang="cpp">
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
double pounds; if(argc!=2) { printf("Usage: CONVERT <ounces>\n"); printf("Try Again"); } else { pounds = atof(argv[1]) / 16.0; printf("%f pounds", pounds); } return 0;
}
</source>
Check the command line parameter: if less than required exit
<source lang="cpp">
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
double pounds; if( argc != 2 ) { printf("Usage: CONVERT <ounces>\n"); printf("Try Again"); exit(1); /* stop the program */ } pounds = atof(argv[1]) / 16.0; printf("%f pounds", pounds); return 0;
}
</source>
Check the command line parameters
<source lang="cpp">
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
if(argc!=2) { printf("You forgot to type your name.\n"); exit(1); } printf("Hello %s", argv[1]); return 0;
}
</source>
Command line parameter: display all of them
<source lang="cpp">
- include <stdio.h>
int main(int argc, char *argv[]) {
int i; for(i = 1; i < argc; i++) printf("%s ", argv[ i ]); return 0;
}
</source>
Parse Arguements
<source lang="cpp"> /* Practical C Programming, Third Edition By Steve Oualline Third Edition August 1997 ISBN: 1-56592-306-5 Publisher: O"Reilly
- /
/* This program is an example of how to parse *
* the command line arguments. It sets up all the * * global variables for a real program, it just doesn"t * * have any body. * **/
/* Program: Print *
* * * Purpose: * * Formats files for printing * * * * Usage: * * print [options] file(s) * * * * Options: * * -v Produce versbose messages * * -o<file> Send output to a file * * (default=print.out) * * -l<lines> Set the number of lines/page. * * (default=66). * */
- include <stdio.h>
- include <stdlib.h>
int verbose = 0; /* verbose mode (default = false) */ char *out_file = "print.out"; /* output filename */ char *program_name; /* name of the program (for errors) */ int line_max = 66; /* number of lines per page */ /*
* do_file -- dummy routine to handle a file * * * * Parameter * * name -- name of the file to print * */
void do_file(char *name) {
printf("Verbose %d Lines %d Input %s Output %s\n", verbose, line_max, name, out_file);
} /*
* usage -- tell the user how to use this program and * * exit * */
void usage(void) {
fprintf(stderr,"Usage is %s [options] [file-list]\n", program_name); fprintf(stderr,"Options\n"); fprintf(stderr," -v verbose\n"); fprintf(stderr," -l<number> Number of lines\n"); fprintf(stderr," -o<name> Set output filename\n"); exit (8);
} int main(int argc, char *argv[]) {
/* save the program name for future use */ program_name = argv[0]; /* * loop for each option. * Stop if we run out of arguments * or we get an argument without a dash. */ while ((argc > 1) && (argv[1][0] == "-")) { /* * argv[1][1] is the actual option character. */ switch (argv[1][1]) { /* * -v verbose */ case "v": verbose = 1; break; /* * -o<name> output file * [0] is the dash * [1] is the "o" * [2] starts the name */ case "o": out_file = &argv[1][2]; break; /* * -l<number> set max number of lines */ case "l": line_max = atoi(&argv[1][2]); break; default: fprintf(stderr,"Bad option %s\n", argv[1]); usage(); } /* * move the argument list up one * move the count down one */ ++argv; --argc; } /* * At this point all the options have been processed. * Check to see if we have no files in the list * and if so, we need to process just standard in. */ if (argc == 1) { do_file("print.in"); } else { while (argc > 1) { do_file(argv[1]); ++argv; --argc; } } return (0);
}
</source>
Process the command line input
<source lang="cpp">
- include <stdio.h>
int main(int argc, char *argv[]) {
int t, i; for(t=0; t<argc; ++t) { i = 0; while(argv[t][i]) { putchar(argv[t][i]); ++i; } printf("\n"); } return 0;
}
</source>
Use the command line parameter
<source lang="cpp">
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
double pounds; pounds = atof(argv[1]) / 16.0; printf("%f pounds", pounds); return 0;
}
</source>
Verify the user input and display file content
<source lang="cpp">
- include <stdio.h>
- include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *fp; char ch; if(argc!=2) { printf("You forgot to enter the filename.\n"); exit(1); } if((fp=fopen(argv[1], "w"))==NULL) { printf("Cannot open file.\n"); exit(1); } do { ch = getchar(); putc(ch, fp); } while (ch != "$"); fclose(fp); return 0;
}
</source>