Changeset ae47a23


Ignore:
Timestamp:
Jun 29, 2017, 1:10:54 PM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
0d78043
Parents:
c9383ee
Message:

use getopt_long for pretty print, and update error message

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    rc9383ee rae47a23  
    1111// Created On       : Fri May 15 23:12:02 2015
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Wed Jun 28 21:56:47 2017
    14 // Update Count     : 440
     13// Last Modified On : Thu Jun 29 12:46:50 2017
     14// Update Count     : 441
    1515//
    1616
     
    481481                        break;
    482482                  case '?':
    483                         assertf( false, "Unknown option: '%c'\n", (char)optopt );
     483                        if ( optopt ) {                                                         // short option ?
     484                                assertf( false, "Unknown option: -%c\n", (char)optopt );
     485                        } else {
     486                                assertf( false, "Unknown option: %s\n", argv[optind - 1] );
     487                        } // if
    484488                  default:
    485489                        abort();
  • tools/prettyprinter/main.cc

    rc9383ee rae47a23  
    11//
    2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
     2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
     
    1010// Created On       : Wed Jun 28 22:57:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun 29 09:02:37 2017
    13 // Update Count     : 15
     12// Last Modified On : Thu Jun 29 13:09:32 2017
     13// Update Count     : 58
    1414//
    1515
     
    1919using namespace std;
    2020#include <unistd.h>                                                                             // close
     21#include <getopt.h>                                                                             // getopt
    2122#include "filter.h"
    2223
    2324extern FILE * yyin;
    24 extern int yylineno;
    2525extern int yydebug;
    2626extern int yyparse( void );
    2727
     28bool parse_cmdline( int argc, char * argv[] ) {
     29        enum { Html, Identity, Latex, Nocode, ParseTree, };
     30
     31        static struct option long_opts[] = {
     32                { "html", no_argument, nullptr, Html },
     33                { "identity", no_argument, nullptr, Identity },
     34                { "latex", no_argument, nullptr, Latex },
     35                { "nocode", no_argument, nullptr, Nocode },
     36                { "parse-tree", no_argument, nullptr, ParseTree },
     37                { nullptr, 0, nullptr, 0 }
     38        }; // long_opts
     39        int long_index;
     40
     41        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
     42
     43        int c;
     44        while ( (c = getopt_long( argc, argv, "hilnp", long_opts, &long_index )) != -1 ) {
     45                switch ( c ) {
     46                  case Html:
     47                  case 'h':
     48                        filter = HTML;
     49                        break;
     50                  case Identity:
     51                  case 'i':
     52                        filter = ::Identity;
     53                        break;
     54                  case Latex:
     55                  case 'l':
     56                        filter = LaTeX;
     57                        break;
     58                  case Nocode:
     59                  case 'n':
     60                        filter = ::Nocode;
     61                        break;
     62                  case ParseTree:
     63                  case 'p':
     64                        filter = Parse_Tree;
     65                  case '?':
     66                        if ( optopt ) {                                                         // short option ?
     67                                cerr << "Unknown option: -" << (char)optopt << endl;
     68                        } else {                                                                        // long option
     69                                cerr << "Unknown option: " << argv[optind - 1] << endl;
     70                        } // if
     71                        return false;
     72                  default:
     73                        abort();
     74                } // switch
     75        } // while
     76
     77        if ( optind != argc ) {                                                         // input files ?
     78                if ( optind == argc - 1 ) {                                             // any commands after the flags ? => input file name
     79                        yyin = fopen( argv[ optind ], "r" );
     80                        if ( yyin == nullptr ) {
     81                                cerr << "Open failure for input file \"" << argv[ optind ] << "\"" << endl;
     82                                return false;
     83                        } // if
     84                } else {
     85                        cerr << "Too many input files " << argv[ optind + 1 ] << endl;
     86                        return false;
     87                } // if
     88        } // if
     89        return true;
     90} // parse_cmdline
     91
    2892int main( int argc, char *argv[] ) {
    29         yyin = stdin;
     93        yyin = stdin;                                                                           // defaults
    3094        filter = Nocode;
    3195
    32         try {
    33                 switch ( argc ) {
    34                   case 3:
    35                         yyin = fopen( argv[ 2 ], "r" );
    36                         if ( yyin == nullptr ) {
    37                                 throw ios_base::failure( "unknown printer option arguments" );
    38                         } // if
    39                         // FALL THROUGH
    40                   case 2: {
    41                           string arg( argv[1] );
    42 
    43                           if ( arg == "-identity" ) {
    44                                   filter = Identity;
    45                           } else if ( arg == "-parse_tree" ) {
    46                                   filter = Parse_Tree;
    47                           } else if ( arg == "-nocode" ) {
    48                                   filter = Nocode;
    49                           } else if ( arg == "-latex" ) {
    50                                   filter = LaTeX;
    51                           } else if ( arg == "-html" ) {
    52                                   filter = HTML;
    53                           } else {
    54                                   throw ios_base::failure( "unknown printer option arguments" );
    55                           } // if
    56                           break;
    57                   }
    58                   default:
    59                         throw ios_base::failure( "wrong number of arguments" );
    60                 } // switch
    61         } catch( ios_base::failure err ) {
    62                 cerr << err.what() << endl;
    63                 cerr << "Usage: [" << argv[0]
    64                          << "-identity |"
    65                          << "-parse_tree |"
    66                          << "-nocode |"
    67                          << "-latex |"
    68                          << "-html"
     96        if ( ! parse_cmdline( argc, argv ) ) {
     97                cerr << "Usage: " << argv[0]
     98                         << " ["
     99                         << "-h/--html | "
     100                         << "-i/--identity | "
     101                         << "-l/--latex | "
     102                         << "-n/--nocode | "
     103                         << "-p/--parse-tree"
    69104                         << "] [input-file]"
    70105                         << endl;
    71106                exit( EXIT_FAILURE );                                                   // TERMINATE
    72         } // try
     107        } // if
    73108
    74109        //yydebug = 1;
Note: See TracChangeset for help on using the changeset viewer.