Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created April 29, 2015 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twyatt/d62217953ed386bfc703 to your computer and use it in GitHub Desktop.
Save twyatt/d62217953ed386bfc703 to your computer and use it in GitHub Desktop.
CS 320 flag parsing
// parse command line arguments for flags (i.e. help and output filename)
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') { /* flag */
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
usage(argv[0]);
exit(EXIT_SUCCESS);
}
if (strcmp(argv[i], "-o") == 0) {
if (i >= argc - 1) {
fprintf(stderr, "Missing output file argument.\n");
exit(EXIT_FAILURE);
} else {
char output_file[255];
int result = sscanf(argv[++i], "%255s", output_file);
if (result != 1) {
fprintf(stderr, "Error reading output file parameter: %s\n", argv[i]);
exit(EXIT_FAILURE);
}
output = fopen(output_file, "w");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment