Subcommand '--version' parameters are not supported

Bug #1619708 reported by Stephen Finucane
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cliff
New
Undecided
Unassigned

Bug Description

I'm reworking some code to use cliff. I've modified the sample app to add the following file:

    $ cat demoapp/cliffdemo/api_db.py
    from cliff.command import Command

    class Sync(Command):
        """Sync the database up to the most recent version."""
        def get_parser(self, prog_name):
            parser = super(Sync, self).get_parser(prog_name)
            parser.add_argument(
                '--version', nargs='?', metavar='<version>', default=None,
                help='Database version')
            return parser

        def take_action(self, parsed_args):
            version = parsed_args.version
            self.app.stdout.write('syncing version "%s"\n' % version)
    $ git diff demoapp/setup.py
    diff --git a/demoapp/setup.py b/demoapp/setup.py
    index dd8695d..7562a7f 100644
    --- a/demoapp/setup.py
    +++ b/demoapp/setup.py
    @@ -60,6 +60,7 @@ setup(
                 'file = cliffdemo.show:File',
                 'show file = cliffdemo.show:File',
                 'unicode = cliffdemo.encoding:Encoding',
    + 'api-db_sync = cliffdemo.api_db:Sync',
             ],
         },

However, when I run this command it appears the '--version' parameter is parsed by the top level parser instead of being propagated down like the '--help' parser is.

    $ demoapp api-db sync --help
    usage: nova-manage api-db sync [-h] [--version [<version>]]

    Sync the database up to the most recent version.

    optional arguments:
      -h, --help show this help message and exit
      --version [<version>]
                            Database version

    $ demoapp api-db sync --version 2
    demoapp 0.1

This seems like a bug, to me: the '--version' parameter makes no sense in this location in a command and does not need to be reserved.

Revision history for this message
Stephen Finucane (stephenfinucane) wrote :

Updated to reflect impact to demoapp.

summary: - "reserved" options don't work with subcommands
+ Subcommand '--version' parameters are not supported
description: updated
Revision history for this message
Stephen Finucane (stephenfinucane) wrote :

Based on the argparse library, this shouldn't happen :/

    >>> import argparse
    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument('-g', '--global')
    _StoreAction(option_strings=['-g', '--global'], dest='global', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
    >>> subparsers = parser.add_subparsers(dest="subparser_name")
    >>> foo_parser = subparsers.add_parser('foo')
    >>> foo_parser.add_argument('-g', '--global', dest='foo_global')
    _StoreAction(option_strings=['-g', '--global'], dest='foo_global', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
    >>> foo_parser.add_argument('-c', '--count')
    _StoreAction(option_strings=['-c', '--count'], dest='count', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
    >>> args = parser.parse_args(['-g', 'xyz', 'foo', '--count', '42'])
    >>> parser.parse_args(['-g', 'xyz', 'foo', '--count', '42'])
    Namespace(count='42', foo_global=None, global='xyz', subparser_name='foo')
    >>> parser.parse_args(['foo', '-g', 'xyz', '--count', '42'])
    Namespace(count='42', foo_global='xyz', global=None, subparser_name='foo')
    >>> parser.add_argument('--version', action='version', version='0.1')
    _VersionAction(option_strings=['--version'], dest='version', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help="show program's version number and exit", metavar=None)
    >>> foo_parser.add_argument('--version', dest='foo_version')
    _StoreAction(option_strings=['--version'], dest='foo_version', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
    >>> parser.parse_args(['foo', '-g', 'xyz', '--count', '42', '--version', '2'])
    Namespace(count='42', foo_global='xyz', foo_version='2', global=None, subparser_name='foo')
    >>> parser.parse_args(['--version', '2', 'foo', '-g', 'xyz', '--count', '42'])
    0.1

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.