Skip to content

Commit fd9755a

Browse files
committed
Error checking inside argparse
1 parent 5f95d88 commit fd9755a

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

precicecasegenerate/cli.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def makeGenerateParser(add_help: bool = True) -> argparse.ArgumentParser:
2222
)
2323
parser.add_argument(
2424
"input_file",
25-
type=Path,
25+
type=cli_helper.yaml_file,
2626
nargs="?",
2727
help="Path to the input YAML topology file.",
28-
default=Path(cli_helper.DEFAULT_TOPOLOGY_NAME)
28+
default=cli_helper.DEFAULT_TOPOLOGY_NAME # Needs to be a string to cover the case no input is given
2929
)
3030
parser.add_argument(
3131
"-v", "--verbose", action="store_true", help="Enable verbose logging output."
@@ -42,10 +42,6 @@ def runGenerate(args: argparse.Namespace) -> int:
4242
setup_logging(verbose=args.verbose)
4343
logger.info("Program started.")
4444

45-
return_value: int = cli_helper.validate_args(args)
46-
if return_value != 0:
47-
return return_value
48-
4945
input_file: Path = Path(args.input_file)
5046
output_root: Path = Path(args.output_path)
5147

@@ -117,6 +113,7 @@ def main() -> int:
117113
# Parse the command line arguments
118114
parser = makeGenerateParser()
119115
args = parser.parse_args()
116+
logger.debug(f"Arguments parsed. Arguments: {vars(args)}.")
120117
return runGenerate(args)
121118

122119

precicecasegenerate/cli_helper.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,28 @@
1111
PRECICE_CONFIG_FILE_NAME: str = "precice-config.xml"
1212
GENERATED_DIR_NAME: str = "_generated"
1313
LOG_DIR_NAME: str = ".logs"
14-
DEFAULT_TOPOLOGY_NAME:str = "topology.yaml"
14+
DEFAULT_TOPOLOGY_NAME: str = "topology.yaml"
1515

1616

17-
18-
def validate_args(args: argparse.Namespace) -> int:
17+
def yaml_file(filepath: str) -> Path:
1918
"""
20-
Validate the arguments passed to the CLI.
21-
This checks if the input file exists and is a YAML file.
22-
:param args: The parsed arguments.
23-
:return: 0 if the arguments are valid, 1 otherwise.
19+
Check if the filepath points to an existing YAML file.
20+
Otherwise, raise an argparse.ArgumentTypeError.
21+
:param filepath: The path to the input file as a string.
22+
:return: The path to the input file as a Path object.
2423
"""
25-
logger.debug(f"Arguments parsed. Arguments: {vars(args)}. Checking if given file exists.")
26-
27-
input_file: Path = Path(args.input_file).resolve()
24+
input_file = Path(filepath).resolve()
2825

2926
# Check if the file exists
3027
if not input_file.is_file():
3128
logger.critical(f"File {input_file.resolve()} does not exist. Aborting program.")
32-
return 1
29+
raise argparse.ArgumentTypeError(f"File '{input_file.resolve()}' does not exist.")
3330
logger.debug(f"File {input_file.resolve()} exists.")
3431

3532
# Check if the file is a YAML file
36-
if input_file.suffix.lower() in [".yaml", ".yml"]:
37-
logger.debug(f"File {input_file.resolve()} is a YAML file.")
38-
else:
33+
if input_file.suffix.lower() not in [".yaml", ".yml"]:
3934
logger.critical(f"File {input_file.resolve()} is not a YAML file. Aborting program.")
40-
return 1
41-
return 0
35+
raise argparse.ArgumentTypeError(f"The file '{input_file}' is not a YAML file.")
36+
logger.debug(f"File {input_file.resolve()} is a YAML file.")
37+
38+
return input_file

0 commit comments

Comments
 (0)