#! /usr/bin/env python
from __future__ import print_function

import argparse
import sys
import textwrap

from .api import check_bmi


def configure_parser_test(sub_parsers=None):
    help = "Test a BMI class."

    example = textwrap.dedent(
        """

    Examples:

    bmi test bmimodule:BmiClass
    """
    )

    if sub_parsers is None:
        p = argparse.ArgumentParser(
            description=help,
            fromfile_prefix_chars="@",
            epilog=example,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )
    else:
        p = sub_parsers.add_parser(
            "test",
            help=help,
            description=help,
            epilog=example,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        )

    p.add_argument("cls", help="Full name of class to test.")
    p.add_argument("--infile", default="", help="Name of input file for init method.")
    p.add_argument(
        "--manifest", default="", type=str, help="Name of manifest file of input files."
    )
    p.add_argument("--bmi-version", default="1.1", help="BMI version to test against")
    p.add_argument("--help-pytest", action="store_true", help="Print help for pytest")
    p.set_defaults(func=execute)

    return p


def execute(args, extra):
    return check_bmi(
        args.cls,
        input_file=args.infile,
        manifest=args.manifest,
        bmi_version=args.bmi_version,
        extra_args=extra,
        help_pytest=args.help_pytest,
    )


def main():
    p = configure_parser_test()

    args, extra = p.parse_known_args()

    sys.exit(args.func(args, extra))

@@ -91,22 +91,108 @@
 mport te
-xtwrap
+mpfile%0Aimport textwrap%0A%0Afrom model_metadata.api import query, stage%0Afrom scripting import cd
 %0A%0Afrom .
@@ -1311,116 +1311,390 @@
   p.
-set_defaults(func=execute)%0A%0A    return p%0A%0A%0Adef execute(args, extra):%0A    return check_bmi(%0A        args.cls,
+add_argument(%0A        %22--stage%22, action=%22store_true%22, help=%22Stage with defaults for testing%22%0A    )%0A    p.set_defaults(func=execute)%0A%0A    return p%0A%0A%0Adef execute(args, extra):%0A    if args.stage:%0A        stage_dir = tempfile.mkdtemp()%0A        model = args.cls.split(%22:%22)%5B-1%5D%0A        input_file = query(model, %22run.config_file.path%22)%0A        manifest = stage(model, stage_dir)%0A    else:
 %0A   
@@ -1708,17 +1708,19 @@
 put_file
-=
+ = 
 args.inf
@@ -1722,17 +1722,16 @@
 s.infile
-,
 %0A       
@@ -1739,17 +1739,19 @@
 manifest
-=
+ = 
 args.man
@@ -1755,18 +1755,236 @@
 manifest
-,
 %0A
+        stage_dir = %22.%22%0A%0A    print(%22Running tests in %7B0%7D%22.format(stage_dir))%0A    with cd(stage_dir):%0A        return check_bmi(%0A            args.cls,%0A            input_file=input_file,%0A            manifest=manifest,%0A    
         
@@ -2013,16 +2013,20 @@
 ersion,%0A
+    
         
@@ -2047,24 +2047,28 @@
 ra,%0A        
+    
 help_pytest=
@@ -2081,24 +2081,28 @@
 elp_pytest,%0A
+    
     )%0A%0A%0Adef 

Add --stage argument to stage the tests with defaults.