def __init__(self): # instantiate the CommandLineParser class self.commands = CommandLineParser() # step 01 # add the acceptable arguments for our command line program self.add_arguments() # step 02 # parse the arguments,if successful return True and arguments dictionary(True,{'-f':'folder/example.txt'}) # returns False if something went wrong self.response = self.parse_arguments() # step 03
class Example(object): def __init__(self): # instantiate the CommandLineParser class self.commands = CommandLineParser() # step 01 # add the acceptable arguments for our command line program self.add_arguments() # step 02 # parse the arguments,if successful return True and arguments dictionary(True,{'-f':'folder/example.txt'}) # returns False if something went wrong self.response = self.parse_arguments() # step 03 #print (self.response) def __str__(self): return 'Example object' def __repr__(self): return 'Example object' # step 02 def add_arguments(self): self.file = self.commands.addArgument('-f',help_text = 'the file to read the content from') self.help = self.commands.addArgument('-h',help_text = 'show help') # step 03 def parse_arguments(self): response = self.commands.parseArguments() return response # check to see, which arguments are passed in and do stuff according to the arguments def do_stuff(self): # if the response is not false if (not self.response == False): for i in self.response[1].iterkeys(): # if the file argument is passed, open the file and print the content # -f ex/ex.txt if (i == self.file): with open(self.response[1][self.file],'r') as f: content = f.read() print ("Content of the file:-\n{0}".format(content)) # if -h is passed as an argument, display help if (i == self.help): self.commands.showHelp() else: print ("warning: NO ARGUMENTS passed") def __del__(self): return True
class Test_c_parse_1(unittest.TestCase): def setUp(self): self.commands = CommandLineParser() # test get_helpTexts with a blank list def test_get_helpTexts(self): expected = False self.assertEqual(self.commands.get_helpTexts(),expected) def tearDown(self): del self.commands
def setUp(self): self.commands = CommandLineParser() self.commands.addArgument('-f',help_text = "file") self.commands.addArgument('-p',help_text = "update/patch") self.commands.addArgument('-d',help_text = "delete/remove") self.commands.addArgument('-h',help_text = "show help")
class Test_c_parse_0(unittest.TestCase): def setUp(self): self.commands = CommandLineParser() self.commands.addArgument('-f',help_text = "file") self.commands.addArgument('-p',help_text = "update/patch") self.commands.addArgument('-d',help_text = "delete/remove") self.commands.addArgument('-h',help_text = "show help") def test_str_function(self): expected = 'CommandLineParser object' self.assertEqual(self.commands.__str__(),expected) def test_repr_function(self): expected = 'CommandLineParser object' self.assertEqual(self.commands.__repr__(),expected) def test_addArgument(self): expected = '-f' self.assertEqual(self.commands.addArgument('-f'),expected) #-------------------------------------------------------------------- # test formatCommands with command line arguments def test_formatCommands_0(self): test_args = ["test.py","-f","/images/flower.png","-p","/images/flowers.png"] expected = ["-f /images/flower.png","-p /images/flowers.png"] with patch.object(sys,'argv',test_args): self.assertEqual(self.commands.formatCommands(),expected) # test formatCommands without command line arguments def test_formatCommands_1(self): test_args = ["test.py"] expected = False with patch.object(sys,'argv',test_args): self.assertEqual(self.commands.formatCommands(),expected) # test formatCommands with one single argument def test_formatCommands_2(self): test_args = ["test.py","-f"] expected = ['-f 0'] with patch.object(sys,'argv',test_args): self.assertEqual(self.commands.formatCommands(),expected) # test formatCommands with many single argument def test_formatCommands_3(self): test_args = ["test.py","-f","-h"] expected = ['-f 0','-h 0'] with patch.object(sys,'argv',test_args): self.assertEqual(self.commands.formatCommands(),expected) #-------------------------------------------------------------------- # test parseArguments with command line arguments def test_parseArguments_0(self): test_args = ["test.py","-f","/images/flower.png"] expected = (True,{"-f":"/images/flower.png"}) with patch.object(sys,'argv',test_args): self.commands.formatCommands() self.assertEqual(self.commands.parseArguments(),expected) # test parseArguments without command line arguments def test_parseArguments_1(self): test_args = ["test.py"] expected = False with patch.object(sys,'argv',test_args): self.commands.formatCommands() self.assertEqual(self.commands.parseArguments(),expected) #-------------------------------------------------------------------- # test get_argumentsDict def test_get_argumentsDict(self): test_args = ["test.py","-f","/images/flower.png"] expected = {"-f":"/images/flower.png"} with patch.object(sys,'argv',test_args): self.commands.parseArguments() self.assertEqual(self.commands.get_argumentsDict(),expected) #-------------------------------------------------------------------- # test get_helpTexts def test_get_helpTexts(self): expected = ['file','update/patch','delete/remove','show help'] self.assertEqual(self.commands.get_helpTexts(),expected) # test get_argsKeys def test_get_argsKeys(self): test_args = ["test.py",'-p','update.exe',"-f","/images/flower.png",'-d','help.txt'] expected = ['-d','-f','-p'] with patch.object(sys,'argv',test_args): self.commands.parseArguments() self.assertEqual(self.commands.get_argsKeys(),expected) # test get_argsValues def test_get_argsValues(self): test_args = ["test.py","-f","/images/flower.png",'-d','help.txt'] expected = ['help.txt','/images/flower.png'] with patch.object(sys,'argv',test_args): self.commands.parseArguments() self.assertEqual(self.commands.get_argsValues(),expected) def tearDown(self): del self.commands
def setUp(self): self.commands = CommandLineParser()
def __init__(self): self.commands_list = [] self.commands = CommandLineParser() self.add_arguments() self.response = self.parse_arguments()
class HandleCommandLineArguments(object): def __init__(self): self.commands_list = [] self.commands = CommandLineParser() self.add_arguments() self.response = self.parse_arguments() # string reprsentation of a class def __str__(self): return 'HandleCommandLineArguments object' # representation of the class def __repr__(self): return 'HandleCommandLineArguments object' # add acceptable arguments def add_arguments(self): start_frame = self.commands.addArgument( '-s', help_text="start frame number.") number_of_frames = self.commands.addArgument( '-n', help_text="number of frames to capture.") delay_time = self.commands.addArgument('-d', help_text="delay time.") name_prefix = self.commands.addArgument('-p', help_text="name prefix.") extension = self.commands.addArgument('-e', help_text="extension.") resolution = self.commands.addArgument('-r', help_text="resolution.") output_directory = self.commands.addArgument( '-o', help_text="output directory.") help_ = self.commands.addArgument('-h', help_text="show help.") self.commands_list.append(start_frame) self.commands_list.append(number_of_frames) self.commands_list.append(delay_time) self.commands_list.append(name_prefix) self.commands_list.append(extension) self.commands_list.append(resolution) self.commands_list.append(output_directory) self.commands_list.append(help_) # parse the arguments def parse_arguments(self): response = self.commands.parseArguments() return response # get the len of commands passed def get_len_of_commands_list(self): try: return len(self.response[1]) except: return 0 # get the response object def get_response_obj(self): return self.response # get the acceptable commands list def get_commands_list(self): for i in self.commands_list: yield i # display the help def display_help(self): print( "usage: python main.py -s [] -n [] -d [] -p [] -e [] -r [] -o []") print( "Example: python main.py -s 1 -n 1 -d 1 -p screenshot_ -e png -r 1024x768 -o shots" ) self.commands.showHelp() # delete this object def __del__(self): return True