ScenarioCliOptions.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Cedric Seehausen (usdnr at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
26 #include "ScenarioCliOptions.h"
28 #include <SimoxUtility/algorithm/string/string_tools.h>
29 #include <filesystem>
30 #include <iostream>
31 
32 namespace armarx
33 {
35  {
36  using namespace boost::program_options;
37  CmdOptions options;
38  options.error = false;
39  options.showHelp = false;
40  options.print = false;
41  options.wait = false;
42  options.scxPath = "";
43  options.parameters = "";
44  options.remote = false;
45 
46  // create program options
47  options.description.reset(new options_description(std::string(
48  "This executable is an application within the ArmarX framework.\n")
49  + "==================================================================\n"
50  + " Starting an ArmarX Scenario\n"
51  + "==================================================================\n"
52  + "In order to start a Scenario pass either a path to an scx file or a Scenario name with a command."
53  + " Example command:\n\n"
54 
55  + " > armarx scenario command scxpath/scxname [package name] [-a application]\n\n"
56 
57  + "Help options"));
58 
59 
60  options.description->add_options()("help,h", "Help option");
61 
62  options.description->add_options()("command,c", value <std::string>(), "What should be done: {\n \tstart, \n \tdeploy, \n \tstop, \n \tkill, \n \tremove, \n \trestart (kills by default), \n \tlist (List all Scenario in Packages in ArmarX.DefaultPackages)\n \tstatus (Show the status of the Scenario)\n \t generate (generate the old local/remote icegrid xml configs)\n}");
63  options.description->add_options()("file,f", value <std::string>()->default_value("."), "Path to the scx file (default: looks in current dir)");
64  options.description->add_options()("package,p", value<std::string>(), "[optional] If the packages are included in your default config as ArmarX.DefaultPackages this can be left empty");
65  options.description->add_options()("application,a", value<std::string>(), "[optional] Application out of the Scenario to execute the command with");
66  options.description->add_options()("parameters", value<std::string>()->default_value(""), "[optional] Use this if you want to start an scenario with additional commandline parameters");
67  options.description->add_options()("print", "Print the execution commands of the CLI instead of executing them");
68  options.description->add_options()("wait,w", "Wait for all applications to quit before exiting");
69  options.description->add_options()("remote", "[optional] if you want to deploy an Scenario remotely this has to be added to the command");
70 
71  positional_options_description pd;
72  pd.add("command", 1);
73  pd.add("file", 1);
74  pd.add("package", 1);
75  pd.add("application", 1);
76 
77  variables_map vm;
78  store(command_line_parser(argc, argv).options(*options.description).positional(pd).allow_unregistered().run(), vm);
79  notify(vm);
80 
81  if (vm.count("help"))
82  {
83  options.showHelp = true;
84  return options;
85  }
86 
87  if (vm.count("file"))
88  {
89  options.scxPath = vm["file"].as<std::string>();;
90  }
91 
92  if (vm.count("command"))
93  {
94  options.command = vm["command"].as<std::string>();
95  options.command = simox::alg::to_lower(options.command);
96 
97  if (options.command != "start"
98  && options.command != "stop"
99  && options.command != "kill"
100  && options.command != "restart"
101  && options.command != "list"
102  && options.command != "generate"
103  && options.command != "status"
104  && options.command != "deploy"
105  && options.command != "remove"
106  && options.command != "periodic_status"
107  )
108  {
109  options.error = true;
110  }
111  }
112 
113  if (vm.count("package"))
114  {
115  options.packageName = vm["package"].as<std::string>();
116  }
117 
118  if (vm.count("application"))
119  {
120  options.applicationName = vm["application"].as<std::string>();
121  }
122 
123  if (vm.count("parameters"))
124  {
125  options.parameters = vm["parameters"].as<std::string>();
126  }
127  if (vm.count("print"))
128  {
129  options.print = true;
130  }
131  if (vm.count("wait"))
132  {
133  options.wait = true;
134  }
135  if (vm.count("remote"))
136  {
137  options.remote = true;
138  }
139 
140  if (options.scxPath.empty() || options.command.empty())
141  {
142  options.error = true;
143  }
144 
145  return options;
146  }
147 
148  void ScenarioManagerCliOptions::showHelp(CmdOptions options, std::string errorMessage)
149  {
150  std::cout << *options.description << std::endl;
151  if (!errorMessage.empty())
152  {
153  std::cout << "\033[1m" << LogSender::GetColorCodeString(LogSender::eRed) << std::endl
154  << "Error: " << LogSender::GetColorCodeString(LogSender::eReset) << "\033[1m" << errorMessage << std::endl;
155  }
156  }
157 
159  {
160  if (options.scxPath.empty())
161  {
162  return "";
163  }
164 
165  std::filesystem::path boostScxPath(options.scxPath);
166 
167  if (std::filesystem::exists(boostScxPath))
168  {
169  if (std::filesystem::is_regular_file(boostScxPath))
170  {
171  return boostScxPath.stem().string();
172  }
173  else if (std::filesystem::is_directory(boostScxPath))
174  {
175  std::vector< std::string > all_matching_files;
176 
177  std::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
178  for (std::filesystem::directory_iterator i(boostScxPath.string()); i != end_itr; ++i)
179  {
180  // Skip if not a file
181  if (!std::filesystem::is_regular_file(i->status()))
182  {
183  continue;
184  }
185 
186  if (!(i->path().extension() == ".scx"))
187  {
188  continue;
189  }
190 
191  // File matches, store it
192  all_matching_files.push_back(i->path().stem().string());
193  }
194 
195  if (all_matching_files.size() == 0)
196  {
197  return "";
198  }
199  else if (all_matching_files.size() == 1)
200  {
201  return all_matching_files[0];
202  }
203  else
204  {
205  std::cout << "Warning: found multiple scx files in this directory choosing " << all_matching_files[0];
206  return all_matching_files[0];
207  }
208  }
209  else
210  {
211  return "";
212  }
213  }
214  else
215  {
216  //input should be an Scenario name now
217  return options.scxPath;
218  }
219  }
220 }
armarx::CmdOptions::command
std::string command
Definition: ScenarioCliOptions.h:41
armarx::ScenarioManagerCliOptions::showHelp
void showHelp(CmdOptions options, std::string errorMessage="")
Definition: ScenarioCliOptions.cpp:148
armarx::LogSender::GetColorCodeString
static std::string GetColorCodeString(MessageTypeT verbosityLevel)
Definition: LogSender.cpp:300
armarx::ScenarioManagerCliOptions::parseCmdOptions
CmdOptions parseCmdOptions(int argc, char *argv[])
Definition: ScenarioCliOptions.cpp:34
armarx::LogSender::eRed
@ eRed
Definition: LogSender.h:73
armarx::CmdOptions::description
std::shared_ptr< boost::program_options::options_description > description
Definition: ScenarioCliOptions.h:45
armarx::ScenarioManagerCliOptions::GetScenarioNameByCommandLineInput
static std::string GetScenarioNameByCommandLineInput(CmdOptions string)
Definition: ScenarioCliOptions.cpp:158
boost::program_options
Definition: ApplicationOptions.h:37
armarx::CmdOptions::packageName
std::string packageName
Definition: ScenarioCliOptions.h:42
armarx::CmdOptions::showHelp
bool showHelp
Definition: ScenarioCliOptions.h:35
armarx::CmdOptions::print
bool print
Definition: ScenarioCliOptions.h:37
armarx::armem::server::ltm::mongodb::util::store
void store(const mongocxx::database &db, const armem::wm::Memory &m)
Definition: operations.cpp:237
armarx::CmdOptions::parameters
std::string parameters
Definition: ScenarioCliOptions.h:44
ScenarioCliOptions.h
armarx::CmdOptions::remote
bool remote
Definition: ScenarioCliOptions.h:39
armarx::CmdOptions::error
bool error
Definition: ScenarioCliOptions.h:36
armarx::CmdOptions::wait
bool wait
Definition: ScenarioCliOptions.h:38
armarx::CmdOptions::applicationName
std::string applicationName
Definition: ScenarioCliOptions.h:43
armarx::CmdOptions
Definition: ScenarioCliOptions.h:33
Logging.h
armarx::CmdOptions::scxPath
std::string scxPath
Definition: ScenarioCliOptions.h:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::LogSender::eReset
@ eReset
Definition: LogSender.h:71