Category : command-line-arguments

I want to pass a short product code when running my script: ./myscript.py –productcode r|u|c Then use the short product code to look up data stored in a tuple in the python code: # create tuples for each product r=("Redhat","7.2") u=("Ubuntu","7.5") c=("Centos","8.1") # parse the command line parser = argparse.ArgumentParser() parser.add_argument("–productcode", help="Short code for product") ..

Read more

Here is the code. def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="infomedia" ) parser.add_argument("file", help="path to file") parser.add_argument( "-i", "–info", type=str, default="False", help="get information about", ) cli_args = parser.parse_args() worker = Worker( cli_args.input, cli_args.info, ) worker._application() When the program is running with -h / –help it shows the default values. positional arguments: file path to file ..

Read more

I use coverage.py and unittest to test get_arguments need_to_test.py def get_arguments(): parser = argparse.ArgumentParser() parser.add_argument(‘-c’, ‘–config’, type=str, required=True) parser.add_argument(‘–num’, type=str, default=’0′) …. args = parser.parse_args() return args test_need_to_test.py import need_to_test class Test_need_to_test(unittest.TestCase): def setUp(self): self.args = need_to_test.get_arguments() def test_get_arguments(self): #test self.args here … def tearDown(self): pass if __name__ == ‘__main__’: unittest.main(testRunner=xmlrunner.XMLTestRunner(output=’test-reports’)) But when i run ..

Read more

I want to add an argument parsing with the following logic: A group which has either "-url (mandatory) -u (optional) -p (optional)" or "-l (mandatory)" groups. My code is like this: def cmdline(): parser = argparse.ArgumentParser(description=’Report the performance numbers from log.’); root_group = parser.add_mutually_exclusive_group(required=True); url_group = root_group.add_argument_group(); url_group.add_argument(‘-url’, ‘–url’, required=True, help=’URL to get sysmgr.log.’); url_group.add_argument(‘-u’, ..

Read more

Given an argument parser with n arguments, where I change the default value of only a small subset every run from the command line, is there a clean way of extracting a dict/namespace of all the non-default k,v arguments? parser = argparse.ArgumentParser() parser.add_argument("–a",type=str,default=’a’) parser.add_argument("–b",type=str,default=’b’) parser.add_argument("–c",type=str,default=’c’) parser.add_argument("–d",type=str,default=’d’) And python run.py –a "e" I would like to ..

Read more

I should start by saying that due to unrelated issues I had to uninstall and reinstall python several times, so that could have something to do with this. But when I go to modify my setup in the python setup application, install pip is checked but never installs. So I had to manually install it. ..

Read more