argparse

Is it bad form to raise ArgumentError by hand?

一笑奈何 提交于 2019-12-10 01:28:37
问题 If you want to add an extra check not provided by argparse , such as: if variable a == b then c should be not None ...is it permissible to raise ArgumentError yourself? Or, should you raise Exception instead? Also what is common practice for this kind of situation? Say that you add a piece of code that's almost like a local extension of the library. Should you use the same exception type(s) as those provided by the library you are extending? 回答1: There's nothing inherently wrong with raising

python using argparse.ArgumentParser method

孤街浪徒 提交于 2019-12-10 01:21:29
问题 I've tried to learn how argparse.ArgumentParser works and I've write a couple lines for that : global firstProduct global secondProduct myparser=argparse.ArgumentParser(description='parser test') myparser.add_argument("product1",help="enter product1",dest='product_1') myparser.add_argument("product2",help="enter product2",dest='product_2') args=myparser.parse_args() firstProduct=args.product_1 secondProduct=args.product_2 I just want to that when User run this script with 2 parameters my code

How can I set a subparser to be optional in argparse?

混江龙づ霸主 提交于 2019-12-09 22:35:23
问题 import argparse parser_sub = subparsers.add_parser('files') parser_sub.add_argument( '--file-name', action='store', dest='filename', nargs='*') options = parser.parse_args() Output: error: too few arguments. As per this link: https://bugs.python.org/issue9253 it states that subparsers cant be optional. Can this behaviour be changed? I would like my subcommands to be optional. How can I achieve this through argparse in python 2.6? 回答1: There's not much that can be added to that bug/issue https

python argparse with dependencies

感情迁移 提交于 2019-12-09 14:43:49
问题 I'm writing a script which has 2 arguments which are mutually exclusive, and an option that only makes sense with one of those arguments. I'm trying to set up argparse to fail if you call it with the argument that makes no sense. To be clear: -m -f makes sense -s makes sense -s -f should throw errors no arguments are fine. The code I have is: parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file') parser.add_argument('host', nargs=1, help="ip address to

How to set custom output handlers for argparse in Python?

烂漫一生 提交于 2019-12-09 12:49:03
问题 I have configured logger to print both onto terminal stdout and to a file so I can have an archive of logging messages that I can refer to. That is easily accomplished by adding a FileHandler to your logging object. Easy peasy. What I want to accomplish now is to make argparse log also to the same file along with logs to stdout when it encounters parsing errors. So far it only prints to stdout . I looked in the argparse documentation but I can't find anything about setting a different output

How to make a short and long version of a required argument using Python Argparse?

半城伤御伤魂 提交于 2019-12-09 07:51:53
问题 I want to specify a required argument called inputdir but I also would like to have a shorthand version of it called i . I don't see a concise solution to do this without making both optional arguments and then doing my own check. Is there a preferred practice for this that I'm not seeing or the only way is to make both optional and do my own error-handling? Here is my code: import argparse parser = argparse.ArgumentParser() parser.add_argument("inputdir", help="Specify the input directory")

python argparse: How can I display help automatically on error?

狂风中的少年 提交于 2019-12-09 07:51:44
问题 Currently when I enter invalid options or omit positional arguments, argparse kicks me back to the prompt and displays the usage for my app. This is ok, but I would rather automatically display the full help listing (that explains the options, etc) than require the user to type ./myscript.py -h Thanks! Jamie 回答1: This thread over at Google groups has the following code snippet which seems to do the trick (modified slightly). class DefaultHelpParser(argparse.ArgumentParser): def error(self,

TypeError: __init__() got an unexpected keyword argument 'type' in argparse

爱⌒轻易说出口 提交于 2019-12-09 07:48:11
问题 Hey so I'm using argparse to try and generate a quarterly report. This is what the code looks like: parser = argparse.ArgumentParser() parser.add_argument('-q', "--quarter", action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4 ") parser.add_argument('-y', "--year", action='store_true',type=str,help="Enter a year in the format YYYY ") args = parser.parse_args() the error I receive is: TypeError: init () got an unexpected keyword argument 'type' as far as I can tell from the

Python dependencies between groups using argparse

£可爱£侵袭症+ 提交于 2019-12-09 06:21:43
问题 I started to learn Python, and now I'm learning the great benefits of argparse . Using argparse , I have created two groups of arguments: group_list and group_simulate . Each of the groups has its own arguments -- the user can specify only one argument in each group (achieved using parser.add_mutually_exclusive_group() ). And now my target is present a syntax error if the user specified arguments from both groupgs and not from only one of them -- I want to achieve this by using the

argparse optional value for argument

早过忘川 提交于 2019-12-08 19:22:57
问题 I want to distinguish between these three cases: The flag is not present at all python example.py ; The flag is present but without a value python example.py -t ; and The flag is present and has a value python example.py -t ~/some/path . How can I do this with Python argparse ? The first two cases would be covered by action='store_true' but then the third case becomes invalid. 回答1: You can do this with nargs='?': One argument will be consumed from the command line if possible, and produced as