argparse

Argparse: Required argument 'y' if 'x' is present

冷暖自知 提交于 2019-12-17 04:25:10
问题 I have a requirement as follows: ./xyifier --prox --lport lport --rport rport for the argument prox , I use action='store_true' to check if it is present or not. I do not require any of the arguments. But, if --prox is set I require rport and lport as well. Is there an easy way of doing this with argparse without writing custom conditional coding. More Code: non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy') non_int.add_argument('--lport', type=int, help='Listen

How do you write tests for the argparse portion of a python module?

狂风中的少年 提交于 2019-12-17 04:17:17
问题 I have a Python module that uses the argparse library. How do I write tests for that section of the code base? 回答1: You should refactor your code and move the parsing to a function: def parse_args(args): parser = argparse.ArgumentParser(...) parser.add_argument... # ...Create your parser as you like... return parser.parse_args(args) Then in your main function you should just call it with: parser = parse_args(sys.argv[1:]) (where the first element of sys.argv that represents the script name is

How do you write tests for the argparse portion of a python module?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 04:17:12
问题 I have a Python module that uses the argparse library. How do I write tests for that section of the code base? 回答1: You should refactor your code and move the parsing to a function: def parse_args(args): parser = argparse.ArgumentParser(...) parser.add_argument... # ...Create your parser as you like... return parser.parse_args(args) Then in your main function you should just call it with: parser = parse_args(sys.argv[1:]) (where the first element of sys.argv that represents the script name is

Python argparse: default value or specified value

穿精又带淫゛_ 提交于 2019-12-17 04:14:31
问题 I would like to have a optional argument that will default to a value if only the flag is present with no value specified, but store a user-specified value instead of the default if the user specifies a value. Is there already an action available for this? An example: python script.py --example # args.example would equal a default value of 1 python script.py --example 2 # args.example would equal a default value of 2 I can create an action, but wanted to see if there was an existing way to do

Python argparse mutual exclusive group

元气小坏坏 提交于 2019-12-17 03:04:29
问题 What I need is: pro [-a xxx | [-b yyy -c zzz]] I tried this but does not work. Could someone help me out? group= parser.add_argument_group('Model 2') group_ex = group.add_mutually_exclusive_group() group_ex.add_argument("-a", type=str, action = "store", default = "", help="test") group_ex_2 = group_ex.add_argument_group("option 2") group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test") group_ex_2.add_argument("-c", type=str, action = "store", default = "", help=

python命令行传入参数

假装没事ソ 提交于 2019-12-16 15:57:03
1.sys import sys a=eval(sys.argv[1]) b=eval(sys.argv[2]) print(a+b) 1 2 3 4 5 6 evel()函数是将字符串形式的int,字典等转化成对应真正的int,字典 在这里插入图片描述 2.argparse(python自带库) import argparse parser = argparse.ArgumentParser(description="Demo of argparse") parser.add_argument('-n','--name', default=' 5 ') parser.add_argument('-y','--year', default='20') args = parser.parse_args() print(args) a = args.name b = args.year print(type(a)) print(a+b) 来源: https://www.cnblogs.com/ruiy/p/11730224.html

parsing argument in python

[亡魂溺海] 提交于 2019-12-14 03:42:54
问题 I have a problem I am trying to find a solution. I am not sure if I can do it with argparse. I want to be able to specify an option myprog -a 1 myprog -a 2 Now when I have a = 1 , I want to be able to specify b and c . But when a = 2 , I can only specify d . myprog -a 1 -b 3 -c 0 myprog -a 2 -d 3 Also a must always be specified 回答1: You can't do this with switched values as a single parse_args call, but you can do one of: Use sub-commands/sub-parsers Do partial parsing before fully

CLI option that is an aggregation of two other options and precedence, in python

∥☆過路亽.° 提交于 2019-12-14 03:13:57
问题 I'm using argparse to parse CLI options in my Python scripts. I would create a flag that is equivalent to specifying two other flags. So python myscript.py --flagc is equivalent to python myscript.py --flaga --flagb This is my code: args = parser.parse_args() if args.flagc: args.flaga = True args.flagb = True The problem is flaga and flagb have also opposite flags, no-flaga and no-flagb : parser.add_argument("--flaga", dest="flaga", action="store_true") parser.add_argument("--no-flaga", dest=

Change argparse usage message argument order

风流意气都作罢 提交于 2019-12-14 02:55:50
问题 I'm using argparse in python3. In my script, I have some subparsers, a positional argument and some optional arguments. I have an optional argument to pass in any number of file paths, and it's using nargs='*' . The usage message for my script displays like this: usage: myprog.py subparser1 [-h] [--dir DIR] [--files [FILES [FILES ...]]] positional_arg But if you actually put the positional_arg after the --files flag as suggested by this usage message, I think the parser ends up consuming it

Handling argparse conflicts

北战南征 提交于 2019-12-13 23:34:49
问题 If I import a Python module that is already using argparse , however, I would like to use argparse in my script as well ...how should I go about doing this? I'm receiving a unrecognized arguments error when using the following code and invoking the script with a -t flag: Snippet: #!/usr/bin/env python .... import conflicting_module import argparse ... ################################# # Step 0: Configure settings... # ################################# parser = argparse.ArgumentParser