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