Quick fix for Q_ASSERT error, may rework m_pMiniLexica for next release
[linguistica.git] / CommandLine.cpp
blob69e4e2f5c60bf3dcc2e8a99d1b77bcd9f4d2deae
1 // Implementation of command-line argument processing functions
2 // Copyright © 2009 The University of Chicago
3 #include "CommandLine.h"
5 #include <cstdio>
6 #include <QString>
8 /// Requires: variables referred to by arguments pre-initialized with
9 /// their default values.
10 int processCommandLineArgs(int argc, char** argv,
11 QString& corpus, int& count, QString& logFile,
12 QString& goldStdFile, QString& outputDir,
13 bool& mini, bool& prefix, bool& suffix, bool& verbose,
14 QString& error )
16 QString arg, temp;
17 int i, j;
19 if( argc < 2 )
21 if( argv[0] == QString("-h") ||
22 argv[0] == QString("--help") ||
23 argv[1] == QString("-h") ||
24 argv[1] == QString("--help") )
26 error = "";
27 return -1;
31 for( i=0; i<argc-1; i++ )
33 arg = QString( argv[i] );
35 if( arg == "--help" || arg == "-h" )
37 error = "";
38 return -1;
41 if( arg.find("-") == 0 )
43 if( arg.find("c") > 0 )
45 if( arg.length() > 2 )
47 error = "Illegal option combination : \'" + arg + "\'\n\n";
48 return -1;
51 if( i < argc-2 )
53 temp = QString( argv[i+1] );
54 // Check each digit is number
55 for( j=0; j< static_cast <int> (temp.length()); j++ )
57 if( !temp[j].isNumber() )
59 error = "Illegal string following \'-c\' option : \'temp\'\n\n";
60 return -1;
63 count = temp.toInt();
65 else
67 error = "Illegal use of \'-c\' option.\n\n";
68 return -1;
71 else if( arg.find("g") > 0 )
73 if( arg.length() > 2 )
75 error = "Illegal option combination : \'" + arg + "\'\n\n";
76 return -1;
79 if( i < argc-2 )
81 goldStdFile = QString( argv[i+1] );
83 else
85 error = "Illegal use of \'-g\' option.\n\n";
86 return -1;
89 else if( arg.find("l") > 0 )
91 if( arg.length() > 2 )
93 error = "Illegal option combination : \'" + arg + "\'\n\n";
94 return -1;
97 if( i < argc-2 )
99 logFile = QString( argv[i+1] );
101 else
103 error = "Illegal use of \'-l\' option.\n\n";
104 return -1;
107 else if( arg.find("o") > 0 )
109 if( arg.length() > 2 )
111 error = "Illegal option combination : \'" + arg + "\'\n\n";
112 return -1;
115 if( i < argc-2 )
117 outputDir = QString( argv[i+1] );
119 else
121 error = "Illegal use of \'-o\' option.\n\n";
122 return -1;
125 else
127 for( j = 1; j < static_cast <int> ( arg.length() ); j++ )
129 if( QString("fmpsv").find( arg[j] ) < 0 )
131 error = "Illegal option : \'" + QString( arg[j] ) + "\'\n\n";
132 return -1;
136 if( arg.find("m") > 0 ) mini = true;
137 if( arg.find("p") > 0 ) prefix = true;
138 if( arg.find("s") > 0 ) suffix = true;
139 if( arg.find("v") > 0 ) verbose = true;
144 corpus = QString( argv[argc-1] );
146 if( corpus == "--help" || corpus == "-h" )
148 error = "";
149 return -1;
152 return 0;
155 void DisplayHelp()
157 using std::printf;
159 printf( "Usage: linguistica [options] corpus\n" );
160 printf( "Options:\n" );
161 printf( " -c X\t\tX is an integer. Read X number of words from the corpus for the analysis. If omitted, default is 10000.\n" );
162 printf( " -g gold-std\tCompare results to this gold standard. File must be XML format created by Alchemist and readable.\n" );
163 printf( " -h\t\tHelp: void use of all other options and arguments and print option informations.\n" );
164 printf( " -l log-file\tStore log information to this file name. File must be writeable.\n" );
165 printf( " -m\t\tFind more than one level of morphology using mini-lexica.\n" );
166 printf( " -o output-dir\tOutput to this directory (default is same directory as corpus). Directory must be writeable.\n" );
167 printf( " -p\t\tFind prefixes.\n" );
168 printf( " -s\t\tFind suffixes.\n" );
169 printf( " -v\t\tVerbose: print the log to the screen.\n\n" );
171 printf( "Example:\n\n " );
173 printf( " linguistica -spm -l myLog.txt -c 50000 BrownCorpus.txt\n\n" );
175 printf( "Using the first 50,000 distinct words of the file \'BrownCorpus.txt\',\n" );
176 printf( "find more than one level of suffixes and prefixes using mini-lexica. Keep\n" );
177 printf( "a log of all actions in \'myLog.txt.\'\n\n" );