]> git.donarmstrong.com Git - mothur.git/blob - systemcommand.cpp
5bc9b46e47522d38fe4a89f0119df8785a4d8d4d
[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
14 SystemCommand::SystemCommand(string option){
15         try {
16                 abort = false;
17                 
18                 //allow user to run help
19                 if(option == "help") { help(); abort = true; }
20                 
21                 else {
22                         //valid paramters for this command
23                         string Array[] =  {"command"};
24                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25                         
26                         OptionParser parser(option);
27                         map<string,string> parameters = parser.getParameters();
28                         
29                         ValidParameters validParameter;
30                         
31                         //check to make sure all parameters are valid for command
32                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
33                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
34                         }
35                         
36                         //check for required parameters
37                         command = validParameter.validFile(parameters, "command", false);
38                         if (command == "not found") { mothurOut("command is a required parameter."); mothurOutEndLine(); abort = true; }
39                 }       
40
41         }
42         catch(exception& e) {
43                 errorOut(e, "SystemCommand", "SystemCommand");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48
49 void SystemCommand::help(){
50         try {
51                 mothurOut("The system command allows you to execute a system command from within mothur.\n");
52                 mothurOut("The system command parameter is command and it is required.\n");
53                 mothurOut("The system command should be in the following format: system(command=yourCommand).\n");
54                 mothurOut("Example system(command=clear).\n");
55                 mothurOut("Note: No spaces between parameter labels (i.e. command), '=' and parameters (i.e.yourCommand).\n\n");
56         }
57         catch(exception& e) {
58                 errorOut(e, "SystemCommand", "help");
59                 exit(1);
60         }
61 }
62
63 //**********************************************************************************************************************
64
65 int SystemCommand::execute(){
66         try {
67                 
68                 if (abort == true) { return 0; }
69                 
70                 system(command.c_str());
71                 
72                 return 0;               
73         }
74
75         catch(exception& e) {
76                 errorOut(e, "SystemCommand", "execute");
77                 exit(1);
78         }
79 }
80
81 //**********************************************************************************************************************