ch2-getopt

Chapter_2     arglist buffering







getopt_long.c     ALP, p. 29-31


#include <getopt.h> // for struct option, optarg, optind, getopt_long()
#include <stdio.h> // for printf(), fprintf(), stdout, stderr, NULL,
// getchar(), EOF, FILE, fopen(), fputc(), fclose()
#include <stdlib.h> // for exit(), abort()

const char* prog; // program name, argv[0]

void print_usage (FILE* stream, int exit_code);

int main (int argc, char* argv[])
{
prog = argv[0]; // program name

if (argc == 1)
{print_usage (stdout, 0);} // exit code 0 (normal termination)

int next_option; // return value of getopt_long()
const char* const short_options = "ho:v";

const struct option long_options[] =
{
{ "help", 0, NULL, 'h' }, // no arguments
{ "output", 1, NULL, 'o' }, // one arg, a file name
{ "verbose", 0, NULL, 'v' }, // no arg
{ NULL, 0, NULL, 0 } // Required at end of array
};

const char* out = NULL; //output filename, NULL for stdout
int verbose = 0; // Whether to display verbose messages

do
{
next_option = getopt_long (argc, argv, short_options,
long_options, NULL);
switch (next_option)
{
case 'h': // -h or --help
print_usage (stdout, 0); // exit code 0 (normal termination)
case 'o': // -o or --output
out = optarg; // output filename
break;
case 'v': // -v or --verbose
verbose = 1;
break;
case '?': // user specified an invalid option
print_usage (stderr, 1); // exit code 1 (abnormal termination)
case -1: // no more options are found
break;
default: // Something else, unexpected
abort (); // end program abruptly (do not flush streams
} // or close open files)
} while (next_option != -1); // Done with options

if (verbose) // print arguments following options
{ // `optind' points to first nonoption argument:
int i;
for (i = optind; i < argc; i++)
{printf ("Argument: %s\n", argv[i]);}
}

FILE *fp = NULL;
if (out != NULL)
{fp = fopen(out, "w");} // open for writing
if (fp == NULL) // if no out file or error opening file
{fp = stdout;}

int c;

while ((c = getchar()) != EOF)
{fputc(c, fp);}

if (fp != NULL)
{fclose(fp);}

return 0;
} // return from main() flushes streams and closes open files

void print_usage (FILE* stream, int exit_code)
{ // `stream' is typically `stdout' or `stderr'
fprintf (stream, "Usage: %s options [inputfile ...]\n", prog);
fprintf (stream, " -h --help Display this usage information\n"
" -o --output filename Write output to file\n"
" -v --verbose Print verbose messages\n"); // concatenate strings
exit (exit_code); // end program with `exit_code'
} // exit() flushes streams and closes open files

/*
gcc getopt_long.c -o getopt_long
./getopt_long
Usage: ./getopt_long options [inputfile ...]
-h --help Display this usage information
-o --output filename Write output to file
-v --verbose Print verbose messages

./getopt_long -hh --help
Usage: ...

./getopt_long -?
./getopt_long: invalid option -- '?'
Usage: ...

./getopt_long --?
./getopt_long: unrecognized option '--?'
Usage: ...

./getopt_long -v
Hello!
Hello!
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)

./getopt_long -hov out.txt // no file is created
Usage: ...

./getopt_long -ov out.txt // file `v' is created
Hello!
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
// `v' contains "Hello!"
// no verbose messages are printed

rm v // delete file `v'
./getopt_long -v -o out.txt // file `out.txt' is created
Hello! // Enter
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
// `out.txt' contains "Hello!"
// no verbose messages are printed

./getopt_long -v -o out.txt Hello!
Argument: Hello!
How are you today? // Enter
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
// `out.txt' contains "How are you today?"

./getopt_long Hello! -o out.txt "What's" -v up?
Argument: Hello!
Argument: What's // ' is interpreted by the shell, so we write "What's"
Argument: up?
Have a nice day! // Enter
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
// `out.txt' contains "Have a nice day!"

rm out.txt // clean
*/









Chapter_2     arglist BACK_TO_TOP buffering



Comments

Popular posts from this blog

Contents