From 4795e01d5469cd22dff545dd837897822af7a318 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 19 Oct 2020 11:40:28 -0400 Subject: [PATCH] Support being called from a graphical shell through filename association. This adds a hack to the LAMMPS shell that hides the first argument from the LAMMPS class, if it does not start with a '-' and instead tries to open it as an input file. --- tools/lammps-shell/lammps-shell.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/lammps-shell/lammps-shell.cpp b/tools/lammps-shell/lammps-shell.cpp index d4d3b291b6..b2c8137aba 100644 --- a/tools/lammps-shell/lammps-shell.cpp +++ b/tools/lammps-shell/lammps-shell.cpp @@ -716,6 +716,16 @@ int main(int argc, char **argv) omp_threads = dupstring(std::string("OMP_NUM_THREADS=" + std::to_string(nthreads))); putenv(omp_threads); + // handle the special case where the first argument is not a flag but a file + // this happens for example when using file type associations on Windows. + // in this case we save the pointer and remove it from argv. + char *input_file = nullptr; + if ((argc > 1) && (argv[1][0] != '-')) { + --argc; + input_file = argv[1]; + for (int i = 1; i < argc; ++i) argv[i] = argv[i+1]; + } + lmp = lammps_open_no_mpi(argc, argv, nullptr); if (lmp == nullptr) return 1; @@ -723,9 +733,13 @@ int main(int argc, char **argv) init_commands(); // pre-load an input file that was provided on the command line - for (int i = 0; i < argc; ++i) { - if ((strcmp(argv[i], "-in") == 0) || (strcmp(argv[i], "-i") == 0)) { - lammps_file(lmp, argv[i + 1]); + if (input_file) { + lammps_file(lmp, input_file); + } else { + for (int i = 0; i < argc; ++i) { + if ((strcmp(argv[i], "-in") == 0) || (strcmp(argv[i], "-i") == 0)) { + lammps_file(lmp, argv[i + 1]); + } } }