From 9c11adc38b57ed991f7c38c3353ff254c86edd23 Mon Sep 17 00:00:00 2001
From: Attila Farkas <ati@mail.muni.cz>
Date: Tue, 4 Feb 2020 10:58:06 +0100
Subject: [PATCH] move argument parsing to separate module

---
 create.py                        | 49 +++++++++++++++++++-------------
 modules/input_argument_parser.py | 31 ++++++++++++++++++++
 2 files changed, 60 insertions(+), 20 deletions(-)
 create mode 100644 modules/input_argument_parser.py

diff --git a/create.py b/create.py
index 5ad7906..b51eb17 100644
--- a/create.py
+++ b/create.py
@@ -1,32 +1,41 @@
 #!/usr/bin/python3
-""" A script that generates a Vagrantfile from yaml. """
+"""
+This script generates a Vagrantfile and ansible files from a YAML definition
+of virtual machines and network topology.  See the documentation for details.
+"""
 
 import sys
-import jinja2
 
 from modules.file_generator import generate_vagrantfile, generate_ansible_files
 from modules.device_creator import open_file
 from modules.routing import create_border_router
+from modules.input_argument_parser import parse_input_args
 
-if len(sys.argv) == 3:
-    if str(sys.argv[1]) == "-l":
-        ansible_local = True
-        input_file_name = str(sys.argv[2])
-    elif str(sys.argv[2]) == "-l":
-        ansible_local = True
-        input_file_name = str(sys.argv[1])
-    else:
-        print("Error: Expecting a yml file and optionally a flag -l.")
-        sys.exit()
-elif len(sys.argv) == 2:
-    ansible_local = False
-    input_file_name = str(sys.argv[1])
-else:
-    print("Error: Expecting a yml file and optionally a flag -l.")
-    sys.exit()
 
-device_definitions = open_file(input_file_name)
+""" Parsing the input arguments. """
+flags = dict()
+
+try:
+    input_file_name = parse_input_args(sys.argv, flags)
+except Exception:
+    print("Input arguments could not be parsed.")
+    raise
+
+""" Parsing the definitions file. """
+try:
+    device_definitions = open_file(input_file_name)
+except Exception:
+    print("Definitions file could not be parsed.")
+    raise
+
+
+
+# TODO
+
 create_border_router(device_definitions)
+if "ansible_local" in flags and flags["ansible_local"]:
+    generate_vagrantfile(device_definitions, True)
+else:
+    generate_vagrantfile(device_definitions, False)
 
-generate_vagrantfile(device_definitions, ansible_local)
 generate_ansible_files(device_definitions)
diff --git a/modules/input_argument_parser.py b/modules/input_argument_parser.py
new file mode 100644
index 0000000..d407790
--- /dev/null
+++ b/modules/input_argument_parser.py
@@ -0,0 +1,31 @@
+""" This module contains functions to parse input arguments. """
+
+def parse_input_args(argv, flags):
+    """ 
+    Parses the given input arguments for input file name and flags.
+
+    :param argv: input arguments (list of strings)
+    :param flags: dictionary to write the parsed flags
+    :returns: name of the input file
+    :raise AttributeError: invalid number of args or unknown flag
+    """
+
+    input_file_name = None
+
+    if len(argv) == 3:
+        if str(argv[1]) == "-l":
+            flags["ansible_local"] = True
+            input_file_name = str(argv[2])
+        elif str(argv[2]) == "-l":
+            flags["ansible_local"] = True
+            input_file_name = str(argv[1])
+        else:
+            print("Error: Expecting a yml file and optionally a flag -l.")
+            raise AttributeError
+    elif len(argv) == 2:
+        input_file_name = str(argv[1])
+    else:
+        print("Error: Expecting a yml file and optionally a flag -l.")
+        raise AttributeError
+
+    return input_file_name
-- 
GitLab