def HandleCommandLine(cls, serviceClassString=None, argv=None, customInstallOptions='', customOptionHandler=None):
 err = 0
 if (argv is None):
  argv = sys.argv
 if (len(argv) <= 1):
  usage()
 serviceName = cls._svc_name_
 serviceDisplayName = cls._svc_display_name_
 if (serviceClassString is None):
  serviceClassString = GetServiceClassString(cls)
 import getopt
 try:
  (opts, args) = getopt.getopt(argv[1:], customInstallOptions, ['password=', 'username=', 'startup=', 'perfmonini=', 'perfmondll=', 'interactive', 'wait='])
 except getopt.error as details:
  print details
  usage()
 userName = None
 password = None
 perfMonIni = perfMonDll = None
 startup = None
 interactive = None
 waitSecs = 0
 for (opt, val) in opts:
  if (opt == '--username'):
   userName = val
  elif (opt == '--password'):
   password = val
  elif (opt == '--perfmonini'):
   perfMonIni = val
  elif (opt == '--perfmondll'):
   perfMonDll = val
  elif (opt == '--interactive'):
   interactive = 1
  elif (opt == '--startup'):
   map = {'manual': win32service.SERVICE_DEMAND_START, 'auto': win32service.SERVICE_AUTO_START, 'disabled': win32service.SERVICE_DISABLED}
   try:
    startup = map[val.lower()]
   except KeyError:
    print ("'%s' is not a valid startup option" % val)
  elif (opt == '--wait'):
   try:
    waitSecs = int(val)
   except ValueError:
    print '--wait must specify an integer number of seconds.'
    usage()
 arg = args[0]
 knownArg = 0
 if (arg == 'start'):
  knownArg = 1
  print ('Starting service %s' % serviceName)
  try:
   StartService(serviceName, args[1:])
   if waitSecs:
    WaitForServiceStatus(serviceName, win32service.SERVICE_RUNNING, waitSecs)
  except win32service.error as exc:
   print ('Error starting service: %s' % exc.strerror)
 elif (arg == 'restart'):
  knownArg = 1
  print ('Restarting service %s' % serviceName)
  RestartService(serviceName, args[1:])
  if waitSecs:
   WaitForServiceStatus(serviceName, win32service.SERVICE_RUNNING, waitSecs)
 elif (arg == 'debug'):
  knownArg = 1
  if (not hasattr(sys, 'frozen')):
   svcArgs = ' '.join(args[1:])
   try:
    exeName = LocateSpecificServiceExe(serviceName)
   except win32api.error as exc:
    if (exc[0] == winerror.ERROR_FILE_NOT_FOUND):
     print 'The service does not appear to be installed.'
     print 'Please install the service before debugging it.'
     sys.exit(1)
    raise
   try:
    os.system(('%s -debug %s %s' % (exeName, serviceName, svcArgs)))
   except KeyboardInterrupt:
    pass
  else:
   DebugService(cls, args)
 if ((not knownArg) and (len(args) != 1)):
  usage()
 if (arg == 'install'):
  knownArg = 1
  try:
   serviceDeps = cls._svc_deps_
  except AttributeError:
   serviceDeps = None
  try:
   exeName = cls._exe_name_
  except AttributeError:
   exeName = None
  try:
   exeArgs = cls._exe_args_
  except AttributeError:
   exeArgs = None
  try:
   description = cls._svc_description_
  except AttributeError:
   description = None
  print ('Installing service %s' % (serviceName,))
  try:
   InstallService(serviceClassString, serviceName, serviceDisplayName, serviceDeps=serviceDeps, startType=startup, bRunInteractive=interactive, userName=userName, password=password, exeName=exeName, perfMonIni=perfMonIni, perfMonDll=perfMonDll, exeArgs=exeArgs, description=description)
   if customOptionHandler:
    customOptionHandler(*(opts,))
   print 'Service installed'
  except win32service.error as exc:
   if (exc.winerror == winerror.ERROR_SERVICE_EXISTS):
    arg = 'update'
   else:
    print ('Error installing service: %s (%d)' % (exc.strerror, exc.winerror))
    err = exc.winerror
  except ValueError as msg:
   print ('Error installing service: %s' % str(msg))
   err = (-1)
   try:
    RemoveService(serviceName)
   except win32api.error:
    print 'Warning - could not remove the partially installed service.'
 if (arg == 'update'):
  knownArg = 1
  try:
   serviceDeps = cls._svc_deps_
  except AttributeError:
   serviceDeps = None
  try:
   exeName = cls._exe_name_
  except AttributeError:
   exeName = None
  try:
   exeArgs = cls._exe_args_
  except AttributeError:
   exeArgs = None
  try:
   description = cls._svc_description_
  except AttributeError:
   description = None
  print 'Changing service configuration'
  try:
   ChangeServiceConfig(serviceClassString, serviceName, serviceDeps=serviceDeps, startType=startup, bRunInteractive=interactive, userName=userName, password=password, exeName=exeName, displayName=serviceDisplayName, perfMonIni=perfMonIni, perfMonDll=perfMonDll, exeArgs=exeArgs, description=description)
   if customOptionHandler:
    customOptionHandler(*(opts,))
   print 'Service updated'
  except win32service.error as exc:
   print ('Error changing service configuration: %s (%d)' % (exc.strerror, exc.winerror))
   err = exc.winerror
 elif (arg == 'remove'):
  knownArg = 1
  print ('Removing service %s' % serviceName)
  try:
   RemoveService(serviceName)
   print 'Service removed'
  except win32service.error as exc:
   print ('Error removing service: %s (%d)' % (exc.strerror, exc.winerror))
   err = exc.winerror
 elif (arg == 'stop'):
  knownArg = 1
  print ('Stopping service %s' % serviceName)
  try:
   if waitSecs:
    StopServiceWithDeps(serviceName, waitSecs=waitSecs)
   else:
    StopService(serviceName)
  except win32service.error as exc:
   print ('Error stopping service: %s (%d)' % (exc.strerror, exc.winerror))
   err = exc.winerror
 if (not knownArg):
  err = (-1)
  print ("Unknown command - '%s'" % arg)
  usage()
 return err

