简体   繁体   中英

How to extend another extension's command in Hg?

I want to add additional option to the hg convert command, which is provided by internal extension hgext.convert .

I try to do the following in my __init__.py :

def extsetup(ui):
    entry = extensions.wrapcommand(commands.table, 'convert', convert_cmd)
    entry[1].append(('', 'test', None, _("test option")))

def convert_cmd(...): ...

But after enabling my extension and running proper hg convert --test , I get the following error:

hg: unknown command 'convert'
(did you mean one of clone, recover, revert?)

If I print the keys of commands.table , I can see that there are no custom commands inside.

How can I get and extend custom command?

According to "Writing Mercurial Extensions" :

After extsetup, the cmdtable is copied into the global command table in Mercurial.

So I need to modify the command inside the extension's cmdtable itself. It's a little bit hacky, but works for now:

from hgext import convert

def extsetup(ui):
    entry = extensions.wrapcommand(convert.cmdtable, 'convert', convert_cmd)
    entry[1].append(('', 'test', None, _("test option")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM