简体   繁体   English

在python中传递可选参数

[英]Passing optional argument in python

I am trying to append a url by passing the parameters by commandline argument. 我试图通过命令行参数传递参数来附加URL。 Here is how I am trying: 这是我正在尝试的方法:

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input', type=str)
parser.add_argument('output', metavar='text', type=str)
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

When I execute 当我执行

python url.py text.csv hello

It appends the second passed argument to the url. 它将第二个传递的参数附加到url。 I want to know how to make the second argument optional so that even without providing the second argument I want the url to be printed by concatenating nothing to the url. 我想知道如何使第二个参数成为可选参数,以便即使不提供第二个参数,我也希望通过不将任何内容链接到url来打印url。 Here is the output I am expecting: 这是我期望的输出:

When both arguments given: 当两个参数都给出时:

python url.py text.csv hello

The output should be 输出应为

https://example.com/?z=12&text=hello&loc{}

When single argument given 当给出单个参数时

python url.py text.csv

The output should be 输出应为

https://example.com/?z=12&text=&loc{}

Try nargs='?' 试试nargs='?' and define a default : 并定义一个default

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input')
parser.add_argument('output', metavar='text', nargs='?', default='')
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

When I test it with just one commandline string, args.output is the default: 当我仅使用一个命令行字符串对其进行测试时, args.output是默认设置:

In [91]: args = parser.parse_args(['one'])

In [92]: args
Out[92]: Namespace(input='one', output='')

In [93]: args = parser.parse_args(['one','two'])

In [94]: args
Out[94]: Namespace(input='one', output='two')

It is best to only use nargs='?' 最好只使用nargs='?' (or '*' or '+') with the last positional argument. (或'*'或'+')和最后一个位置参数。 It's possible to use it on earlier ones, but there are complications that can send you back with more questions. 可以在较早的版本上使用它,但是有些复杂性会使您遇到更多问题。

As I see it, you have two options. 如我所见,您有两个选择。 Either you avoid using argparse altogether and go for something like: 您要么完全避免使用argparse要么这样做:

import sys
args_input  = sys.argv[1]
args_output = sys.argv[2] if len(sys.argv) > 2 else ''
url = 'https://example.com/?z=12&text='+args_output+'&loc{}'
print url

Or you add the - to your optional argument and, as Ignacio's reply suggests, you set a default empty value for the output argument: 或者,将-添加到您的可选参数中,然后按照Ignacio的答复建议,为output参数设置默认的空值:

import argparse
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument('input', metavar='input', type=str)
parser.add_argument('-output', metavar='text', type=str, default='')
args = parser.parse_args()
url = 'https://example.com/?z=12&text='+args.output+'&loc{}'
print url

With this second option you'll have to call it like: 使用第二个选项时,您必须像这样调用它:

python url.py text.csb -output hello

Or 要么

python url.py text.csb

but it is more extensible if you want to add more arguments afterwards. 但是如果您以后要添加更多参数,则可扩展性更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM