diff --git a/scrapy/commands/runspider.py b/scrapy/commands/runspider.py index e9804e061..fe7142a57 100644 --- a/scrapy/commands/runspider.py +++ b/scrapy/commands/runspider.py @@ -4,6 +4,7 @@ import os from scrapy.utils.spider import iter_spider_classes from scrapy.command import ScrapyCommand from scrapy.exceptions import UsageError +from scrapy.utils.conf import arglist_to_dict def _import_file(filepath): abspath = os.path.abspath(filepath) @@ -33,8 +34,17 @@ class Command(ScrapyCommand): def long_desc(self): return "Run the spider defined in the given file" + def add_options(self, parser): + ScrapyCommand.add_options(self, parser) + parser.add_option("-a", dest="spargs", action="append", default=[], metavar="NAME=VALUE", \ + help="set spider argument (may be repeated)") + def process_options(self, args, opts): ScrapyCommand.process_options(self, args, opts) + try: + opts.spargs = arglist_to_dict(opts.spargs) + except ValueError: + raise UsageError("Invalid -a value, use -a NAME=VALUE", print_help=False) def run(self, args, opts): if len(args) != 1: @@ -49,7 +59,7 @@ class Command(ScrapyCommand): spclasses = list(iter_spider_classes(module)) if not spclasses: raise UsageError("No spider found in file: %s\n" % filename) - spider = spclasses.pop()() + spider = spclasses.pop()(**opts.spargs) # schedule spider and start engine self.crawler.queue.append_spider(spider) self.crawler.start()