]> git.donarmstrong.com Git - mothur.git/blob - systemcommand.cpp
6a1e3555f80ffd3f6f5a1565296faf4072995629
[mothur.git] / systemcommand.cpp
1 /*
2  *  systemcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "systemcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> SystemCommand::setParameters(){  
14         try {
15                 CommandParameter pcommand("command", "String", "", "", "", "", "",false,false); parameters.push_back(pcommand);
16                                 
17                 vector<string> myArray;
18                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "SystemCommand", "setParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 SystemCommand::SystemCommand(string option)  {
28         try {
29                 abort = false; calledHelp = false;   
30                 
31                 //allow user to run help
32                 if(option == "help") { help(); abort = true; calledHelp = true; }
33                 
34                 else {
35                         vector<string> myArray = setParameters();
36                         
37                         OptionParser parser(option);
38                         map<string, string> parameters = parser.getParameters();
39                         map<string, string>::iterator it;
40                         
41                         ValidParameters validParameter;
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         string commandOption = validParameter.validFile(parameters, "command", false);                  
46                         if (commandOption == "not found") { commandOption = ""; }
47                         else { command = commandOption; }
48                         
49                         if ((option == "") && (commandOption == "")) { m->mothurOut("You must enter a command to run."); m->mothurOutEndLine(); abort = true; }
50                         else if (commandOption == "") { 
51                                 //check for outputdir and inputdir parameters
52                                 int commaPos = option.find_first_of(',');
53                                 
54                                 //if there is a comma then grab string up to that pos
55                                 if (commaPos != option.npos) {
56                                         option = option.substr(0, commaPos);
57                                 }
58                         
59                                 command = option;
60                         }
61                 }       
62
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "SystemCommand", "SystemCommand");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 string SystemCommand::getHelpString(){
72         try {
73                 string helpString = "";
74                 helpString += "The system command allows you to execute a system command from within mothur.\n";
75                 helpString += "The system has no parameters.\n";
76                 helpString += "The system command should be in the following format: system(yourCommand).\n";
77                 helpString += "Example system(clear).\n";
78                 return helpString;
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "SystemCommand", "help");
82                 exit(1);
83         }
84 }
85
86 //**********************************************************************************************************************
87
88 int SystemCommand::execute(){
89         try {
90                 
91                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
92                 
93                 system(command.c_str());
94                 
95                 return 0;               
96         }
97
98         catch(exception& e) {
99                 m->errorOut(e, "SystemCommand", "execute");
100                 exit(1);
101         }
102 }
103
104 //**********************************************************************************************************************