C++.


main(): - 4


/p>

file_names.push_back( string( pchar ));

. , . . , - .

, , . , :

prog - d dataOl

prog -oout_file dataOl

( .)

. ( .)

#include <iostream>

 

#include <string>

#include <vector>

 

#include <ctype.h>

 

const char *const program_name = "comline";

const char *const program_version = "version 0.01 (08/07/97)";

 

inline void usage( int exit_value = 0 )

{

//

// exit_value ...

 

cerr << " :\n"

<< program_name << " "

<< "[-d] [-h] [-v] \n\t"

<< "[-o output_file] [-l limit] \n\t"

<< "file_name\n\t[file_name [file_name [ ... ]]]\n\n"

<< " [] :\n\n\t"

<< "-h: .\n\t\t"

<< " \n\n\t"

<< "-v: .\n\t\t"

<< " \n\n\t"

<< "-d: .\n\t\t \n\n\t"

<< "-l limit\n\t\t"

<< "limit \n\n\t"

<< "-o ofile\n\t\t"

<< ", \n\t\t"

<< " \n\n"

<< "file_name\n\t\t"

<< " \n\t\t"

<< " --\n\t\t"

<< " \n\n"

<< ":\n\t\t"

<< "$command chapter7.doc\n\t\t"

<< "$command -d -l 1024 -o test_7_8 "

<< "chapter7.doc chapter8.doc\n\n";

 

exit( exit_value );

}

 

int main( int argc, char* argv[] )

{

bool debug_on = false;

bool ofile_on = false;

bool limit_on = false;

int limit = -1;

string ofile;

vector<string> file_names;

 

cout << " :\n"

<< "argc: " << argc << endl;

 

for ( int ix = 1; ix < argc; ++ix )

{

cout << "argv[ " << ix << " ]: "

<< argv[ ix ] << endl;

 

char *pchar = argv[ ix ];

switch ( pchar[ 0 ] )

{

case '-':

{

cout << " \'-\'\n";

switch( pchar[ 1 ] )

{

case 'd':

cout << " -d: "

<< " \n";

 

debug_on = true;

break;

 

case 'v':

cout << " -v: "

<< " \n";

 

cout << program_name

<< " :: "

<< program_version

<< endl;

 

return 0;

 

case 'h':

cout << " -h: "

<< "\n";

 

// break : usage()

usage();

 

case 'o':

cout << " -o: \n";

ofile_on = true;

break;

case 'l':

cout << " -l: "

<< " \n";

 

limit_on = true;

break;

 

default:

cerr << program_name

<< " : : "

<< " : - "

<< pchar << "\n\n";

 

// break : usage()

usage( -1 );

}

break;

}

 

default: //

cout << "default: : "

<< pchar << endl;

 

if ( ofile_on ) {

ofile_on = false;

ofile = pchar;

}

else

if ( limit_on ) {

limit_on = false;

limit = atoi( pchar );

if ( limit < 0 ) {

cerr << program_name

<< " : : "

<< " limit.\n\n";

 

usage( -2 );

}

}

else file_names.push_back( string( pchar ));

break;

}

}

 

if ( file_names.empty() ) {

cerr << program_name

<< " : : "

<< " .\n\n";

usage( -3 );

}

 

if ( limit != -1 )

cout << " limit: "

<< limit << endl;

 

if ( ! ofile.empty() )

cout << " : "

<< ofile << endl;

 

cout << (file_names.size() == 1 ? ", " : ", ")

<< "() :\n";

 

for ( int inx = 0; inx < file_names.size(); ++inx )

cout << "\t" << file_names[ inx ] << endl;

}

a.out -d -l 1024 -o test_7_8 chapter7.doc chapters.doc

:

:

argc: 8

argv[ 1 ]: -d

'-'

-d:

argv[ 2 ]: -l

'-'

-l:

argv[ 3 ]: 1024

default: : 1024

argv[ 4 ]: -o

'-'

-o:

argv[ 5 ]: test_7_8

default: : test_7_8

argv[ 6 ]: chapter7.doc

default: : chapter7.doc

argv[ 7 ]: chapter8.doc

default: : chapter8.doc

limit: 1024

: test_7_8

, () :

chapter7.doc

chapter8.doc