diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index 9c6ef67945..eeb0096b81 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -211,6 +211,9 @@ Convenience functions .. doxygenfunction:: logmesg(LAMMPS *lmp, const std::string &mesg) :project: progguide +.. doxygenfunction:: missing_cmd_args + :project: progguide + .. doxygenfunction:: flush_buffers(LAMMPS *lmp) :project: progguide diff --git a/src/utils.cpp b/src/utils.cpp index a8e1b14104..a58dcb6b37 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -22,8 +22,8 @@ #include "memory.h" #include "modify.h" #include "text_file_reader.h" -#include "update.h" #include "universe.h" +#include "update.h" #include #include @@ -120,6 +120,12 @@ std::string utils::strfind(const std::string &text, const std::string &pattern) return ""; } +void utils::missing_cmd_args(const std::string &file, int line, const std::string &cmd, + Error *error) +{ + if (error) error->all(file, line, "Illegal {} command: missing argument(s)", cmd); +} + /* specialization for the case of just a single string argument */ void utils::logmesg(LAMMPS *lmp, const std::string &mesg) @@ -141,7 +147,7 @@ void utils::flush_buffers(LAMMPS *lmp) { if (lmp->screen) fflush(lmp->screen); if (lmp->logfile) fflush(lmp->logfile); - if (lmp->universe->uscreen) fflush(lmp->universe->uscreen); + if (lmp->universe->uscreen) fflush(lmp->universe->uscreen); if (lmp->universe->ulogfile) fflush(lmp->universe->ulogfile); } @@ -566,14 +572,14 @@ void utils::bounds(const char *file, int line, const std::string &str, error->all(file, line, fmt::format("Invalid range string: {}", str)); if (nlo < nmin) - error->all(file, line, fmt::format("Numeric index {} is out of bounds " - "({}-{})", nlo, nmin, nmax)); + error->all(file, line, fmt::format("Numeric index {} is out of bounds ({}-{})", + nlo, nmin, nmax)); else if (nhi > nmax) - error->all(file, line, fmt::format("Numeric index {} is out of bounds " - "({}-{})", nhi, nmin, nmax)); + error->all(file, line, fmt::format("Numeric index {} is out of bounds ({}-{})", + nhi, nmin, nmax)); else if (nlo > nhi) - error->all(file, line, fmt::format("Numeric index {} is out of bounds " - "({}-{})", nlo, nmin, nhi)); + error->all(file, line, fmt::format("Numeric index {} is out of bounds ({}-{})", + nlo, nmin, nhi)); } } @@ -805,7 +811,7 @@ std::string utils::star_subst(const std::string &name, bigint step, int pad) auto star = name.find('*'); if (star == std::string::npos) return name; - return fmt::format("{}{:0{}}{}",name.substr(0,star),step,pad,name.substr(star+1)); + return fmt::format("{}{:0{}}{}", name.substr(0, star), step, pad, name.substr(star + 1)); } /* ---------------------------------------------------------------------- diff --git a/src/utils.h b/src/utils.h index 86f31508e7..6aa28dd436 100644 --- a/src/utils.h +++ b/src/utils.h @@ -21,7 +21,7 @@ #include -#include // IWYU pragma: export +#include // IWYU pragma: export namespace LAMMPS_NS { @@ -47,6 +47,18 @@ namespace utils { std::string strfind(const std::string &text, const std::string &pattern); + /*! Print error message about missing arguments for command + * + * This function simplifies the repetitive reporting missing arguments to a command. + * + * \param file name of source file for error message + * \param line line number in source file for error message + * \param cmd name of the failing command + * \param error pointer to Error class instance (for abort) or nullptr */ + + [[noreturn]] void missing_cmd_args(const std::string &file, int line, const std::string &cmd, + Error *error); + /* Internal function handling the argument list for logmesg(). */ void fmtargs_logmesg(LAMMPS *lmp, fmt::string_view format, fmt::format_args args);