From d02d3071e7858df06c1ffe570d5ba0f2cb8d61be Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Mon, 14 Oct 2024 01:12:29 -0400 Subject: [PATCH] progress --- Package/Python/__init__.py | 0 .../src/bellande_parser/bellande_parser.py | 67 ++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 Package/Python/__init__.py diff --git a/Package/Python/__init__.py b/Package/Python/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Package/Python/src/bellande_parser/bellande_parser.py b/Package/Python/src/bellande_parser/bellande_parser.py index a03e54c..1c2e5e5 100644 --- a/Package/Python/src/bellande_parser/bellande_parser.py +++ b/Package/Python/src/bellande_parser/bellande_parser.py @@ -15,7 +15,7 @@ #!/usr/bin/env python3 -import re +import re, sys, json from typing import Dict, List, Union, Any class Bellande_Format: @@ -141,3 +141,68 @@ class Bellande_Format: return 'null' else: return str(value) + + def main(self) -> int: + """ + Main method to handle command-line operations. + Returns an integer exit code. + """ + if len(sys.argv) < 2: + print("Usage: Bellande_Format [] []") + print("Commands: parse , write , help") + return 1 + + command = sys.argv[1] + + try: + if command == 'parse': + if len(sys.argv) < 3: + print("Error: Please provide a file path to parse.") + return 1 + file_path = sys.argv[2] + result = self.parse_bellande(file_path) + print(result) + return 0 + + elif command == 'write': + if len(sys.argv) < 4: + print("Error: Please provide a file path to write to and the input data.") + return 1 + file_path = sys.argv[2] + input_data = sys.argv[3] + + try: + data = json.loads(input_data) + except json.JSONDecodeError: + print("Error: Invalid JSON input. Please provide valid JSON data.") + return 1 + + self.write_bellande(data, file_path) + print(f"Data successfully written to {file_path}") + return 0 + + elif command == 'help': + print("Bellande_Format Usage:") + print(" parse : Parse a Bellande format file and print the result") + print(" write : Write data in Bellande format to a file") + print(" should be a valid JSON string") + print(" help: Display this help message") + return 0 + + else: + print(f"Unknown command: {command}") + print("Use 'Bellande_Format help' for usage information.") + return 1 + + except Exception as e: + print(f"An error occurred: {e}", file=sys.stderr) + return 1 + +def main(): + """ + Function to be used as the entry point in setup.py + """ + return Bellande_Format().main() + +if __name__ == "__main__": + sys.exit(main())