且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python argparse具有不同数量的参数的不同参数

更新时间:2023-02-11 11:06:07

您可以添加更多参数,每个参数分别用于a,b和c,以及参数的可选参数.通过使用命名参数nargs ='?'您可以指定它们是可选的,并使用default ="some value"来确保其不会出错.最后,基于选定的选项a,b或c,您将能够捕获所需的选项.

You may add more parameters, one for each a, b, and c and also optional arguments for your params. By using the named parameter nargs='?' you can specify that they are optional and with the default="some value" you ensure it rises no errors. Finally, based on the selected option, a,b or c you will be able to capture the ones you need.

这是一个简短的用法示例:

Here's a short usage example:

parser.add_argument('x1', type=float, nargs='?', default=0, help='Circ. 1 X-coord')
parser.add_argument('y1', type=float, nargs='?', default=0, help='Circ. 1 Y-coord')
parser.add_argument('r1', type=float, nargs='?', default=70, help='Circ. 1 radius')
parser.add_argument('x2', type=float, nargs='?', default=-6.57, help='Circ. 2 X-coord')
parser.add_argument('y2', type=float, nargs='?', default=7, help='Circ. 2 Y-coord')
parser.add_argument('r2', type=float, nargs='?', default=70, help='Circ. 2 radius')
args = parser.parse_args()

circCoverage(args.x1, args.y1, args.r1, args.x2, args.y2, args.r2)

此处,如果未选择任何值,则使用默认值.您可以玩这个来获得想要的东西.

here, if no values are selected, the default ones are used. You can play with this to get what you want.

欢呼