import sys
import argparse

from lettuce.bin import main as lettuce_main
from lettuce import world
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

BROWSER_CHOICES = [browser.lower()
                   for browser in DesiredCapabilities.__dict__.keys()
                   if not browser.startswith('_')]
BROWSER_CHOICES.append('zope.testbrowser')
BROWSER_CHOICES.sort()
DEFAULT_BROWSER = 'firefox'


def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(prog="Salad", description='BDD browswer-automation made tasty.')

    parser.add_argument('--browser', default=DEFAULT_BROWSER,
                        metavar='BROWSER', choices=BROWSER_CHOICES,
                        help=('Browser to use. Options: %s Default is %s.' %
                              (BROWSER_CHOICES, DEFAULT_BROWSER)))
    parser.add_argument('--remote-url',
                        help='Selenium server url for remote browsers')
    parser.add_argument('args', nargs=argparse.REMAINDER)

    parsed_args = parser.parse_args()
    world.drivers = [parsed_args.browser]
    world.remote_url = parsed_args.remote_url
    lettuce_main(args=parsed_args.args)

if __name__ == '__main__':
    main()

import sys
import argparse

from lettuce.bin import main as lettuce_main
from lettuce import world
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

BROWSER_CHOICES = [browser.lower()
                   for browser in DesiredCapabilities.__dict__.keys()
                   if not browser.startswith('_')]
BROWSER_CHOICES.append('zope.testbrowser')
BROWSER_CHOICES.sort()
DEFAULT_BROWSER = 'firefox'


def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(prog="Salad", description='BDD browswer-automation made tasty.')

    parser.add_argument('--browser', default=DEFAULT_BROWSER,
                        metavar='BROWSER', choices=BROWSER_CHOICES,
                        help=('Browser to use. Options: %s Default is %s.' %
                              (BROWSER_CHOICES, DEFAULT_BROWSER)))
    parser.add_argument('--remote-url',
                        help='Selenium server url for remote browsers')

    (parsed_args, leftovers) = parser.parse_known_args()
    world.drivers = [parsed_args.browser]
    world.remote_url = parsed_args.remote_url
    lettuce_main(args=leftovers)

if __name__ == '__main__':
    main()

Use parse_known_args and pass leftovers to lettuce
Use parse_known_args and pass leftovers to lettuce

Seems to be more reliable at handling weird argument ordering than
REMAINDER was

  import sys
  import argparse
  
  from lettuce.bin import main as lettuce_main
  from lettuce import world
  from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  
  BROWSER_CHOICES = [browser.lower()
                     for browser in DesiredCapabilities.__dict__.keys()
                     if not browser.startswith('_')]
  BROWSER_CHOICES.append('zope.testbrowser')
  BROWSER_CHOICES.sort()
  DEFAULT_BROWSER = 'firefox'
  
  
  def main(args=sys.argv[1:]):
      parser = argparse.ArgumentParser(prog="Salad", description='BDD browswer-automation made tasty.')
  
      parser.add_argument('--browser', default=DEFAULT_BROWSER,
                          metavar='BROWSER', choices=BROWSER_CHOICES,
                          help=('Browser to use. Options: %s Default is %s.' %
                                (BROWSER_CHOICES, DEFAULT_BROWSER)))
      parser.add_argument('--remote-url',
                          help='Selenium server url for remote browsers')
-     parser.add_argument('args', nargs=argparse.REMAINDER)
  
-     parsed_args = parser.parse_args()
+     (parsed_args, leftovers) = parser.parse_known_args()
      world.drivers = [parsed_args.browser]
      world.remote_url = parsed_args.remote_url
-     lettuce_main(args=parsed_args.args)
+     lettuce_main(args=leftovers)
  
  if __name__ == '__main__':
      main()
  
Use parse_known_args and pass leftovers to lettuce
## Code Before:
import sys
import argparse

from lettuce.bin import main as lettuce_main
from lettuce import world
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

BROWSER_CHOICES = [browser.lower()
                   for browser in DesiredCapabilities.__dict__.keys()
                   if not browser.startswith('_')]
BROWSER_CHOICES.append('zope.testbrowser')
BROWSER_CHOICES.sort()
DEFAULT_BROWSER = 'firefox'


def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(prog="Salad", description='BDD browswer-automation made tasty.')

    parser.add_argument('--browser', default=DEFAULT_BROWSER,
                        metavar='BROWSER', choices=BROWSER_CHOICES,
                        help=('Browser to use. Options: %s Default is %s.' %
                              (BROWSER_CHOICES, DEFAULT_BROWSER)))
    parser.add_argument('--remote-url',
                        help='Selenium server url for remote browsers')
    parser.add_argument('args', nargs=argparse.REMAINDER)

    parsed_args = parser.parse_args()
    world.drivers = [parsed_args.browser]
    world.remote_url = parsed_args.remote_url
    lettuce_main(args=parsed_args.args)

if __name__ == '__main__':
    main()

## Instruction:
Use parse_known_args and pass leftovers to lettuce
## Code After:
import sys
import argparse

from lettuce.bin import main as lettuce_main
from lettuce import world
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

BROWSER_CHOICES = [browser.lower()
                   for browser in DesiredCapabilities.__dict__.keys()
                   if not browser.startswith('_')]
BROWSER_CHOICES.append('zope.testbrowser')
BROWSER_CHOICES.sort()
DEFAULT_BROWSER = 'firefox'


def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(prog="Salad", description='BDD browswer-automation made tasty.')

    parser.add_argument('--browser', default=DEFAULT_BROWSER,
                        metavar='BROWSER', choices=BROWSER_CHOICES,
                        help=('Browser to use. Options: %s Default is %s.' %
                              (BROWSER_CHOICES, DEFAULT_BROWSER)))
    parser.add_argument('--remote-url',
                        help='Selenium server url for remote browsers')

    (parsed_args, leftovers) = parser.parse_known_args()
    world.drivers = [parsed_args.browser]
    world.remote_url = parsed_args.remote_url
    lettuce_main(args=leftovers)

if __name__ == '__main__':
    main()
