CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Complete.cc
Go to the documentation of this file.
1 #ifndef COMPLETE_CC
2 #define COMPLETE_CC
3 
7 
8 namespace CASM {
9  namespace Completer {
11 
12  std::string strip_argument(const std::string &raw_input) {
13  std::string stripped = raw_input;
14 
15  if(stripped[0] == '-') {
16  stripped.erase(stripped.begin() + 1, stripped.end());
17  }
18 
19  if(stripped[0] == '-') {
20  stripped.erase(stripped.begin() + 1, stripped.end());
21  }
22 
23  return stripped;
24  }
25 
26  //*****************************************************************************************************//
27 
28  namespace Suboption_impl {
29  std::string pull_short(const po::option_description &single_boost_option) {
30  std::string possible_short;
31  possible_short = single_boost_option.canonical_display_name(po::command_line_style::allow_dash_for_short);
32  if(possible_short.size() > 2 || possible_short[0] != '-' || possible_short[1] == '-') {
33  return "-_";
34  }
35  else {
36  return possible_short;
37  }
38  }
39 
40  std::string pull_long(const po::option_description &single_boost_option) {
41  return single_boost_option.canonical_display_name(po::command_line_style::allow_long);
42  }
43  }
44 
45  Suboption::Suboption(const std::string &init_longname, std::string init_short, ARG_TYPE init_expected_types):
46  m_long(init_longname),
47  m_short(init_short),
48  m_expected_arg(init_expected_types) {
49  _sanity_throw();
50  }
51 
52 
53  Suboption::Suboption(const po::option_description &init_boost_option):
54  m_long(Suboption_impl::pull_long(init_boost_option)),
55  m_short(Suboption_impl::pull_short(init_boost_option)),
56  m_expected_arg(ArgHandler::determine_type(init_boost_option)) {
57  _sanity_throw();
58  }
59 
60  void Suboption::_sanity_throw() const {
61  if(m_long.size() < 3 || m_short.size() != 2) {
62  throw std::runtime_error("--long option must be at least 3 characters long and -s(hort) must be exactly 2!");
63  }
64  if(m_long[0] != '-' || m_long[1] != '-' || m_short[0] != '-') {
65  throw std::runtime_error("Suboption --long and -s(hort) tags must include leading '-' characters!");
66  }
67  }
68 
69  std::string Suboption::long_tag() const {
70  return m_long;
71  }
72 
73  std::string Suboption::short_tag() const {
74  return m_short;
75  }
76 
77  bool Suboption::matches(const std::string &test_tag) const {
78  if(test_tag.size() == 2) {
79  return (test_tag == m_short);
80  }
81 
82  else {
83  return (test_tag == m_long);
84  }
85 
86  throw std::runtime_error("The impossible has occurred. Stepped through an if/else block and hit neither");
87  }
88 
89  ARG_TYPE Suboption::argument_type() const {
90  return m_expected_arg;
91  }
92 
93  //***************************************************************************************************//
94 
95  Option::Option(const std::string &init_tag, const std::vector<Suboption> &init_allowed_subopts):
96  m_tag(init_tag),
97  m_avail_suboptions(init_allowed_subopts) {
98  }
99 
100  Option::Option(const std::string &init_tag, const po::options_description &init_premade_descs):
101  m_tag(init_tag) {
102  m_avail_suboptions.reserve(init_premade_descs.options().size());
103 
104  for(auto it = init_premade_descs.options().begin(); it != init_premade_descs.options().end(); ++it) {
105  m_avail_suboptions.push_back(Suboption(**it));
106  }
107  }
108 
109  std::string Option::tag() const {
110  return m_tag;
111  }
112 
118  std::vector<std::string> Option::probe_suboptions() const {
119  std::vector<std::string> suboption_tag_list;
120  for(auto it = m_avail_suboptions.begin(); it != m_avail_suboptions.end(); ++it) {
121  suboption_tag_list.push_back(it->long_tag());
122  }
123 
124  return suboption_tag_list;
125  }
126 
134  ARG_TYPE Option::probe_argument_type(const std::string &suboption_tag) const {
135  for(auto it = m_avail_suboptions.begin(); it != m_avail_suboptions.end(); ++it) {
136  if(it->matches(suboption_tag)) {
137  return it->argument_type();
138  }
139  }
140 
141  return ARG_TYPE::VOID;
142  }
143 
144  bool Option::matches(const std::string &test_tag) const {
145  return (test_tag == m_tag);
146  }
147 
148  //***************************************************************************************************//
149 
150  Engine::Engine(const std::vector<Option> &init_options):
151  m_avail_options(init_options) {
152  }
153 
159  std::vector<std::string> Engine::probe_options() const {
160  std::vector<std::string> option_list;
161  for(auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
162  option_list.push_back(it->tag());
163  }
164 
165  return option_list;
166  }
167 
174  std::vector<std::string> Engine::probe_suboptions(const std::string &option_tag) const {
175  for(auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
176  if(it->matches(option_tag)) {
177  return it->probe_suboptions();
178  }
179  }
180 
181  // as fallback, show files/dirs
182  return std::vector<std::string>({"BASH_COMP_PATH"});
183  }
184 
191  std::vector<std::string> Engine::probe_arguments(const std::string &option_tag, const std::string &suboption_tag) const {
192  std::vector<std::string> arguments;
193 
194  ARG_TYPE required_arg = _probe_argument_type(option_tag, suboption_tag);
195 
196  switch(required_arg) {
197  case ARG_TYPE::VOID:
198  ArgHandler::void_to_bash(arguments);
199  break;
200 
201  case ARG_TYPE::PATH:
202  ArgHandler::path_to_bash(arguments);
203  break;
204 
205  case ARG_TYPE::COMMAND:
206  ArgHandler::command_to_bash(arguments);
207  break;
208 
209  case ARG_TYPE::SCELNAME:
210  //Add supercell names from PrimClex pointer somehow
211  break;
212 
213  case ARG_TYPE::QUERY:
214  ArgHandler::query_to_bash(arguments);
215  break;
216 
217  case ARG_TYPE::OPERATOR:
218  ArgHandler::operator_to_bash(arguments);
219  break;
220 
221  case ARG_TYPE::CONFIGNAME:
222  //ArgHandler::configname_to_bash();
223  break;
224 
225  case ARG_TYPE::COORDTYPE:
226  //ArgHandler::coord_mode_to_bash();
227  break;
228 
229  default:
230  break;
231  }
232 
233  // as fallback, show files/dirs
234  if(!arguments.size()) {
235  arguments.push_back("BASH_COMP_PATH");
236  }
237 
238  return arguments;
239  }
240 
241 
242 
253  ARG_TYPE Engine::_probe_argument_type(const std::string &option_tag, const std::string &suboption_tag) const {
254  for(auto it = m_avail_options.begin(); it != m_avail_options.end(); ++it) {
255  if(it->matches(option_tag)) {
256  return it->probe_argument_type(suboption_tag);
257  }
258  }
259 
260  return ARG_TYPE::VOID;
261  }
262 
263  void Engine::push_back(const Option &new_option) {
264  m_avail_options.push_back(new_option);
265  return;
266  }
267  }
268 }
269 
270 #endif
static void operator_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for OPERATOR.
Definition: Handlers.cc:97
std::vector< std::string > probe_suboptions(const std::string &option_tag) const
For a particular option, get the available suboptions.
Definition: Complete.cc:174
std::vector< Suboption > m_avail_suboptions
List of all the available –suboptions this option has.
Definition: Complete.hh:125
ARG_TYPE probe_argument_type(const std::string &suboption_tag) const
For a particular –suboption, get what kind of arguments are expected. suboption_tag should be pre-st...
Definition: Complete.cc:134
ArgHandler::ARG_TYPE ARG_TYPE
Definition: Complete.cc:10
Option(const std::string &init_tag, const po::options_description &init_premade_descs)
Construct with program options.
Definition: Complete.cc:100
bool matches(const std::string &test_tag) const
Check if the given string corresponds to the tag of *this.
Definition: Complete.cc:144
const std::string m_long
–long identifier (includes leading "--")
Definition: Complete.hh:59
std::string tag() const
Return the identifying name of *this (e.g. "super", "monte", etc)
Definition: Complete.cc:109
const std::string m_short
-s(hort) identifyer (includes leading '-')
Definition: Complete.hh:62
Main CASM namespace.
Definition: complete.cpp:8
static void query_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for QUERY.
Definition: Handlers.cc:86
std::string short_tag() const
Return short name as char.
Definition: Complete.cc:73
std::string strip_argument(const std::string &raw_input)
Remove "--" or "-" from beginning of string if it exists, and return as new string.
Definition: Complete.cc:12
bool matches(const std::string &test_tag) const
See if a provided string matches either the –long or -s(hort) tags. Expects leading '-' characters...
Definition: Complete.cc:77
std::string long_tag() const
Return long name in string format.
Definition: Complete.cc:69
void _sanity_throw() const
Make sure values make sense.
Definition: Complete.cc:60
std::string pull_long(const po::option_description &single_boost_option)
Get the –long tag from boost.
Definition: Complete.cc:40
static void void_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for VOID (i.e. do nothing) ...
Definition: Handlers.cc:68
static void path_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for PATH.
Definition: Handlers.cc:72
ARG_TYPE argument_type() const
Return the expected types of arguments that follow *this.
Definition: Complete.cc:89
const ARG_TYPE m_expected_arg
Type of arguments expected.
Definition: Complete.hh:65
std::vector< std::string > probe_options() const
Construct by passing boost program options (eventually preferred so that it can update itself) ...
Definition: Complete.cc:159
std::vector< std::string > probe_suboptions() const
Return what the suboptions (–long format) for *this are.
Definition: Complete.cc:118
void push_back(const Option &new_option)
Guess what should be returned based on the current word (probably not gonna make it that smart) ...
Definition: Complete.cc:263
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:45
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:29
std::string m_tag
Name that identifies this casm option (e.g. "monte", "init", etc)
Definition: Complete.hh:122
static void command_to_bash(std::vector< std::string > &arguments)
Fill the output strings with bash completion appropriate values for COMMAND.
Definition: Handlers.cc:77
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:191
std::vector< Option > m_avail_options
List of all the available (presumably casm) options (e.g. "monte", "init", etc)
Definition: Complete.hh:179
Engine()
Default constructor so you can push back your own things.
Definition: Complete.hh:153
ARG_TYPE _probe_argument_type(const std::string &option_tag, const std::string &suboption_tag) const
For a particular option with suboption, get what kind of arguments are expected.
Definition: Complete.cc:253