Defining the order of the arguments with argparse - Python
<p>I have the following command line tool:</p>

<pre><code>import argparse

parser = argparse.ArgumentParser(description = "A cool application.")
parser.add_argument('positional')
parser.add_argument('--optional1')
parser.add_argument('--optional2')

args = parser.parse_args()
print args.positionals
</code></pre>

<p>The output of <code>python args.py</code> is:</p>

<pre><code>usage: args.py [-h] [--optional1 OPTIONAL1] [--optional2 OPTIONAL2]
               positional
</code></pre>

<p>however I would like to have:</p>

<pre><code>usage: args.py [-h] positional [--optional1 OPTIONAL1] [--optional2 OPTIONAL2]
</code></pre>

<p>How could I have that reordering?</p>

<p>You would either have to provide your own help formatter, or specify an explicit usage string:</p>

<pre><code>parser = argparse.ArgumentParser(
    description="A cool application.",
    usage="args.py [-h] positional [--optional1 OPTIONAL1] [--optional2 OPTIONAL2]")
</code></pre>

<p>The order in the help message, though, does not affect the order in which you can specify the arguments. <code>argparse</code> processes any defined options left-to-right, then assigns any remaining arguments to the positional parameters from left to right. Options and positional arguments can, for the most part, be mixed.</p>

python command-line command argparse args