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"
27 
28 #include <filesystem>
29 #include <iostream>
30 
31 #include <SimoxUtility/algorithm/string/string_tools.h>
32 
34 
35 namespace armarx
36 {
37  CmdOptions
39  {
40  using namespace boost::program_options;
41  CmdOptions options;
42  options.error = false;
43  options.showHelp = false;
44  options.print = false;
45  options.wait = false;
46  options.scxPath = "";
47  options.parameters = "";
48 
49  // create program options
50  options.description.reset(new options_description(
51  std::string("This executable is an application within the ArmarX framework.\n") +
52  "==================================================================\n" +
53  " Starting an ArmarX Scenario\n" +
54  "==================================================================\n" +
55  "In order to start a Scenario pass either a path to an scx file or a Scenario name "
56  "with a command." +
57  " Example command:\n\n"
58 
59  + " > armarx scenario command scxpath/scxname [package name] [-a application]\n\n"
60 
61  + "Help options"));
62 
63 
64  options.description->add_options()("help,h", "Help option");
65 
66  options.description->add_options()(
67  "command,c",
68  value<std::string>(),
69  "What should be done: {\n \tstart, \n \tstop, \n \tkill, \n \trestart (kills by "
70  "default), \n \tlist (List all Scenario in Packages in ArmarX.DefaultPackages)\n "
71  "\tstatus (Show the status of the Scenario)\n \t generate (generate the old "
72  "local/remote icegrid xml configs)\n}");
73  options.description->add_options()("file,f",
74  value<std::string>()->default_value("."),
75  "Path to the scx file (default: looks in current dir)");
76  options.description->add_options()(
77  "package,p",
78  value<std::string>(),
79  "[optional] If the packages are included in your default config as "
80  "ArmarX.DefaultPackages this can be left empty");
81  options.description->add_options()(
82  "application,a",
83  value<std::string>(),
84  "[optional] Application out of the Scenario to execute the command with");
85  options.description->add_options()("parameters",
86  value<std::string>()->default_value(""),
87  "[optional] Use this if you want to start an scenario "
88  "with additional commandline parameters");
89  options.description->add_options()(
90  "print", "Print the execution commands of the CLI instead of executing them");
91  options.description->add_options()("wait,w",
92  "Wait for all applications to quit before exiting");
93 
94  positional_options_description pd;
95  pd.add("command", 1);
96  pd.add("file", 1);
97  pd.add("package", 1);
98  pd.add("application", 1);
99 
100  variables_map vm;
101  store(command_line_parser(argc, argv)
102  .options(*options.description)
103  .positional(pd)
104  .allow_unregistered()
105  .run(),
106  vm);
107  notify(vm);
108 
109  if (vm.count("help"))
110  {
111  options.showHelp = true;
112  return options;
113  }
114 
115  if (vm.count("file"))
116  {
117  options.scxPath = vm["file"].as<std::string>();
118  ;
119  }
120 
121  if (vm.count("command"))
122  {
123  options.command = vm["command"].as<std::string>();
124  options.command = simox::alg::to_lower(options.command);
125 
126  if (options.command != "start" && options.command != "stop" &&
127  options.command != "kill" && options.command != "restart" &&
128  options.command != "list" && options.command != "generate" &&
129  options.command != "status" && options.command != "periodic_status")
130  {
131  options.error = true;
132  }
133  }
134 
135  if (vm.count("package"))
136  {
137  options.packageName = vm["package"].as<std::string>();
138  }
139 
140  if (vm.count("application"))
141  {
142  options.applicationName = vm["application"].as<std::string>();
143  }
144 
145  if (vm.count("parameters"))
146  {
147  options.parameters = vm["parameters"].as<std::string>();
148  }
149  if (vm.count("print"))
150  {
151  options.print = true;
152  }
153  if (vm.count("wait"))
154  {
155  options.wait = true;
156  }
157 
158  if (options.scxPath.empty() || options.command.empty())
159  {
160  options.error = true;
161  }
162 
163  return options;
164  }
165 
166  void
167  ScenarioManagerCliOptions::showHelp(CmdOptions options, std::string errorMessage)
168  {
169  std::cout << *options.description << std::endl;
170  if (!errorMessage.empty())
171  {
172  std::cout << "\033[1m" << LogSender::GetColorCodeString(LogSender::eRed) << std::endl
173  << "Error: " << LogSender::GetColorCodeString(LogSender::eReset) << "\033[1m"
174  << errorMessage << std::endl;
175  }
176  }
177 
178  std::string
180  {
181  if (options.scxPath.empty())
182  {
183  return "";
184  }
185 
186  std::filesystem::path boostScxPath(options.scxPath);
187 
188  if (std::filesystem::exists(boostScxPath))
189  {
190  if (std::filesystem::is_regular_file(boostScxPath))
191  {
192  return boostScxPath.stem().string();
193  }
194  else if (std::filesystem::is_directory(boostScxPath))
195  {
196  std::vector<std::string> all_matching_files;
197 
198  std::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
199  for (std::filesystem::directory_iterator i(boostScxPath.string()); i != end_itr;
200  ++i)
201  {
202  // Skip if not a file
203  if (!std::filesystem::is_regular_file(i->status()))
204  {
205  continue;
206  }
207 
208  if (!(i->path().extension() == ".scx"))
209  {
210  continue;
211  }
212 
213  // File matches, store it
214  all_matching_files.push_back(i->path().stem().string());
215  }
216 
217  if (all_matching_files.size() == 0)
218  {
219  return "";
220  }
221  else if (all_matching_files.size() == 1)
222  {
223  return all_matching_files[0];
224  }
225  else
226  {
227  std::cout << "Warning: found multiple scx files in this directory choosing "
228  << all_matching_files[0];
229  return all_matching_files[0];
230  }
231  }
232  else
233  {
234  return "";
235  }
236  }
237  else
238  {
239  //input should be an Scenario name now
240  return options.scxPath;
241  }
242  }
243 } // namespace armarx
armarx::CmdOptions::command
std::string command
Definition: ScenarioCliOptions.h:41
armarx::ScenarioManagerCliOptions::showHelp
void showHelp(CmdOptions options, std::string errorMessage="")
Definition: ScenarioCliOptions.cpp:167
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:38
armarx::LogSender::eRed
@ eRed
Definition: LogSender.h:72
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:179
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:36
armarx::CmdOptions::print
bool print
Definition: ScenarioCliOptions.h:38
armarx::armem::server::ltm::mongodb::util::store
void store(const mongocxx::database &db, const armem::wm::Memory &m)
Definition: operations.cpp:260
armarx::CmdOptions::parameters
std::string parameters
Definition: ScenarioCliOptions.h:44
ScenarioCliOptions.h
armarx::CmdOptions::error
bool error
Definition: ScenarioCliOptions.h:37
armarx::CmdOptions::wait
bool wait
Definition: ScenarioCliOptions.h:39
armarx::CmdOptions::applicationName
std::string applicationName
Definition: ScenarioCliOptions.h:43
armarx::CmdOptions
Definition: ScenarioCliOptions.h:34
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:27
armarx::LogSender::eReset
@ eReset
Definition: LogSender.h:70