Mario Stefanutti Mario Stefanutti - 1 month ago 8
Python Question

Python default parameter if no command line arguments are passed

I'l like to build a program with this behaviour:

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

But if you don't give any parameter, I'd like to have "-r 100" as the default.

Is it possible?

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()

Answer

Just remove the requiredargument of the add_mutually_exclusive_group function call (or set it to False) and you're done:

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)

As you can see, the random option is defaulted to 100 even if:

  • the output option is provided, which seems normal
  • an option from the mutual exclusive group other than random is provided, which can be problematic. you will have to check in your code if random is the only exclusive option which has a value before taking it in account.

This example also includes some tiny improvement to your option parser:

  • use long option names with two dashes (it is a convention but it also allows argparse to correctly recognise option name).
  • remove the nargs=1 in your options definitions which makes you retrieve a list of one value. By removing it, you could retrieve directly the value.
Comments