Serac  0.1
Serac is an implicit thermal strucural mechanics simulation code.
cli.cpp
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Serac Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
8 
9 #include <cstdlib>
10 #include <utility>
11 #include <vector>
12 
13 #include "axom/CLI11.hpp"
14 
17 
18 namespace serac::cli {
19 
20 //------- Command Line Interface -------
21 
22 std::unordered_map<std::string, std::string> defineAndParse(int argc, char* argv[], std::string app_description)
23 {
24  // NOTE: When adding/removing command line options remember to update the following places as well:
25  // src/docs/sphinx/user_guide/command_line_options.rst
26  // serac::cli::printGiven() (in this file)
27 
28  // specify all input arguments
29  axom::CLI::App app{app_description};
30  std::string input_file_path;
31  app.add_option("-i, --input-file", input_file_path, "Input file to use")->check(axom::CLI::ExistingFile);
32  int restart_cycle;
33  auto restart_opt =
34  app.add_option("-c, --restart-cycle", restart_cycle, "Cycle to restart from")->check(axom::CLI::PositiveNumber);
35  bool create_input_file_docs{false};
36  app.add_flag("-d, --create-input-file-docs", create_input_file_docs,
37  "Writes Sphinx documentation for input file, then exits");
38  std::string output_directory;
39  app.add_option("-o, --output-directory", output_directory, "Directory to put outputted files");
40  bool enable_paraview{false};
41  app.add_flag("-p, --paraview", enable_paraview, "Enable ParaView output");
42  bool print_unused{false};
43  app.add_flag("-u, --print-unused", print_unused, "Prints unused entries in input file, then exits");
44  bool version{false};
45  app.add_flag("-v, --version", version, "Print version and provenance information, then exits");
46 
47  // Parse the arguments and check if they are good
48  try {
49  app.parse(argc, argv);
50  } catch (const axom::CLI::ParseError& e) {
51  serac::logger::flush();
52  if (e.get_name() == "CallForHelp") {
53  auto msg = app.help();
54  SLIC_INFO_ROOT(msg);
55  exit(0);
56  } else {
57  auto err_msg = axom::CLI::FailureMessage::simple(&app, e);
58  SLIC_ERROR_ROOT(err_msg);
59  }
60  }
61 
62  // Store found values and set defaults if not set above
63  std::unordered_map<std::string, std::string> cli_opts;
64  if (version) {
65  // If version is on the command line ignore all others and do not require anything
66  cli_opts.insert({"version", {}});
67  } else {
68  if (input_file_path.empty()) {
69  SLIC_ERROR_ROOT("No input file given. Use '--help' for command line options.");
70  }
71 
72  cli_opts.insert({std::string("input-file"), input_file_path});
73  // If a restart cycle was specified
74  if (restart_opt->count() > 0) {
75  cli_opts["restart-cycle"] = std::to_string(restart_cycle);
76  }
77  if (create_input_file_docs) {
78  cli_opts.insert({"create-input-file-docs", {}});
79  }
80  if (print_unused) {
81  cli_opts.insert({"print-unused", {}});
82  }
83  if (output_directory == "") {
84  // if given by user use that otherwise use input file's basename minus extension
85  output_directory = serac::input::getInputFileName(input_file_path);
86  }
87  cli_opts.insert({"output-directory", output_directory});
88  if (enable_paraview) {
89  cli_opts.insert({"paraview", {}});
90  cli_opts.insert({"paraview-directory", output_directory + "_paraview"});
91  }
92  }
93 
94  return cli_opts;
95 }
96 
97 namespace detail {
98 
99 std::string cliValueToString(std::string value) { return value; }
100 
101 std::string cliValueToString(bool value) { return value ? "true" : "false"; }
102 
103 std::string cliValueToString(int value) { return std::to_string(value); }
104 
105 } // namespace detail
106 
107 void printGiven(std::unordered_map<std::string, std::string>& cli_opts)
108 {
109  // Add header
110  std::string optsMsg = axom::fmt::format("\n{:*^80}\n", "Command Line Options");
111 
112  // Create options map
113  // clang-format off
114  std::vector<std::pair<std::string, std::string>> opts_output_map{
115  {"create-input-file-docs", "Create Input File Docs"},
116  {"input-file", "Input File"},
117  {"output-directory", "Output Directory"},
118  {"paraview", "Enable ParaView output"},
119  {"restart-cycle", "Restart Cycle"},
120  {"version", "Print version"}};
121  // clang-format on
122 
123  // Add options to string
124  for (auto output_pair : opts_output_map) {
125  auto search = cli_opts.find(output_pair.first);
126  if (search != cli_opts.end()) {
127  optsMsg += axom::fmt::format("{0}: {1}\n", output_pair.second, detail::cliValueToString(search->second));
128  }
129  }
130 
131  // Add footer
132  optsMsg += axom::fmt::format("{:*^80}\n", "*");
133 
134  SLIC_INFO_ROOT(optsMsg);
135  serac::logger::flush();
136 }
137 
138 } // namespace serac::cli
This file contains the all the necessary functions and macros required for interacting with the comma...
This file contains the all the necessary functions for reading input files.
This file contains the all the necessary functions and macros required for logging as well as a helpe...
Command line functionality.
Definition: cli.cpp:18
std::unordered_map< std::string, std::string > defineAndParse(int argc, char *argv[], std::string app_description)
Defines command line options and parses the found values.
Definition: cli.cpp:22
void printGiven(std::unordered_map< std::string, std::string > &cli_opts)
Prints all given command line options to the screen.
Definition: cli.cpp:107
std::string getInputFileName(const std::string &file_path)
Returns the name of the input file (base name with file extension removed).
Definition: input.cpp:82
std::string version(bool add_SHA)
Returns a string for the version of Serac.
Definition: about.cpp:214