CASM  1.1.0
A Clusters Approach to Statistical Mechanics
APICommand_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_APICommand_impl
2 #define CASM_APICommand_impl
3 
4 #include "casm/app/APICommand.hh"
6 #include "casm/casm_io/Log.hh"
7 #include "casm/clex/PrimClex.hh"
8 
9 namespace CASM {
10 
12 template <typename CommandType>
13 std::unique_ptr<CommandType> make_api_command(
14  CommandArgs const &args, typename CommandType::OptionType &opt) {
15  po::store(po::parse_command_line(args.argc(), args.argv(), opt.desc()),
16  opt.vm());
17  po::notify(opt.vm());
18  return notstd::make_unique<CommandType>(args, opt);
19 }
20 
26 template <typename CommandType>
27 std::unique_ptr<CommandType> make_api_command(
28  std::string cli_str, PrimClex &primclex,
29  typename CommandType::OptionType &opt) {
30  fs::path root;
31  if (primclex.has_dir()) {
32  root = primclex.dir().root_dir();
33  }
34  CommandArgs args{cli_str, &primclex, root};
35  return make_api_command<CommandType>(args, opt);
36 }
37 
39 template <typename CommandType>
40 int run_api_command(const CommandArgs &args) {
41  typename CommandType::OptionType opt;
42 
43  try {
44  po::store(po::parse_command_line(args.argc(), args.argv(), opt.desc()),
45  opt.vm());
46 
47  // gets default values
48  po::notify(opt.vm());
49 
50  CommandType f{args, opt};
51 
52  int code;
53 
54  // help
55  if (f.vm().count("help")) {
56  code = f.help();
57  }
58  // extended command descriptions
59  else if (f.vm().count("desc")) {
60  code = f.desc();
61  }
62  // checks that can be made without getting defaults
63  else {
64  code = f.vm_count_check();
65  if (code) {
66  f.help();
67  } else {
68  code = f.run();
69  }
70  }
71 
72  return code;
73  } catch (po::error &e) {
74  err_log() << "ERROR: " << e.what() << std::endl << std::endl;
75  return ERR_INVALID_ARG;
76  } catch (CASM::runtime_error &e) {
77  err_log() << "ERROR: " << e.what() << std::endl << std::endl;
78  return e.code();
79  } catch (std::exception &e) {
80  err_log() << "ERROR: " << e.what() << std::endl << std::endl;
81  return ERR_UNKNOWN;
82  }
83 }
84 
85 } // namespace CASM
86 
87 #endif
#define ERR_UNKNOWN
Definition: errors.hh:10
#define ERR_INVALID_ARG
Definition: errors.hh:7
int argc() const
Definition: CLIParse.hh:20
char ** argv() const
Definition: CLIParse.hh:22
fs::path root_dir() const
Return casm project directory path.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
const DirectoryStructure & dir() const
Access DirectoryStructure object. Throw if not set.
Definition: PrimClex.cc:230
bool has_dir() const
Check if DirectoryStructure exists.
Definition: PrimClex.cc:228
Main CASM namespace.
Definition: APICommand.hh:8
std::unique_ptr< CommandType > make_api_command(CommandArgs const &args, typename CommandType::OptionType &opt)
Parse command line options and make API command. Throws for parsing errors.
int run_api_command(const CommandArgs &args)
Standardizes how 'casm X' api commands are executed and implemented.
Log & err_log()
Definition: Log.hh:426
PrimClex * primclex
Definition: settings.cc:135
Data structure holding basic CASM command info.