CASM  1.1.0
A Clusters Approach to Statistical Mechanics
rm.cc
Go to the documentation of this file.
1 #include "casm/app/rm.hh"
2 
4 #include "casm/casm_io/Log.hh"
6 #include "casm/clex/PrimClex.hh"
9 
10 // need to add specializations here
13 
14 namespace CASM {
15 
16 namespace Completer {
18 
19 bool RmOption::force() const { return vm().count("force"); }
20 
21 bool RmOption::data() const { return vm().count("data"); }
22 
28 
29  m_desc.add_options()("data,d", "Remove calculation data only.")(
30  "force,f",
31  "Force remove including data and dependent objects (for --type=scel).");
32 
34 
35  return;
36 }
37 } // namespace Completer
38 
39 /*
40  namespace DB {
41 
42  template<>
43  class MakeInterfaceData<RmCommand> {
44 
45  template<typename DataObject>
46  static InterfaceData<DataObject> eval(
47  const RmCommand& cmd,
48  InterfaceData<DataObject>& data) {
49 
50  // set 'selected' column
51  if(cmd.in_project() && cmd.vm.count("selection")) {
52  data.m_sel.resize(1);
53  data.m_sel[0].reset(
54  new Selection<DataObject>(
55  cmd.primclex().db<DataObject>(),
56  cmd.opt().selection_path()));
57  data.m_ss << cmd.opt().selection_path();
58  }
59  }
60 
61  };
62 
63  }
64 */
65 
66 // -- RmCommandImplBase --------------------------------------------
67 
70  public:
71  RmCommandImplBase(const RmCommand &cmd);
72 
73  virtual ~RmCommandImplBase() {}
74 
75  virtual int help() const;
76 
77  virtual int desc() const;
78 
79  virtual int run() const;
80 
81  protected:
82  const RmCommand &m_cmd;
83 };
84 
86 
88  log() << std::endl << m_cmd.opt().desc() << std::endl;
91  return 0;
92 }
93 
94 int RmCommandImplBase::desc() const { // -- custom --
95  help();
96 
97  log() << "Erase objects specified by --names or --selection, and data if "
98  "applicable. \n\n"
99 
100  "Calculated structures are mapped to the closest matching "
101  "configuration \n"
102  "consistent with the primitive crystal structure. \n\n"
103 
104  "For complete update options description for a particular config "
105  "type, use\n"
106  "--desc along with '--type <typename>'.\n\n";
107 
108  return 0;
109 }
110 
112  err_log() << "ERROR: No --type\n";
114  return ERR_INVALID_ARG;
115 }
116 
117 // -- RmCommandImpl -----------------
118 
125 template <typename DataObject>
127  public:
129 
130  int help() const override;
131 
132  int desc() const override;
133 
134  int run() const override;
135 };
136 
137 template <typename DataObject>
139  log()
140  << std::endl
141  << m_cmd.opt().desc() << std::endl
142  << "For complete options description for a particular config type, use:\n"
143  << " casm " << RmCommand::name << " --desc --type "
144  << traits<DataObject>::short_name << "\n\n";
145  return 0;
146 }
147 
148 template <typename DataObject>
150  log() << DB::Remove<DataObject>::desc << std::endl;
151  return 0;
152 }
153 
154 template <typename DataObject>
156  return DB::Remove<DataObject>::run(m_cmd.primclex(), m_cmd.opt());
157 }
158 
159 // -- class RmCommand ----------------------------------------------------
160 
161 const std::string RmCommand::name = "rm";
162 
164  : APICommand<Completer::RmOption>(_args, _opt) {}
165 
167 
169  if (!in_project()) {
170  err_log().error("No casm project found");
171  err_log() << std::endl;
172  return ERR_NO_PROJ;
173  }
174 
175  return 0;
176 }
177 
178 int RmCommand::help() const { return impl().help(); }
179 
180 int RmCommand::desc() const { return impl().desc(); }
181 
182 int RmCommand::run() const { return impl().run(); }
183 
185  if (!m_impl) {
186  if (in_project()) {
187  if (DB::types_short().count(opt().db_type())) {
188  DB::for_type_short(opt().db_type(),
190  } else {
191  std::stringstream msg;
192  msg << "--type " << opt().db_type() << " is not allowed for 'casm "
193  << name << "'.";
194  print_names(err_log());
195  throw CASM::runtime_error(msg.str(), ERR_INVALID_ARG);
196  }
197  } else {
198  m_impl = notstd::make_unique<RmCommandImplBase>(*this);
199  }
200  }
201  return *m_impl;
202 }
203 
204 void RmCommand::print_names(std::ostream &sout) const {
205  sout << "The allowed types are:\n";
206 
207  for (const auto &db_type : opt().db_type_opts()) {
208  sout << " " << db_type << std::endl;
209  }
210 }
211 
212 void RmCommand::print_config_names(std::ostream &sout) const {
213  sout << "The allowed types with --data option are:\n";
214 
215  for (const auto &db_type : opt().db_type_opts()) {
216  if (DB::config_types_short().count(db_type)) {
217  sout << " " << db_type << std::endl;
218  }
219  }
220 }
221 
222 } // namespace CASM
#define ERR_NO_PROJ
Definition: errors.hh:13
#define ERR_INVALID_ARG
Definition: errors.hh:7
bool in_project() const
Definition: APICommand.cc:15
const OptionType & opt() const
Definition: APICommand.hh:61
int count(std::string s) const
Definition: APICommand.hh:57
void add_names_suboption()
Add a –names suboption.
Definition: Handlers.cc:706
const po::options_description & desc()
Get the program options, filled with the initialized values.
Definition: Handlers.cc:321
po::variables_map & vm()
Get the variables map.
Definition: Handlers.cc:310
void add_help_suboption()
Add a plain –help and –desc suboptions.
Definition: Handlers.cc:561
po::options_description m_desc
Definition: Handlers.hh:260
void add_db_type_suboption(std::string _default, std::set< std::string > _configtype_opts)
Definition: Handlers.cc:536
void add_dry_run_suboption(std::string msg=default_dry_run_msg())
Definition: Handlers.cc:749
void add_selection_suboption(const fs::path &_default="MASTER")
Add –selection suboption (defaults to MASTER)
Definition: Handlers.cc:407
void initialize() override
Fill in the options descriptions accordingly.
Definition: rm.cc:23
bool data() const
Definition: rm.cc:21
bool force() const
Definition: rm.cc:19
std::string db_type() const
Definition: Handlers.cc:555
static int run(const PrimClex &, const Completer::RmOption &opt)
Definition: Remove_impl.hh:130
void error(const std::string &what)
Definition: Log.hh:129
RmCommandImplBase & impl() const
Definition: rm.cc:184
int desc() const override
Definition: rm.cc:180
int run() const override
Definition: rm.cc:182
int help() const override
Definition: rm.cc:178
static const std::string name
Definition: rm.hh:46
void print_config_names(std::ostream &sout) const
Definition: rm.cc:212
void print_names(std::ostream &sout) const
Definition: rm.cc:204
~RmCommand()
Definition: rm.cc:166
RmCommand(const CommandArgs &_args, Completer::RmOption &_opt)
Definition: rm.cc:163
int vm_count_check() const override
Definition: rm.cc:168
std::unique_ptr< RmCommandImplBase > m_impl
Definition: rm.hh:69
Defaults used if DataObject type doesn't matter or not given.
Definition: rm.cc:69
virtual int help() const
Definition: rm.cc:87
virtual int run() const
Definition: rm.cc:111
virtual int desc() const
Definition: rm.cc:94
const RmCommand & m_cmd
Definition: rm.cc:82
virtual ~RmCommandImplBase()
Definition: rm.cc:73
RmCommandImplBase(const RmCommand &cmd)
Definition: rm.cc:85
int help() const override
Definition: rm.cc:138
int desc() const override
Definition: rm.cc:149
int run() const override
Definition: rm.cc:155
RmCommandImpl(const RmCommand &cmd)
Definition: rm.cc:128
void for_type_short(std::string short_name, F f)
const std::set< std::string > & types_short()
std::set of all QueryTraits<DataObject>::short_name
const std::set< std::string > & config_types_short()
std::set of all QueryTraits<ConfigType>::short_name
Main CASM namespace.
Definition: APICommand.hh:8
Log & log()
Definition: Log.hh:424
Log & err_log()
Definition: Log.hh:426
Data structure holding basic CASM command info.