def HandleCommandLine ( cls , serviceClassString = None , argv = None , customInstallOptions = '' , customOptionHandler = None ) : err = 0 if ( argv is None ) : argv = sys . argv if ( len ( argv ) <= 1 ) : usage ( ) serviceName = cls . _svc_name_ serviceDisplayName = cls . _svc_display_name_ if ( serviceClassString is None ) : serviceClassString = GetServiceClassString ( cls ) import getopt try : ( opts , args ) = getopt . getopt ( argv [ 1 : ] , customInstallOptions , [ 'password=' , 'username=' , 'startup=' , 'perfmonini=' , 'perfmondll=' , 'interactive' , 'wait=' ] ) except getopt . error as details : print details usage ( ) userName = None password = None perfMonIni = perfMonDll = None startup = None interactive = None waitSecs = 0 for ( opt , val ) in opts : if ( opt == '--username' ) : userName = val elif ( opt == '--password' ) : password = val elif ( opt == '--perfmonini' ) : perfMonIni = val elif ( opt == '--perfmondll' ) : perfMonDll = val elif ( opt == '--interactive' ) : interactive = 1 elif ( opt == '--startup' ) : map = { 'manual' : win32service . SERVICE_DEMAND_START , 'auto' : win32service . SERVICE_AUTO_START , 'disabled' : win32service . SERVICE_DISABLED } try : startup = map [ val . lower ( ) ] except KeyError : print ( "'%s' is not a valid startup option" % val ) elif ( opt == '--wait' ) : try : waitSecs = int ( val ) except ValueError : print '--wait must specify an integer number of seconds.' usage ( ) arg = args [ 0 ] knownArg = 0 if ( arg == 'start' ) : knownArg = 1 print ( 'Starting service %s' % serviceName ) try : StartService ( serviceName , args [ 1 : ] ) if waitSecs : WaitForServiceStatus ( serviceName , win32service . SERVICE_RUNNING , waitSecs ) except win32service . error as exc : print ( 'Error starting service: %s' % exc . strerror ) elif ( arg == 'restart' ) : knownArg = 1 print ( 'Restarting service %s' % serviceName ) RestartService ( serviceName , args [ 1 : ] ) if waitSecs : WaitForServiceStatus ( serviceName , win32service . SERVICE_RUNNING , waitSecs ) elif ( arg == 'debug' ) : knownArg = 1 if ( not hasattr ( sys , 'frozen' ) ) : svcArgs = ' ' . join ( args [ 1 : ] ) try : exeName = LocateSpecificServiceExe ( serviceName ) except win32api . error as exc : if ( exc [ 0 ] == winerror . ERROR_FILE_NOT_FOUND ) : print 'The service does not appear to be installed.' print 'Please install the service before debugging it.' sys . exit ( 1 ) raise try : os . system ( ( '%s -debug %s %s' % ( exeName , serviceName , svcArgs ) ) ) except KeyboardInterrupt : pass else : DebugService ( cls , args ) if ( ( not knownArg ) and ( len ( args ) != 1 ) ) : usage ( ) if ( arg == 'install' ) : knownArg = 1 try : serviceDeps = cls . _svc_deps_ except AttributeError : serviceDeps = None try : exeName = cls . _exe_name_ except AttributeError : exeName = None try : exeArgs = cls . _exe_args_ except AttributeError : exeArgs = None try : description = cls . _svc_description_ except AttributeError : description = None print ( 'Installing service %s' % ( serviceName , ) ) try : InstallService ( serviceClassString , serviceName , serviceDisplayName , serviceDeps = serviceDeps , startType = startup , bRunInteractive = interactive , userName = userName , password = password , exeName = exeName , perfMonIni = perfMonIni , perfMonDll = perfMonDll , exeArgs = exeArgs , description = description ) if customOptionHandler : customOptionHandler ( * ( opts , ) ) print 'Service installed' except win32service . error as exc : if ( exc . winerror == winerror . ERROR_SERVICE_EXISTS ) : arg = 'update' else : print ( 'Error installing service: %s (%d)' % ( exc . strerror , exc . winerror ) ) err = exc . winerror except ValueError as msg : print ( 'Error installing service: %s' % str ( msg ) ) err = ( - 1 ) try : RemoveService ( serviceName ) except win32api . error : print 'Warning - could not remove the partially installed service.' if ( arg == 'update' ) : knownArg = 1 try : serviceDeps = cls . _svc_deps_ except AttributeError : serviceDeps = None try : exeName = cls . _exe_name_ except AttributeError : exeName = None try : exeArgs = cls . _exe_args_ except AttributeError : exeArgs = None try : description = cls . _svc_description_ except AttributeError : description = None print 'Changing service configuration' try : ChangeServiceConfig ( serviceClassString , serviceName , serviceDeps = serviceDeps , startType = startup , bRunInteractive = interactive , userName = userName , password = password , exeName = exeName , displayName = serviceDisplayName , perfMonIni = perfMonIni , perfMonDll = perfMonDll , exeArgs = exeArgs , description = description ) if customOptionHandler : customOptionHandler ( * ( opts , ) ) print 'Service updated' except win32service . error as exc : print ( 'Error changing service configuration: %s (%d)' % ( exc . strerror , exc . winerror ) ) err = exc . winerror elif ( arg == 'remove' ) : knownArg = 1 print ( 'Removing service %s' % serviceName ) try : RemoveService ( serviceName ) print 'Service removed' except win32service . error as exc : print ( 'Error removing service: %s (%d)' % ( exc . strerror , exc . winerror ) ) err = exc . winerror elif ( arg == 'stop' ) : knownArg = 1 print ( 'Stopping service %s' % serviceName ) try : if waitSecs : StopServiceWithDeps ( serviceName , waitSecs = waitSecs ) else : StopService ( serviceName ) except win32service . error as exc : print ( 'Error stopping service: %s (%d)' % ( exc . strerror , exc . winerror ) ) err = exc . winerror if ( not knownArg ) : err = ( - 1 ) print ( "Unknown command - '%s'" % arg ) usage ( ) return err
handle command line for a windows service prescribed name that will be called by py2exe .