
import inspect
import os
import sys

from .version import __version__
from distutils.version import StrictVersion


def run(min_version=None):
    '''
        This function gets run from your robot code something like this:
        
            if __name__ == '__main__':
                retval = wpilib.run()
                exit(retval)
            
        This syntax ensures that the code will never run on the cRio, as the
        robot code is not the main code module.
        
        This will parse command line arguments, and allow the user to perform
        a number of different actions on the robot code.
        
        :param min_version:    Specify the minimum version of pyfrc required
                               to run tests
    '''
    
    if min_version is not None:
        pyfrc_version = StrictVersion(__version__)
        min_version = StrictVersion(min_version)
        
        if pyfrc_version < min_version:
            print("ERROR: robot code requires pyfrc %s or later (currently running %s)" % (min_version, pyfrc_version))
            return 1
    
    if len(sys.argv) == 1:
        print("Usage: %s upload|test ...", file=sys.stderr)
        return 1
    
    try:
        frame = inspect.currentframe()
        if frame is None:
            print("Your python interpreter does not seem to support 'currentframe'. Please use a different python interpreter", file=sys.stderr)
            return 4
        
        frame = frame.f_back
        
        if 'run' not in frame.f_globals:
            print('Your robot code does not seem to have a "run" function. It must have a run function!', file=sys.stderr)
            return 3
        
        run_fn = frame.f_globals['run']
        file_location = os.path.abspath(frame.f_code.co_filename)
        
    finally:
        del frame
    
    arg1 = sys.argv[1]
    del sys.argv[1]
    
    if arg1 == 'upload':
        from .cli import cli_upload
        cli_upload.run(run_fn, file_location)
        
    elif arg1 == 'test':
        from .cli import cli_test
        cli_test.run(run_fn, file_location)
        
    elif arg1 == 'sim':
        from .cli import cli_sim
        cli_sim.run(run_fn, file_location)

    elif arg1 == 'netsim':
        from .cli import cli_sim
        cli_sim.run(run_fn, file_location, True)

@@ -109,16 +109,679 @@
 ersion%0A%0A
+def require_version(min_version=None):%0A    '''%0A        You should *always* call this%0A         %0A        :param min_version:    Specify the minimum version of pyfrc required%0A                               to run tests.%0A        :return:               True if the version requirement is met, False otherwise%0A    '''%0A    if min_version is not None:%0A        pyfrc_version = StrictVersion(__version__)%0A        min_version = StrictVersion(min_version)%0A        %0A        if pyfrc_version %3C min_version:%0A            print(%22ERROR: robot code requires pyfrc %25s or later (currently running %25s)%22 %25 (min_version, pyfrc_version))%0A            return False%0A        %0A    return True%0A
 %0Adef run
@@ -1432,306 +1432,42 @@
  if 
-min_version is not None:%0A        pyfrc_version = StrictVersion(__version__)%0A        min_version = StrictVersion(min_version)%0A        %0A        if pyfrc_version %3C min_version:%0A            print(%22ERROR: robot code requires pyfrc %25s or later (currently running %25s)%22 %25 (min_version, pyfrc_version))%0A    
+not require_version(min_version):%0A
     
@@ -2654,32 +2654,32 @@
  import cli_sim%0A
-
         cli_sim.
@@ -2711,8 +2711,9 @@
 , True)%0A
+%0A

Add require_version call