From eb218f5c55a911b21d433c0fa5571d9cacbe5d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Mon, 13 Jan 2025 16:04:58 +0100 Subject: [PATCH] STYLE: Use a dedicated function to parse script input arguments (#79) Use a dedicated function to parse script input arguments: improves readaibility, and reduces the complexity of the `main` method. --- scripts/tract_math | 15 +++++++++++---- scripts/tract_querier | 8 +++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/tract_math b/scripts/tract_math index a7c0ec5..fb08834 100755 --- a/scripts/tract_math +++ b/scripts/tract_math @@ -10,6 +10,8 @@ import warnings import os, sys # sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +from tract_querier.tract_math import operations + def tract_math_operation(help_text, needs_one_tract=True): ''' @@ -29,10 +31,7 @@ def tract_math_operation(help_text, needs_one_tract=True): return internal_decorator -def main(): - from tract_querier.tract_math import operations - from tract_querier.tract_math import TractMathWrongArgumentsError - +def _build_arg_parser(): usage = r""" usage: %(prog)s ... operation ... \ [ optional flags ] @@ -84,7 +83,15 @@ def main(): parser.add_argument('operation_parameters', type=str, nargs=REMAINDER, help="operation parameters") + return parser + + +def main(): + from tract_querier.tract_math import TractMathWrongArgumentsError + + parser = _build_arg_parser() args = parser.parse_args() + # Load the global modules after the parsing of parameters from tract_querier.tractography import tractography_from_files diff --git a/scripts/tract_querier b/scripts/tract_querier index 2f658d9..8d71038 100755 --- a/scripts/tract_querier +++ b/scripts/tract_querier @@ -64,7 +64,8 @@ def affine_transform_tract(affine_transform, tract): return tract -def main(): +def _build_arg_parser(): + parser = OptionParser( version=0.1, usage="usage: %prog -t tractography_file -a atlas_file " @@ -101,6 +102,11 @@ def main(): "to put both in AC-PC coordinate space" ) + return parser + + +def main(): + parser = _build_arg_parser() (options, args) = parser.parse_args() if (