Created
April 29, 2015 23:54
-
-
Save twyatt/d62217953ed386bfc703 to your computer and use it in GitHub Desktop.
CS 320 flag parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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