Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对Get和Set使用一个命令参数?_Python_Argparse - Fatal编程技术网

Python 如何对Get和Set使用一个命令参数?

Python 如何对Get和Set使用一个命令参数?,python,argparse,Python,Argparse,我试图通过使用argparse实现参数解析。我需要的是Get和Set命令都有一个选项。这在Python中可能吗 这就是我需要的-例如: $ python prog.py -width Width is 15 cm. $ python prog.py -width=20 Width is set to 20 cm. 我试过了。但是我找不到一个方法来实现它。我必须为该选项使用两个不同的名称。一个是“getwidth”,另一个是“width”。这是我的密码: parser = argparse.Ar

我试图通过使用argparse实现参数解析。我需要的是Get和Set命令都有一个选项。这在Python中可能吗

这就是我需要的-例如:

$ python prog.py -width
Width is 15 cm.
$ python prog.py -width=20
Width is set to 20 cm.
我试过了。但是我找不到一个方法来实现它。我必须为该选项使用两个不同的名称。一个是“getwidth”,另一个是“width”。这是我的密码:

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument("-width", help="Set width")
parser.add_argument("-getwidth", action='store_true', help="Get width")

args = parser.parse_args()

if args.width:
    print("Set width to %s cm" % args.width)
if args.getwidth:
    print("Width is %s cm" % width_value)
以下是我的代码的结果:

$python ts.py-getwidth
宽度为21.0厘米
$python ts.py-宽度=25
设置宽度为25厘米

您可以这样做,将arg值0转换为默认值。像这样

import  argparse

width_value = 5

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument("-width",  help="Set width (0 = use default)")

args = parser.parse_args()
action = ""

if args.width != "0":
    width_value = int(args.width)
    action = "set to "

print("Width is %s%s cm" % (action, width_value))

给出输出

$ python pyargs.py -width=11
Width is set to 11 cm

$ python pyargs.py -width=0
Width is 5 cm

您可以这样做,使arg值0转换为默认值。像这样

import  argparse

width_value = 5

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument("-width",  help="Set width (0 = use default)")

args = parser.parse_args()
action = ""

if args.width != "0":
    width_value = int(args.width)
    action = "set to "

print("Width is %s%s cm" % (action, width_value))

给出输出

$ python pyargs.py -width=11
Width is set to 11 cm

$ python pyargs.py -width=0
Width is 5 cm

您可以使用
nargs='?'

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
    "-width",
    help="Set width",
    nargs='?',
    const='get',
)

args = parser.parse_args()

if args.width == 'get':
    print("Width is %s cm" % 1)
elif args.width is not None:
    print("Set width to %s cm" % args.width)

您可以使用
nargs='?'
执行此操作:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
    "-width",
    help="Set width",
    nargs='?',
    const='get',
)

args = parser.parse_args()

if args.width == 'get':
    print("Width is %s cm" % 1)
elif args.width is not None:
    print("Set width to %s cm" % args.width)

据我所知,这在使用
argparse时是不可能的。您可以像以前一样使用
action='store\u true'
使参数不需要值,但是您不能使两个参数具有相同的名称,或者它会引发
argparse.ArgumentError
据我所知,这在
argparse
中是不可能的。您可以像以前一样使用
action='store\u true'
创建一个不需要值的参数,但不能创建两个同名参数,否则会引发
argparse.ArgumentError
谢谢luther!这正是我需要的。@user1556331:Using
nargs='?“
在文档中有描述。谢谢luther!这正是我所需要的。@user1556331:Using
nargs='?“
在文档中有描述。谢谢。我也想过这个。这可能是一个解决方案。谢谢。我也想过这个。这可能是一个解决办法。