This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients Martin.d'Anjou, benschmaus, bethard, docs@python, eric.araujo, eric.smith, martin.panter, mburger, paul.j3, r.david.murray, rhettinger, terry.reedy, tshepang
Date 2014-02-13.03:22:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1392261774.4.0.0112486935545.issue9694@psf.upfronthosting.co.za>
In-reply-to
Content
As Steven pointed out, the existing `add_argument_group` mechanism can be used to group required arguments.  For example

    ------------------------- temp.py --------------------------
    parser = argparse.ArgumentParser(description = 'Do something')
    group1 = parser.add_argument_group('required arguments')
    group1.add_argument('--reqarg', '-r', required=True)
    parser.add_argument('--optarg','-o')
    parser.add_argument('foo')
    parser.print_help()
    ------------------------------------------------------------
    usage: ipython [-h] --reqarg REQARG [--optarg OPTARG] foo
    Do something
    positional arguments:
      foo
    optional arguments:
      -h, --help            show this help message and exit
      --optarg OPTARG, -o OPTARG
    required arguments:
      --reqarg REQARG, -r REQARG

Positional 'foo' can also be put in the 'required' group:
    
    group1.add_argument('foo')

    required arguments:
      --reqarg REQARG, -r REQARG
      foo

The distinction between 'positionals' and 'optionals' (or flagged) is essential to the parsing, but it is not necessary for Help Formatting.

I can imagine grouping arguments by 'required/not-required' properties.  It might be worth constructing an alternative HelpFormatter class that regroups the arguments in this way.  Subclassing the HelpFormatter is the established way of adding features to the help display.

The existing HelpFormatter flags 'required' arguments in the usage line with '[]'.  There it is has the added task of flagging Mutually Exclusive Groups in the same way.

It's worth keeping in mind that whether an argument is 'required' or not is determined in 2 different ways.  There is an optional 'required' flag (default False).  But this flag is not allowed for 'positionals'.  Instead with those 'argparse' looks at 'nargs' ('?*' are not required).

The 'required' attribute of an argument (Action) is ignored during 'parse_args' until the end.  At that time it makes an inventory of 'required' arguments that have not been seen, and potentially raises an error.  That testing was changed in a relatively recent patch, and produced an unintended change in whether 'subparsers' were required or not. (I could look up those issues in needed).

I'll think about creating the alternative HelpFormatter.
History
Date User Action Args
2014-02-13 03:22:54paul.j3setrecipients: + paul.j3, rhettinger, terry.reedy, bethard, eric.smith, eric.araujo, r.david.murray, docs@python, benschmaus, tshepang, martin.panter, mburger, Martin.d'Anjou
2014-02-13 03:22:54paul.j3setmessageid: <1392261774.4.0.0112486935545.issue9694@psf.upfronthosting.co.za>
2014-02-13 03:22:54paul.j3linkissue9694 messages
2014-02-13 03:22:53paul.j3create