CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Complete.cc
Go to the documentation of this file.
1 #ifndef COMPLETE_CC
2 #define COMPLETE_CC
3 
5 
8 
9 namespace CASM {
10 namespace Completer {
12 
13 std::string strip_argument(const std::string &raw_input) {
14  std::string stripped = raw_input;
15 
16  if (stripped[0] == '-') {
17  stripped.erase(stripped.begin() + 1, stripped.end());
18  }
19 
20  if (stripped[0] == '-') {
21  stripped.erase(stripped.begin() + 1, stripped.end());
22  }
23 
24  return stripped;
25 }
26 
27 //*****************************************************************************************************//
28 
29 namespace Suboption_impl {
30 std::string pull_short(const po::option_description &single_boost_option) {
31  std::string possible_short;
32  possible_short = single_boost_option.canonical_display_name(
33  po::command_line_style::allow_dash_for_short);
34  if (possible_short.size() > 2 || possible_short[0] != '-' ||
35  possible_short[1] == '-') {
36  return "-_";
37  } else {
38  return possible_short;
39  }
40 }
41 
42 std::string pull_long(const po::option_description &single_boost_option) {
43  return single_boost_option.canonical_display_name(
44  po::command_line_style::allow_long);
45 }
46 } // namespace Suboption_impl
47 
48 Suboption::Suboption(const std::string &init_longname, std::string init_short,
49  ARG_TYPE init_expected_types)
50  : m_long(init_longname),
51  m_short(init_short),
52  m_expected_arg(init_expected_types) {
53  _sanity_throw();
54 }
55 
56 Suboption::Suboption(const po::option_description &init_boost_option)
57  : m_long(Suboption_impl::pull_long(init_boost_option)),
58  m_short(Suboption_impl::pull_short(init_boost_option)),
59  m_expected_arg(ArgHandler::determine_type(init_boost_option)) {
60  _sanity_throw();
61 }
62 
64  if (m_long.size() < 3 || m_short.size() != 2) {
65  throw std::runtime_error(
66  "--long option must be at least 3 characters long and -s(hort) must be "
67  "exactly 2!");
68  }
69  if (m_long[0] != '-' || m_long[1] != '-' || m_short[0] != '-') {
70  throw std::runtime_error(
71  "Suboption --long and -s(hort) tags must include leading '-' "
72  "characters!");
73  }
74 }
75 
76 std::string Suboption::long_tag() const { return m_long; }
77 
78 std::string Suboption::short_tag() const { return m_short; }
79 
80 bool Suboption::matches(const std::string &test_tag) const {
81  if (test_tag.size() == 2) {
82  return (test_tag == m_short);
83  }
84 
85  else {
86  return (test_tag == m_long);
87  }
88 
89  throw std::runtime_error(
90  "The impossible has occurred. Stepped through an if/else block and hit "
91  "neither");
92 }
93 
95 
96 //***************************************************************************************************//
97 
98 Option::Option(const std::string &init_tag,
99  const std::vector<Suboption> &init_allowed_subopts)
100  : m_tag(init_tag), m_avail_suboptions(init_allowed_subopts) {}
101 
102 Option::Option(const std::string &init_tag,
103  const po::options_description &init_premade_descs)
104  : m_tag(init_tag) {
105  m_avail_suboptions.reserve(init_premade_descs.options().size());
106 
107  for (auto it = init_premade_descs.options().begin();
108  it != init_premade_descs.options().end(); ++it) {
109  m_avail_suboptions.push_back(Suboption(**it));
110  }
111 }
112 
113 std::string Option::tag() const { return m_tag; }
114 
120 std::vector<std::string> Option::probe_suboptions() const {
121  std::vector<std::string> suboption_tag_list;
122  for (auto it = m_avail_suboptions.begin(); it != m_avail_suboptions.end();
123  ++it) {
124  suboption_tag_list.push_back(it->long_tag());
125  }
126 
127  return suboption_tag_list;
128 }
129 
137 ARG_TYPE Option::probe_argument_type(const std::string &suboption_tag) const {
138  for (auto it = m_avail_suboptions.begin(); it != m_avail_suboptions.end();
139  ++it) {
140  if (it->matches(suboption_tag)) {
141  return it->argument_type();
142  }
143  }
144 
145  return ARG_TYPE::VOID;
146 }
147 
148 bool Option::matches(const std::string &test_tag) const {
149  return (test_tag == m_tag);
150 }
151 
152 //***************************************************************************************************//
153 
154 Engine::Engine(const std::vector<Option> &init_options)
155  : m_avail_options(init_options) {}
156 
162 std::vector<std::string> Engine::probe_options() const {
163  std::vector<std::string> option_list;
164  for (auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
165  option_list.push_back(it->tag());
166  }
167 
168  return option_list;
169 }
170 
178 std::vector<std::string> Engine::probe_suboptions(
179  const std::string &option_tag) const {
180  for (auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
181  if (it->matches(option_tag)) {
182  return it->probe_suboptions();
183  }
184  }
185 
186  // as fallback, show files/dirs
187  return std::vector<std::string>({"BASH_COMP_PATH"});
188 }
189 
197 std::vector<std::string> Engine::probe_arguments(
198  const std::string &option_tag, const std::string &suboption_tag) const {
199  std::vector<std::string> arguments;
200 
201  ARG_TYPE required_arg = _probe_argument_type(option_tag, suboption_tag);
202 
203  switch (required_arg) {
204  case ARG_TYPE::VOID:
205  ArgHandler::void_to_bash(arguments);
206  break;
207 
208  case ARG_TYPE::PATH:
209  ArgHandler::path_to_bash(arguments);
210  break;
211 
212  case ARG_TYPE::COMMAND:
213  ArgHandler::command_to_bash(arguments);
214  break;
215 
216  case ARG_TYPE::SCELNAME:
217  // Add supercell names from PrimClex pointer somehow
218  ArgHandler::scelname_to_bash(arguments);
219  break;
220 
221  case ARG_TYPE::QUERY:
222  ArgHandler::query_to_bash(arguments);
223  break;
224 
225  case ARG_TYPE::OPERATOR:
226  ArgHandler::operator_to_bash(arguments);
227  break;
228 
229  case ARG_TYPE::CONFIGNAME:
231  break;
232 
233  case ARG_TYPE::COORDTYPE:
234  // ArgHandler::coord_mode_to_bash();
235  break;
236 
237  case ARG_TYPE::DBTYPE:
238  ArgHandler::dbtype_to_bash(arguments);
239  break;
240 
241  case ARG_TYPE::ENUMMETHOD:
243  break;
244 
245  case ARG_TYPE::CONFIGTYPE:
247  break;
248 
249  case ARG_TYPE::CALCTYPE:
250  ArgHandler::calctype_to_bash(arguments);
251  break;
252 
253  case ARG_TYPE::BSET:
254  ArgHandler::bset_to_bash(arguments);
255  break;
256 
257  case ARG_TYPE::CLEX:
258  ArgHandler::clex_to_bash(arguments);
259  break;
260 
261  case ARG_TYPE::REF:
262  ArgHandler::ref_to_bash(arguments);
263  break;
264 
265  case ARG_TYPE::ECI:
266  ArgHandler::eci_to_bash(arguments);
267  break;
268 
269  case ARG_TYPE::PROPERTY:
270  ArgHandler::property_to_bash(arguments);
271  break;
272 
273  case ARG_TYPE::INFOMETHOD:
275  break;
276 
277  default:
278  break;
279  }
280 
281  // as fallback, show files/dirs
282  if (!arguments.size()) {
283  arguments.push_back("BASH_COMP_PATH");
284  }
285 
286  return arguments;
287 }
288 
299 ARG_TYPE Engine::_probe_argument_type(const std::string &option_tag,
300  const std::string &suboption_tag) const {
301  for (auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
302  if (it->matches(option_tag)) {
303  return it->probe_argument_type(suboption_tag);
304  }
305  }
306 
307  return ARG_TYPE::VOID;
308 }
309 
310 void Engine::push_back(const Option &new_option) {
311  m_avail_options.push_back(new_option);
312  return;
313 }
314 } // namespace Completer
315 } // namespace CASM
316 
317 #endif
static void ref_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for REF.
Definition: Handlers.cc:215
static void infomethod_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:261
static void void_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:92
static void configtype_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:175
static void eci_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for ECI.
Definition: Handlers.cc:226
static void calctype_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:182
static void operator_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:128
static void clex_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for CLEX.
Definition: Handlers.cc:204
static void property_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:239
static void command_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:99
static void scelname_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:104
static void dbtype_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for DBTYPE.
Definition: Handlers.cc:160
static void bset_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for BSET.
Definition: Handlers.cc:193
static void path_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for PATH.
Definition: Handlers.cc:94
static void enummethod_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:167
static void query_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for QUERY.
Definition: Handlers.cc:140
static void configname_to_bash(std::vector< std::string > &arguments)
Definition: Handlers.cc:116
Engine()
Default constructor so you can push back your own things.
Definition: Complete.hh:157
std::vector< std::string > probe_options() const
Get a list of all available options.
Definition: Complete.cc:162
std::vector< std::string > probe_suboptions(const std::string &option_tag) const
For a particular option, get the available suboptions.
Definition: Complete.cc:178
ARG_TYPE _probe_argument_type(const std::string &option_tag, const std::string &suboption_tag) const
Definition: Complete.cc:299
std::vector< Option > m_avail_options
Definition: Complete.hh:188
std::vector< std::string > probe_arguments(const std::string &option_tag, const std::string &suboption_tag) const
Return the arguments that should be bash completed.
Definition: Complete.cc:197
void push_back(const Option &new_option)
Append a new option to the engine.
Definition: Complete.cc:310
Option(const std::string &init_tag, const po::options_description &init_premade_descs)
Construct with program options.
Definition: Complete.cc:102
ARG_TYPE probe_argument_type(const std::string &suboption_tag) const
Definition: Complete.cc:137
std::vector< Suboption > m_avail_suboptions
List of all the available –suboptions this option has.
Definition: Complete.hh:131
std::string tag() const
Return the identifying name of *this (e.g. "super", "monte", etc)
Definition: Complete.cc:113
std::string m_tag
Name that identifies this casm option (e.g. "monte", "init", etc)
Definition: Complete.hh:127
bool matches(const std::string &test_tag) const
Check if the given string corresponds to the tag of *this.
Definition: Complete.cc:148
std::vector< std::string > probe_suboptions() const
Return what the suboptions (–long format) for *this are.
Definition: Complete.cc:120
std::string short_tag() const
Return short name as char.
Definition: Complete.cc:78
ARG_TYPE argument_type() const
Return the expected types of arguments that follow *this.
Definition: Complete.cc:94
const ARG_TYPE m_expected_arg
Type of arguments expected.
Definition: Complete.hh:69
void _sanity_throw() const
Make sure values make sense.
Definition: Complete.cc:63
const std::string m_long
–long identifier (includes leading "--")
Definition: Complete.hh:63
const std::string m_short
-s(hort) identifyer (includes leading '-')
Definition: Complete.hh:66
Suboption(const std::string &init_longname, std::string init_short, ARG_TYPE init_expected_types)
Explicit construction. Be sure to include "--" and '-' in the tags.
Definition: Complete.cc:48
bool matches(const std::string &test_tag) const
Definition: Complete.cc:80
std::string long_tag() const
Return long name in string format.
Definition: Complete.cc:76
std::string pull_long(const po::option_description &single_boost_option)
Get the –long tag from boost.
Definition: Complete.cc:42
std::string pull_short(const po::option_description &single_boost_option)
Get the -s(hort) tag from boost, or make it "- " if it doesn't exist.
Definition: Complete.cc:30
ArgHandler::ARG_TYPE ARG_TYPE
Definition: Complete.cc:11
std::string strip_argument(const std::string &raw_input)
Definition: Complete.cc:13
Main CASM namespace.
Definition: APICommand.hh:8