From 2dc6b57c242456c4982eb204011b7a320f9b141c Mon Sep 17 00:00:00 2001
From: Amandine PERRIN <amandine.perrin@pasteur.fr>
Date: Tue, 20 Oct 2020 10:24:36 +0200
Subject: [PATCH] Move check type of argument (percentage) to utils_argparse.py

---
 PanACoTA/subcommands/corepers.py | 15 ++-------------
 PanACoTA/utils_argparse.py       | 13 +++++++++++++
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/PanACoTA/subcommands/corepers.py b/PanACoTA/subcommands/corepers.py
index d9e97bde..62756a8c 100755
--- a/PanACoTA/subcommands/corepers.py
+++ b/PanACoTA/subcommands/corepers.py
@@ -200,18 +200,7 @@ def build_parser(parser):
         parser to configure in order to extract command-line arguments
     """
     import argparse
-
-    def percentage(param):
-        try:
-            param = float(param)
-        except Exception:
-            msg = "argument -t tol: invalid float value: {}".format(param)
-            raise argparse.ArgumentTypeError(msg)
-        if param < 0 or param > 1:
-            msg = ("The minimum %% of genomes required in a family to be persistent must "
-                   "be in [0, 1]. Invalid value: {}".format(param))
-            raise argparse.ArgumentTypeError(msg)
-        return param
+    from PanACoTA import utils_argparse
 
     # Create command-line parser for all options and arguments to give
     required = parser.add_argument_group('Required arguments')
@@ -221,7 +210,7 @@ def build_parser(parser):
                           help=("Specify the output directory for your core/persistent genome."),
                           default=".")
     optional = parser.add_argument_group('Optional arguments')
-    optional.add_argument("-t", "--tol", dest="tol", default=1, type=percentage,
+    optional.add_argument("-t", "--tol", dest="tol", default=1, type=utils_argparse.percentage,
                           help=("min %% of genomes having at least 1 member in a family to "
                                 "consider the family as persistent (between 0 and 1, "
                                 "default is 1 = 100%% of genomes = Core genome)."
diff --git a/PanACoTA/utils_argparse.py b/PanACoTA/utils_argparse.py
index f553a571..8f07f939 100644
--- a/PanACoTA/utils_argparse.py
+++ b/PanACoTA/utils_argparse.py
@@ -123,3 +123,16 @@ def mash_dist(param):
         msg = f"error: mash distance must be between 0 and 1: invalid value: '{param}'"
         raise argparse.ArgumentTypeError(msg)
     return param
+
+
+def percentage(param):
+        try:
+            param = float(param)
+        except Exception:
+            msg = "argument -t tol: invalid float value: {}".format(param)
+            raise argparse.ArgumentTypeError(msg)
+        if param < 0 or param > 1:
+            msg = ("The minimum %% of genomes required in a family to be persistent must "
+                   "be in [0, 1]. Invalid value: {}".format(param))
+            raise argparse.ArgumentTypeError(msg)
+        return param
-- 
GitLab