common arguments for subcommands

57 views
Skip to first unread message

Ted Stern

unread,
Apr 26, 2012, 6:26:19 PM4/26/12
to argh-...@googlegroups.com
Let's say that I have a couple of subcommands:

    @argh.arg('--forceopt',
              help='Forceopt help')
    def force(local_args):
        "Run FORCE post-processing, which includes MDOT"
        global args
        print "local_args = ", localargs
        yield "Does something with force, Namespace =", args

    @argh.arg('--ensiopt',
              help='ensiopt help')
    def ensi(local_args):
        "Run ENSI post-processing"
        global args
        print "local_args = ", localargs
        yield "Does something with ensi, Namespace =", args

And then I want to have a combo command 'std', which runs both of the other commands.

I want to be able to pass the same suboptions from each of the other subcommands to the combo command, to be passed down to the calls inside std to force and ensi.  Is there a simple way to do this?

Ted

Tyler Butler

unread,
Aug 19, 2012, 6:53:53 PM8/19/12
to argh-...@googlegroups.com
Alas, there doesn't appear to be a simple way to do it without an argh code change. 

Using argparse directly, you'd do this by passing a parents argument to the add_parser command. However, it doesn't appear that argh exposes that parameter in argh.helpers.add_commands. It would be fairly trivial to add a new parents keyword argument to add_commands, then pass it to add_parser when needed inside the function. It might be a bit more complicated than I'm making it out to be since I haven't looked deeply at the code, but it seems relatively straightforward.

Ted Stern

unread,
Aug 26, 2012, 9:32:29 PM8/26/12
to argh-...@googlegroups.com
Actually, the solution turned out to be very easy.

Define the arguments outside the decorator. This returns a function
which can be used to decorate more than one function.

cmd1opt = argh.arg(...options)

@cmd1opt
def cmd1(args)
...

cmd2opt = argh.arg(...options)
@cmd2opt
def cmd2(args)
....

# composite command:
@cmd1opt
@cmd2opt
def composite_command(args)
...

And you're done.

Ted
--
Frango ut patefaciam -- I break so that I may reveal

Tyler Butler

unread,
Aug 27, 2012, 1:12:45 PM8/27/12
to argh-...@googlegroups.com
Ahhh, makes sense - will have to give this a try. Thanks for the tip!
Reply all
Reply to author
Forward
0 new messages