argparse

can't pass arguements using argparse and python 3.4.2 on Windows 7

让人想犯罪 __ 提交于 2019-12-24 13:53:00
问题 I've upgraded to Python 3.4.2 and argparse (from optparse) but neither appears to recognise command line options. As a simple test I run this; #test_argparse.py def main(): import argparse parser = argparse.ArgumentParser(description='Execute database query.') parser.add_argument("-q", "--query", dest="query", help="Name of the query file to be run") args = parser.parse_args() print(args) if __name__ == '__main__': main() From the command line, when I run test_argparse.py -q get_msre_for_book

Python argparse: Does it have to return a list?

百般思念 提交于 2019-12-24 12:38:25
问题 I am trying to obtain a string of numbers from argparse. It's optional whether or not the argument -n is provided. import argparse parser = argparse.ArgumentParser() parser.add_argument('-n', nargs=1) # -n is optional but must come with one and only one argument args = parser.parse_args() test = args.n if test != 'None': print("hi " + test) The program fails when I do not provide "-n argument", but works fine when I do. Traceback (most recent call last): File "parse_args_test.py", line 7, in

Unittest with command-line arguments

时间秒杀一切 提交于 2019-12-24 12:26:20
问题 From what I understand from another SO post, to unittest a script that takes command line arguments through argparse, I should do something like the code below, giving sys.argv[0] as arg. import unittest import match_loc class test_method_main(unittest.TestCase): loc = match_loc.main() self.assertEqual(loc, [4]) if __name__ == '__main__': sys.argv[1] = 'aaaac' sys.argv[2] = 'ac' unittest.main(sys.argv[0]) This returns the error: usage: test_match_loc.py [-h] text patterns [patterns ...] test

Why does Python's argparse use an error code of 2 for SystemExit?

最后都变了- 提交于 2019-12-24 11:15:06
问题 When I give Python's argparse input it doesn't like, it raises a SystemExit with a code of 2, which seems to mean "No such file or directory". Why use this error code? import argparse import errno parser = argparse.ArgumentParser() parser.add_argument('arg') try: parser.parse_args([]) except SystemExit as se: print("Got error code {} which is {} in errno" .format(se.code, errno.errorcode[se.code])) produces this output: usage: se.py [-h] arg se.py: error: too few arguments Got error code 2

Django - where put argparse command to modify -h option?

纵然是瞬间 提交于 2019-12-24 09:40:03
问题 I would like to modify my help option in project I created. I put argparse in manage.py file, like this parser = argparse.ArgumentParser( description="Some desc", formatter_class=argparse.RawTextHelpFormatter ) args = parser.parse_args() print(args) if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Reporter.settings") try: from django.core.management import execute_from_command_line except ImportError: # The above import may fail for some other reason. Ensure that

How to run pytest with a specified test directory on a file that uses argparse?

感情迁移 提交于 2019-12-24 08:05:04
问题 I am writing unit tests for a Python library using pytest I need to specify a directory for test files to avoid automatic test file discovery, because there is a large sub-directory structure, including many files in the library containing "_test" or "test_" in the name but are not intended for pytest Some files in the library use argparse for specifying command-line options The problem is that specifying the directory for pytest as a command-line argument seems to interfere with using

Is there a way to get the argument of argparse in the order in which they were defined?

*爱你&永不变心* 提交于 2019-12-24 08:03:35
问题 I'd like to print all options of the program and they are grouped for readability. However when accessing the arguments via vars(args) , the order is random. 回答1: argparse parses the list of arguments in sys.argv[1:] ( sys.argv[0] is used as the prog value in usage ). args=parser.parse_args() returns a argparse.Namespace object. vars(args) returns a dictionary based on this object ( args.__dict__ ). Keys of a dictionary are unordered. print(args) also uses this dictionary order. The parser

Explain lambda argparse.HelpFormatter(prog, width)

有些话、适合烂在心里 提交于 2019-12-24 06:35:55
问题 This code works properly to increase the width of the help text, but it's unclear. What is the lambda function doing? EDIT: To clarify, the question is not Why are lambda functions useful in general , but instead, how is the argument parser init code using the lambda function ? import argparse import sys formatter = lambda prog: argparse.HelpFormatter(prog, width=100) dummy_text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore

argparse fails when called from unittest test

可紊 提交于 2019-12-24 04:33:15
问题 In a file (say parser.py ) I have: import argparse def parse_cmdline(cmdline=None): parser = argparse.ArgumentParser() parser.add_argument('--first-param',help="Does foo.") parser.add_argument('--second-param',help="Does bar.") if cmdline is not None: args = parser.parse_args(cmdline) else: args = parser.parse_args() return vars(args) if __name__=='__main__': print parse_cmdline() Sure enough, when called from the command line it works and give me pretty much what I expect: $ ./parser.py -

Does argparse support multiple exclusive arguments?

拥有回忆 提交于 2019-12-24 04:26:12
问题 Let's say I have two groups of arguments. You can use any number of arguments from each group but you cannot mix arguments between the groups. Is there a way to customize conflicting arguments in argparse module? I've tried using method add_mutually_exclusive_group but it's not what I'm looking for. 回答1: I've proposed a patch (or rather patches) that would let you test for general logical combinations of arguments. http://bugs.python.org/issue11588 . The core of my ideas there is to add a