如果没有传递命令行参数,则为Python默认参数

时间:2016-10-11 14:28:30

标签: python argparse

我想用这种行为构建一个程序:

用法:sage 4ct.py [-h]( - r R | -i I | -p P)[ - o O]

但如果你没有给出任何参数,我想要" -r 100"作为默认值。

有可能吗?

parser = argparse.ArgumentParser(description = '4ct args')

group_input = parser.add_mutually_exclusive_group(required = True)
group_input.add_argument("-r", "-random", help = "Random graph: dual of a triangulation of N vertices", nargs = 1, type = int, default = 100)
group_input.add_argument("-i", "-input", help = "Input edgelist filename (networkx)", nargs = 1)
group_input.add_argument("-p", "-planar", help = "Load a planar embedding of the graph G.faces() - Automatically saved at each run: input_planar_file.serialized", nargs = 1)
parser.add_argument("-o", "-output", help="Output edgelist filename (networkx)", nargs = 1, required = False)

args = parser.parse_args()

2 个答案:

答案 0 :(得分:4)

只需删除required函数调用的add_mutually_exclusive_group参数(或将其设置为False)即可完成:

import argparse

parser = argparse.ArgumentParser(description = '4ct args')

group_input = parser.add_mutually_exclusive_group(required = False)
group_input.add_argument("-r", "--random", help = "Random graph: dual of a triangulation of N vertices", type = int, default = 100)
group_input.add_argument("-i", "--input", help = "Input edgelist filename (networkx)")
group_input.add_argument("-p", "--planar", help = "Load a planar embedding of the graph G.faces() - Automatically saved at each run: input_planar_file.serialized")
parser.add_argument("-o", "--output", help="Output edgelist filename (networkx)", required = False)

print(parser.parse_args())
# Namespace(input=None, output=None, planar=None, random=100)
print(parser.parse_args("-r 77".split()))
# Namespace(input=None, output=None, planar=None, random=77)
print(parser.parse_args("-o some/path".split()))
# Namespace(input=None, output='some/path', planar=None, random=100)
print(parser.parse_args("-i some/path".split()))
# Namespace(input='some/path', output=None, planar=None, random=100)
print(parser.parse_args("-i some/path -o some/other/path".split()))
# Namespace(input='some/path', output='some/other/path', planar=None, random=100)
print(parser.parse_args("-r 42 -o some/other/path".split()))
# Namespace(input=None, output='some/other/path', planar=None, random=42)

如您所见,random选项默认为100,即使:

  • 提供output选项,这似乎正常
  • 提供了来自random以外的互斥组的选项,这可能会有问题。如果random是唯一具有值的独占选项,那么您需要检查代码。

此示例还包括对选项解析器的一些微小改进:

  • 使用带有两个破折号的长选项名称(这是一种惯例,但它也允许argparse正确识别选项名称)。
  • 删除选项定义中的nargs=1,以便检索一个值的列表。通过删除它,您可以直接检索该值。

答案 1 :(得分:0)

尝试以下内容:

import argparse
import sys

parser = argparse.ArgumentParser(description='4ct args')

group_input = parser.add_mutually_exclusive_group(required=True)
group_input.add_argument("-r", "-random", help="Random graph: dual of a triangulation of N vertices", nargs=1, type=int,             default=100)
group_input.add_argument("-i", "-input", help="Input edgelist filename (networkx)", nargs=1)
group_input.add_argument("-p", "-planar", help="Load a planar embedding of the graph G.faces() - Automatically saved at each run: input_planar_file.serialized",nargs=1)
parser.add_argument("-o", "-output", help="Output edgelist filename (networkx)", nargs=1, required=False)

if not sys.argv[1:]:
    sys.argv.extend(['-r', '100'])

args = parser.parse_args(sys.argv[1:])

基本上,您正在检查是否有任何命令行参数,如果没有,则追加所需的-r 100