Argh for genuinely simple, no command programs?

29 views
Skip to first unread message

Jonathan Eunice

unread,
Jul 4, 2012, 6:41:33 AM7/4/12
to argh-...@googlegroups.com
Really liking the simplicity of argh. 

@arg('cmd')
@arg('name')
@arg('-a', '--account')
@arg('-c', '--conf')
@arg('-v', '--verbose', default=False)

beats the heck out of

     argparser = argparse.ArgumentParser(description='Reboot an AWS instance.', add_help = False)
    argparser.add_argument('-v', dest='verbose', action='store_true', default = False, 
                           help='be more verbose')
    argparser.add_argument('-c', dest='conf', action='store', default=None,
                           help='define config file ')
    argparser.add_argument('-a', dest='account', action='store', default=None,
                           help='set account')
    argparser.add_argument('cmd', action='store')
    argparser.add_argument('name', action='store')

But I'm confused by the apparent lack of support for simply passing such arguments to a single function such as main(). The dispatch mechanism seems to bend over backwards to make sure the program is being called with a program command options style. For simpler programs where the program itself IS the command program options , I'm having to perform an unnatural act to get argh working:

    args = ['main'] + sys.argv[1:]
    p.dispatch(argv=args)

Am I missing something? This seems like the simplest case that should work trivially, right out of the box. 

Jonathan Eunice

unread,
Nov 20, 2012, 11:46:02 AM11/20/12
to argh-...@googlegroups.com
Based on the final comment in this discussion and the recent appearance of the @dispatch_command decorator, this appears to be a resolved issue. 

You have to modify sys.argv early in your program (before the @dispatch_command decorated function) if you want to pretend certain args were passed (e.g. for testing purposes). That is slightly less clean than a simple functional dispatcher for the if __name__ == "__main__": section, but that's a nit. For production code, it's very clean and simple.

Andy Mikhailenko

unread,
Nov 25, 2012, 1:25:37 PM11/25/12
to argh-...@googlegroups.com
Hello,

glad to know that the library gets even more useful.

> That is slightly less clean than a simple functional dispatcher for the if
> __name__ == "__main__": section

Actually, dispatch_command() is just an ordinary function that accepts
another function as its single argument and therefore *can* be used as
decorator, so it's still possible to write proper reusable code:

from commands import trigger_doomsday_device

if __name__ == '__main__':
dispatch_command(trigger_doomsday_device)

The decorator version is rather intended for quick-and-dirty scripting
(though I won't say it's *that* dirty).

--
regards,
Andy

Please avoid sending me Word or PowerPoint attachments.
See http://gnu.org/philosophy/no-word-attachments.html
Plain text, OpenDocument or even PDF are OK.
Reply all
Reply to author
Forward
0 new messages