From 89be1aba0f7171a20847b8c258681b615283fc2d Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Mon, 20 Jul 2020 12:00:58 +0100
Subject: [PATCH] ENH: new remove_ext script, minor adjustments to imrm

---
 fsl/scripts/imrm.py       | 11 ++++-----
 fsl/scripts/remove_ext.py | 51 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 6 deletions(-)
 create mode 100644 fsl/scripts/remove_ext.py

diff --git a/fsl/scripts/imrm.py b/fsl/scripts/imrm.py
index 2db901882..bd8110332 100644
--- a/fsl/scripts/imrm.py
+++ b/fsl/scripts/imrm.py
@@ -23,7 +23,7 @@ with warnings.catch_warnings():
     import fsl.data.image as fslimage
 
 
-usage = """Usage: {} <list of image names to remove>
+usage = """Usage: imrm <list of image names to remove>
 NB: filenames can be basenames or not
 """.strip()
 
@@ -36,14 +36,13 @@ def main(argv=None):
     """Removes all images which are specified on the command line. """
 
     if argv is None:
-        argv = sys.argv
+        argv = sys.argv[1:]
 
-    if len(argv) < 2:
-        exe = op.abspath(argv[0])
-        print(usage.format(exe))
+    if len(argv) < 1:
+        print(usage)
         return 1
 
-    prefixes = [fslpath.removeExt(p, ALLOWED_EXTENSIONS) for p in argv[1:]]
+    prefixes = [fslpath.removeExt(p, ALLOWED_EXTENSIONS) for p in argv]
 
     for prefix, ext in it.product(prefixes, ALLOWED_EXTENSIONS):
 
diff --git a/fsl/scripts/remove_ext.py b/fsl/scripts/remove_ext.py
new file mode 100644
index 000000000..fc21094ff
--- /dev/null
+++ b/fsl/scripts/remove_ext.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+#
+# remove_ext.py - Remove file extensions from NIFTI image paths
+#
+# Author: Paul McCarthy <pauldmccarthy@gmail.com>
+#
+
+
+import sys
+import warnings
+
+import fsl.utils.path as fslpath
+
+# See atlasq.py for explanation
+with warnings.catch_warnings():
+    warnings.filterwarnings("ignore", category=FutureWarning)
+    import fsl.data.image as fslimage
+
+
+usage = """Usage: remove_ext <list of image paths to remove extension from>
+""".strip()
+
+
+ALLOWED_EXTENSIONS = fslimage.ALLOWED_EXTENSIONS + ['.mnc', '.mnc.gz']
+"""List of file extensions that are removed by ``remove_ext``. """
+
+
+def main(argv=None):
+    """Removes file extensions from all paths which are specified on the
+    command line.
+    """
+
+    if argv is None:
+        argv = sys.argv[1:]
+
+    if len(argv) < 1:
+        print(usage)
+        return 1
+
+    removed = []
+
+    for path in argv:
+        removed.append(fslpath.removeExt(path, ALLOWED_EXTENSIONS))
+
+    print(' '.join(removed))
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())
-- 
GitLab