Accessing varying command line arguements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Accessing varying command line arguements (/thread-34402.html) |
Accessing varying command line arguements - Rakshan - Jul-28-2021 I am using a java jar in Pycharm environment to achieve testing in a pythion environment. To make the application more interactive, we plan to use commandline arguments. Eg: python test.py opc.tcp://localhost:XXXX AASo here, I am testing a AA configuration server at the mentioned URL. Now with this input, all the tests are performed. The code used is as follows. class TestClient: def __init__(self, URL, Config): self.Url = URL self.config = Config def perform_test(self): # subprocess.call(['java','-version']) subprocess.call( ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion', '2.0']) def read_results(self): pass if __name__ == "__main__": client = TestClient("opc.tcp://localhost:XXXX", "AA") client.perform_test() client.read_results()But to enhance further I want the user to type the version number and also the test he wants to do. As in (TEST A) as a parameter. But I also need to give a provision that if he does not provide this paramter, he should be able to perform all tests like previously. So I have modified the code this way considering overloading. import re import sys import bs4 as bs4 inputURL = sys.argv[1] inputConfig = sys.argv[2] version = sys.argv[3] print(len(sys.argv)) if len(sys.argv) > 4: typeOfTest = sys.argv[4] class TestClient: def __init__(self, URL, Config, Version): self.Url = URL self.config = Config self.version = Version def __init__(self, URL, Config, Version, TestType): self.testType = TestType #here i expect overloading of constructor def perform_test(self): # subprocess.call(['java','-version']) if len(sys.argv) > 4: subprocess.call( ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion', self.version]) else: subprocess.call( ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion', self.version, '-test', self.testType]) def read_results(self): pass if __name__ == "__main__": if len(sys.argv) > 4: client = TestClient(inputURL, inputConfig, version) else: client = TestClient(inputURL, inputConfig, version, typeOfTest) client.perform_test() client.read_results()So, I type on the command line : python test.py opc.tcp://localhost:XXXX AA 2.0 TESTABut i get an error saying: Is the constructor overloading not poossible here? Please can anyone guide me some alternative?UPDATE: just understood that funciton overloading is not possible in python. How to achieve this scenario working? any suggestions please RE: Accessing varying command line arguements - popejose - Jul-28-2021 Im just a hobbyist at this but my fix would be to use a default value for TestType parameter and test for it.def __init__(self, URL, Config, Version, TestType=None): if TestType == None: ... else: ... RE: Accessing varying command line arguements - deanhystad - Jul-28-2021 Python method overloading doesn't work that way. The last __init__ is the only. The other is unreachable. But you can get something very similar to signature overloading using the dispatch decorator. RE: Accessing varying command line arguements - snippsat - Jul-28-2021 Could do it like this,so with super() inherit all the methods and properties from its parent.Then add another parameter in the __init__() function.I guess also popejose way could work. import re import sys import bs4 as bs4 inputURL = sys.argv[1] inputConfig = sys.argv[2] version = sys.argv[3] print(len(sys.argv)) if len(sys.argv) > 4: typeOfTest = sys.argv[4] class Client: def __init__(self, URL, Config, Version): self.Url = URL self.config = Config self.version = Version def perform_test(self): # subprocess.call(['java','-version']) if len(sys.argv) > 4: subprocess.call( ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion', self.version]) else: subprocess.call( ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion', self.version, '-test', self.testType]) def read_results(self): pass class TestClient(Client): def __init__(self, URL, Config, Version, TestType): super().__init__(URL, Config, Version) self.testType = TestType if __name__ == "__main__": if len(sys.argv) > 4: client = Client(inputURL, inputConfig, version) else: client = TestClient(inputURL, inputConfig, version, typeOfTest) client.perform_test() client.read_results() |