From 67dc4e05c755a1865df4f0ad56c3a5bc14e77a14 Mon Sep 17 00:00:00 2001
From: Amandine PERRIN <amandine.perrin@pasteur.fr>
Date: Thu, 15 Oct 2020 16:50:11 +0200
Subject: [PATCH] cutN must be a positive number

---
 PanACoTA/subcommands/annotate.py |  2 +-
 PanACoTA/subcommands/prepare.py  |  2 +-
 PanACoTA/utils_argparse.py       | 12 ++++++++++++
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/PanACoTA/subcommands/annotate.py b/PanACoTA/subcommands/annotate.py
index 7ea38012..616c2a1c 100755
--- a/PanACoTA/subcommands/annotate.py
+++ b/PanACoTA/subcommands/annotate.py
@@ -479,7 +479,7 @@ def build_parser(parser):
     optional.add_argument("--nbcont", dest="nbcont", type=utils_argparse.cont_num, default=999,
                           help=("Maximum number of contigs allowed to keep a genome. "
                                 "Default is 999."))
-    optional.add_argument("--cutn", dest="cutn", type=int, default=5,
+    optional.add_argument("--cutn", dest="cutn", type=utils_argparse.positive_int, default=5,
                           help=("By default, each genome will be cut into new contigs when "
                                 "at least 5 'N' in a row are found in its sequence. "
                                 "If you don't want to "
diff --git a/PanACoTA/subcommands/prepare.py b/PanACoTA/subcommands/prepare.py
index 6a9c7659..ebb00dae 100644
--- a/PanACoTA/subcommands/prepare.py
+++ b/PanACoTA/subcommands/prepare.py
@@ -312,7 +312,7 @@ def build_parser(parser):
                                 "By default, it will be saved in your "
                                 "out_dir/tmp_files.")
                           )
-    general.add_argument("--cutn", dest="cutn", type=int, default=5,
+    general.add_argument("--cutn", dest="cutn", type=utils_argparse.positive_int, default=5,
                           help=("By default, each genome will be cut into new contigs when "
                                 "at least 5 'N' in a row are found in its sequence. "
                                 "If you don't want to "
diff --git a/PanACoTA/utils_argparse.py b/PanACoTA/utils_argparse.py
index f5bb49bb..5651d00a 100644
--- a/PanACoTA/utils_argparse.py
+++ b/PanACoTA/utils_argparse.py
@@ -99,3 +99,15 @@ def thread_num(param):
     elif param == 0:
         return nb_cpu
     return param
+
+
+def positive_int(param):
+    try:
+        param = int(param)
+    except ValueError:
+        msg = f"error: argument --cutn: invalid int value: '{param}'"
+        raise argparse.ArgumentTypeError(msg)
+    if param < 0:
+        msg = f"error: argument --cutn must be a positive integer: invalid int value: '{param}'"
+        raise argparse.ArgumentTypeError(msg)
+    return param
-- 
GitLab