Python argparse默认值不起作用

时间:2018-05-18 11:18:45

标签: python argparse

我正在尝试使用argparse,该程序可以运行,但默认值不起作用。这是我的代码:

'''This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument.'''

import argparse
import sys

def calc(args):
    #Enable variables within the function to take on values from 'args' object. 
    operation = args.operation
    x = args.x
    y = args.y

    if (operation == "add"):
        return x + y
    elif (operation == "sub"):
        return x - y

parser = argparse.ArgumentParser(description="This is a summing program")   #parser is an object of the class Argument Parser.
parser.add_argument("x", type=float, default=1.0, help="What is the first number?") #add_argument is a method of the class ArgumentParser.
parser.add_argument("y", type=float, default=1.0, help='What is the second number?')
parser.add_argument("operation", type=str, default="add", help='What operation? Can choose add, sub, mul, or div')
args = parser.parse_args()
print(str(calc(args)))

这个简单的程序工作,但试图在没有值的情况下调用它会返回以下错误:

usage: cmdline.py [-h] x y operation
cmdline.py: error: the following arguments are required: x, y, operation

我哪里错了?

3 个答案:

答案 0 :(得分:1)

更改这些行以指示您需要命名的可选命令行参数(因此"-x"而不是"x"):

parser.add_argument("-x", type=float, default=1.0, help="What is the first number?") #add_argument is a method of the class ArgumentParser.
parser.add_argument("-y", type=float, default=1.0, help='What is the second number?')

答案 1 :(得分:1)

您缺少nargs='?'。以下作品:

import argparse
import sys 

def calc(args):
    #Enable variables within the function to take on values from 'args' object. 
    operation = args.operation
    x = args.x
    y = args.y

    if (operation == "add"):
        return x + y 
    elif (operation == "sub"):
        return x - y 

parser = argparse.ArgumentParser(description="This is a summing program")   #parser is an object of the class Argument Parser.
parser.add_argument("x", nargs='?', type=float, default=1.0, help="What is the first number?") #add_argument is a method of the class ArgumentParser.
parser.add_argument("y", nargs='?', type=float, default=1.0, help='What is the second number?')
parser.add_argument("operation", nargs='?', type=str, default="add", help='What operation? Can choose add, sub, mul, or div')
args = parser.parse_args()
print(str(calc(args)))

答案 2 :(得分:0)

@jbcoe - 我觉得你的代码中有一些错字,但谢谢你,它有效!这是解决方案,清理过来:

'''This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument. It uses the argparse module.'''

import argparse

def calc(args):
    #Enable variables within the function to take on values from 'args' object. 
    operation = args.operation
    x = args.x
    y = args.y

    if (operation == "add"):
        return x + y
    elif (operation == "sub"):
        return x - y

parser = argparse.ArgumentParser(description="This is a summing program")   #parser is an object of the class Argument Parser.
parser.add_argument("x", nargs='?', type=float, default=1.0, help="What is the first number?") #add_argument is a method of the class ArgumentParser.
parser.add_argument("y", nargs='?', type=float, default=2.0, help='What is the second number?')
parser.add_argument("operation", nargs='?', type=str, default="add", help='What operation? Can choose add, sub, mul, or div')
args = parser.parse_args()
print(str(calc(args)))