Python default parameter if no command line arguments are passed
<p>I'l like to build a program with this behaviour:</p>

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

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

<p>Is it possible?</p>

<pre><code>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()
</code></pre>

<p>Just remove the <code>required</code>argument of the <code>add_mutually_exclusive_group</code> function call (or set it to False) and you're done:</p>

<pre><code>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)
</code></pre>

<p>As you can see, the <code>random</code> option is defaulted to 100 even if:</p>

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

<hr>

<p>This example also includes some tiny improvement to your option parser:</p>

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

python argparse