From 24f013df73fcbfb65ebbd5dc35f31a88d7c5db4e Mon Sep 17 00:00:00 2001 From: IceArmy Date: Wed, 13 Apr 2011 08:12:43 -0700 Subject: [PATCH] Script arguments can now use --option type args ... as long as the option names don't conflict with the programs option names of course --- python/phantom.py | 8 ++++---- python/pyphantomjs.py | 36 +++++++++++++++++++++++++++++++----- python/utils.py | 2 +- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/python/phantom.py b/python/phantom.py index 91aa1e6f..d2ca811c 100644 --- a/python/phantom.py +++ b/python/phantom.py @@ -49,14 +49,14 @@ class Phantom(QObject): self.m_page = WebPage(self) self.m_clipRect = QRect() # setup the values from args - self.m_script = QString.fromUtf8(args.script[0].read()) - self.m_scriptFile = args.script[0].name - self.m_args = args.script[1:] + self.m_script = QString.fromUtf8(args.script.read()) + self.m_scriptFile = args.script.name + self.m_args = args.script_args self.m_upload_file = args.upload_file autoLoadImages = False if args.load_images == 'no' else True pluginsEnabled = True if args.load_plugins == 'yes' else False - args.script[0].close() + args.script.close() palette = self.m_page.palette() palette.setBrush(QPalette.Base, Qt.transparent) diff --git a/python/pyphantomjs.py b/python/pyphantomjs.py index 85f93cbe..ae7ebae2 100644 --- a/python/pyphantomjs.py +++ b/python/pyphantomjs.py @@ -34,7 +34,9 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) if __name__ == '__main__': # Handle all command-line options p = argParser() - args = p.parse_args() + arg_data = p.parse_known_args(sys.argv[1:]) + args = arg_data[0] + args.script_args = arg_data[1] # register an alternative Message Handler messageHandler = MessageHandler(args.verbose) @@ -48,7 +50,31 @@ if __name__ == '__main__': if len(item_buffer) == 0: p.print_help() sys.exit(1) - args.script = args.upload_file[i:] + + # this is a bug workaround for argparse. + # if you call parse_known_args, and you + # have an --option script arg, the args + # get jumbled up, and it's inconsistent + # + # we're just going to check for -- and + # swap it all back to the right order + if args.script_args: + for i in range(len(args.upload_file)): + if not args.upload_file[i].count('='): + # insert the arg after --option + args.script_args.insert(1, args.script) + # insert value args before --option + if args.upload_file[i+1:]: + arg_buffer = args.upload_file[i+1:] + arg_buffer.reverse() + for val in arg_buffer: + args.script_args.insert(0, val) + args.script = args.upload_file[i] + break + else: + args.script = args.upload_file[i] + args.script_args = args.upload_file[i+1:] + break item_buffer[QString(item[0])] = QString(item[1]) for tag in item_buffer: @@ -63,14 +89,14 @@ if __name__ == '__main__': sys.exit(1) args.proxy = item - if len(args.script) == 0: + if not args.script: p.print_help() sys.exit(1) try: - args.script[0] = open(args.script[0]) + args.script = open(args.script) except IOError as (errno, stderr): - qFatal(str(stderr) + ': \'%s\'' % args.script[0]) + qFatal(str(stderr) + ': \'%s\'' % args.script) app = QApplication(sys.argv) diff --git a/python/utils.py b/python/utils.py index 73d1392f..9cafb222 100644 --- a/python/utils.py +++ b/python/utils.py @@ -53,7 +53,7 @@ def argParser(): formatter_class=argparse.RawTextHelpFormatter ) - parser.add_argument('script', metavar='script.[js|coffee]', nargs='*', + parser.add_argument('script', metavar='script.[js|coffee]', nargs='?', help='The script to execute, and any args to pass to it' ) parser.add_argument('--load-images', default='yes',