00001 /* 00002 * Copyright (c) 2010 Ferruccio Vitale <unixo@devzero.it> 00003 * All rights reserved. 00004 * 00005 * $Id: CommandLine.cpp 70 2010-07-22 12:44:53Z unixo $ 00006 */ 00007 00008 #include "CommandLine.h" 00009 #include <stdlib.h> 00010 00011 CommandLine::CommandLine(int argc, char * const argv[]) : _argc(argc), 00012 _argv(argv), _opts("u:p:s:") 00013 { 00014 int ch; 00015 00016 _fault = false; 00017 _user = "root", _password = "secret", _server = "localhost"; 00018 00019 while ((ch = parseNext()) != EOF) { 00020 switch (ch) { 00021 case 'p': 00022 _password = optionArgument(); 00023 break; 00024 case 's': 00025 _server = optionArgument(); 00026 break; 00027 case 'u': 00028 _user = optionArgument(); 00029 break; 00030 default: 00031 parseError(); 00032 return; 00033 } 00034 } 00035 } 00036 00037 void CommandLine::parseError() 00038 { 00039 cerr << "Unknown parameter!\n"; 00040 printUsage(); 00041 _fault = true; 00042 } 00043 00044 void CommandLine::printUsage() const 00045 { 00046 std::cerr << "usage: ec++ [ -u user ] [ -p password ] [ -s server ]\n\n"; 00047 } 00048 00049 const char * CommandLine::dbUser() const 00050 { 00051 return _fault?NULL:_user; 00052 } 00053 00054 const char * CommandLine::dbPasswd() const 00055 { 00056 return _fault?NULL:_password; 00057 } 00058 00059 const char * CommandLine::dbServer() const 00060 { 00061 return _fault?NULL:_server; 00062 } 00063 00064 int CommandLine::parseNext() const 00065 { 00066 return getopt(_argc, _argv, _opts); 00067 } 00068 00069 const char * CommandLine::optionArgument() const 00070 { 00071 return optarg; 00072 } 00073 00074 bool CommandLine::isFault() 00075 { 00076 return _fault; 00077 }