Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python; argparse; how to specify position of positional arguments

How do I specify the position of a positional argument?

In my example below, the SCR & SCV arguments appear at the end of the optional arguments, I want them to appear at the beginning.

#!/usr/bin/python
import argparse

### Parse arguments ###
parser = argparse.ArgumentParser()
parser.add_argument("SCR",type=int)
parser.add_argument("SCV",type=int)
parser.add_argument("--itemid",nargs='?')
parser.add_argument("--tkt",nargs='?')
parser.add_argument("--rfc",nargs='?')
parser.add_argument("--state",nargs='?')
parser.add_argument("--vendor",nargs='?')
parser.add_argument("--application",nargs='?')
parser.add_argument("--submitter",nargs='?')
parser.add_argument("--assigneddev",nargs='?')
parser.add_argument("--manager",nargs='?')
parser.add_argument("--maasteps",nargs='?')
parser.add_argument("--reasonforfailure",nargs='?')
parser.add_argument("--assignedpm",nargs='?')
parser.add_argument("--release",nargs='?')
parser.add_argument("--erelease",nargs='?')
parser.add_argument("--testenvloaded",nargs='?')
parser.add_argument("--datetestloaded",nargs='?')
parser.add_argument("--dateint2loaded",nargs='?')
parser.add_argument("--tqadeploytime",nargs='?')
parser.add_argument("--prodenvloaded",nargs='?')
parser.add_argument("--dateprodloaded",nargs='?')
parser.add_argument("--proddeploytime",nargs='?')
parser.add_argument("--proddeployer",nargs='?')
args = parser.parse_args()



> scratch.py -h
usage: scratch.py [-h] [--itemid [ITEMID]] [--tkt [TKT]] [--rfc [RFC]]
                  [--state [STATE]] [--vendor [VENDOR]]
                  [--application [APPLICATION]] [--submitter [SUBMITTER]]
                  [--assigneddev [ASSIGNEDDEV]] [--manager [MANAGER]]
                  [--maasteps [MAASTEPS]]
                  [--reasonforfailure [REASONFORFAILURE]]
                  [--assignedpm [ASSIGNEDPM]] [--release [RELEASE]]
                  [--erelease [ERELEASE]] [--testenvloaded [TESTENVLOADED]]
                  [--datetestloaded [DATETESTLOADED]]
                  [--dateint2loaded [DATEINT2LOADED]]
                  [--tqadeploytime [TQADEPLOYTIME]]
                  [--prodenvloaded [PRODENVLOADED]]
                  [--dateprodloaded [DATEPRODLOADED]]
                  [--proddeploytime [PRODDEPLOYTIME]]
                  [--proddeployer [PRODDEPLOYER]]
                  SCR SCV
like image 900
Radamand Avatar asked Apr 13 '15 17:04

Radamand


1 Answers

The default usage formatter separates the positional arguments from the optionals, and displays them at the end. In the case of multiline displays like this, the positionals are put on a separate line. That is what you are seeing.

When parsing positionals can come first.

An obvious way around this is to supply your own usage string, but with this many arguments I can understand not wanting to do so.

https://stackoverflow.com/a/26986546/901925 (last November) has a HelpFormatter patch that disables this reordering. But as written it only applies to single line usage. Multiline usage is handled later in the same method.

like image 53
hpaulj Avatar answered Oct 12 '22 00:10

hpaulj