# To use exit...
import sys,getopt,struct
try:
   from netifaces import AF_INET, AF_INET6
   import netifaces as ni
except:
   print("[FATAL] Please install required \"netiface\" package. Refer to wiki if required.")

class CArgs:
   def __init__(self):
      # Init
      self.NetServer=None
      self.NetPort=None
      self.GameName=None
      self.NetAlias=None
      #
      # Usage Message
      #
      self.Usage = '--- Pydarts Help Page ---\n'
      self.Usage += '\n[ Main options ]\n'
      self.Usage += '--help | -h\t\tprint this help page\n'
      self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
      self.Usage += '--debuglevel=\t\tverbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors\n'
      self.Usage += '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
      self.Usage += '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n'
      self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
      self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
      self.Usage += '--config=\t\tuse an alternate config file (relative to your $HOME/.pydarts config folder)\n'
      self.Usage += '--scoreonlogo\t\tdisplay score on logo instead of displaying it on the middle of the screen\n'
      self.Usage += '\n[ Network options ]\n'
      self.Usage += '--netgamename=\t\tpreset network game name\n'
      self.Usage += '--servername=\t\tpreset network game server or ip address\n'
      self.Usage += '--serveralias=\t\tpreset network server alias (usefull to send to master server a public IP / behind NAT)\n'
      self.Usage += '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
      self.Usage += '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
      self.Usage += '\n[ Servers options ]\n'
      self.Usage += '--listen=\t\tlistening interface (server & master server only). Default is eth0.\n'
      self.Usage += '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
      self.Usage += '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
      self.Usage += '\n[ Advanced options ]\n'
      self.Usage += '--locale=\t\tSelect pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)\n'      
      self.Usage += '--bypass-stats\t\tbypass stats updates\n'
      self.Usage += '--noserial\t\tbypass serial connexion test\n'
      self.Usage += '--serialport=\t\tspecify serial port to use\n'
      self.Usage += '--serialspeed=\t\tspecify serial speed\n'
      self.Usage += '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
      self.Usage += '--animationduration=\tduration for every round animation (ms default:5)\n'
      self.Usage += '--forcecalibration=\tforce board calibration even if config file already exists\n'
      self.Usage += '** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):\n'
      self.Usage += '--gametype=\t\tpreset game type : only "local" is supported for now. No default.\n'
      self.Usage += '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
      self.Usage += '--localplayers=\t\tpreset players name, comma separated, lower case. No Default.\n'
      self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'


      # LIST HERE Option available for CLI
      self.shortopts='hN:S:P:o:a:'
      self.longopts=[      'serveralias=',
                           'localplayers=',
                           'selectedgame=',
                           'gametype=',
                           'masterserver=',
                           'masterport=',
                           'serverport=',
                           'servername=',
                           'netgamename=',
                           'locale=',
                           'debuglevel=',
                           'soundvolume=',
                           'solo=',
                           'colorset=',
                           'resx=',
                           'resy=',
                           'listen=',
                           'config=',
                           'blinktime=',
                           'releasedartstime=',
                           'nbcol=',
                           'espeakpath=',
                           'serialspeed=',
                           'serialport=',
                           'animationduration=',
                           'noserial',
                           'help',
                           'bypass-stats',
                           'fullscreen',
                           'mastertest',
                           'scoreonlogo',
                           'forcecalibration'
                     ]
      #
      #debug - print sys.argv
      try:
         self.opts,self.args = getopt.getopt(sys.argv[1:],self.shortopts,self.longopts) # Option waiting for a value are followed by a :
      except getopt.GetoptError as err: # Print a clean message in case of unrecognized option
         print(str(err)) # will print something like "option -a not recognized"
         sys.exit(1)
      if self.GetParamValue2('help') or self.GetParamValue2('h'):
         print(self.Usage)
         sys.exit(0)

#
# To filter game type
#

   def GetGameType(self,gametype):
      if str(gametype) in ('local'):#For now, only local is allowed by command line
         return gametype
      else:
         return False
#
# To give Logs objects after creation
#

   def SetLogs(self,Logs):
      self.Logs=Logs

#
# Return true if an arguement is present
#
   def GetArgument(self,arg):
      #print self.args
      if arg in self.args:
         return True
#
# Return the value of a parameter, true if the parameter has no value, false otherwise
#
   def GetParamValue(self,parameter):
      return_code = False
      for opt,arg in self.opts:
         if opt == parameter:
            if arg=='':
               return_code = True
            else:
               return_code = arg
      return return_code

#
# New way to get couple param/value
#
   def GetParamValue2(self,p):
      simple="-{}".format(p)
      double="--{}".format(p)
      for opt,arg in self.opts:
         if simple==opt or double==opt:
            if arg=='':
               return True
            else:
               return arg
      return False

#
# Give Network Status
#
   def NetRequested(self):
      if self.GetParamValue2('netgamename') != False or self.GetParamValue2('localplayers') != False:
         return True
      else:
         return False

#
# Give name of requested net game
#
   def NetGameName(self):
      if self.GetParamValue2('netgamename') != False:
         self.GameName=self.GetParamValue2('netgamename')
      else:
         return False
      #print(self.GameName)
      return self.GameName
         
#
# Return Network Server
#
   def GetNetServer(self):
      self.NetServer=self.GetParamValue2('servername')
      return self.NetServer
      
#
# Return Network Server
#
   def GetNetAlias(self):
      if self.GetParamValue2('serveralias'):
         self.NetAlias=self.GetParamValue2('serveralias')
      elif self.GetParamValue2('servername'):
         self.NetAlias=self.GetParamValue2('servername')
      else:
         return False
      return self.NetAlias

#
# Return Network Server port
#
   def GetNetPort(self):
      self.NetPort=self.GetParamValue2('serverport')
      return self.NetPort


#######################
# SERVERS METHODS
#######################

#
# Get ip address from either interface name or ipaddress (search if an ip match on system)
#
   def get_ip_address(self,ifname):
      # Interfaces lookup
      ifaces = ni.interfaces()
      # Print result
      self.Logs.Log("DEBUG","Found thoses interfaces : {}".format(ifaces))
      # Try to find ipaddress from interface name
      try:
         self.Logs.Log("DEBUG","Searching address matching interface name : {}".format(ifname))
         ipaddress = ni.ifaddresses(ifname)[AF_INET][0]['addr']
         # return the ip associated if lookup if a success
         return ipaddress
      # If it fails
      except Exception as e:
         # Print warning
         self.Logs.Log("WARNING","Ip lookup from interface name ({}) failed. Trying from IP.".format(ifname))
         # In case of IP adress given as interface
         for iface in ifaces:
            # Iter all ifaces
            try:
               # Get IP from iface
               ipaddress=ni.ifaddresses(iface)[AF_INET][0]['addr']
            # if it fails print error
            except:
               ipaddress=None
            # Compare ip address to arg given, if it match, so the argument was an ip address
            if ipaddress==ifname:
               # Return IP given so it can use it to listen
               return ipaddress
      # If everything failed, return None
      return None

38c61275175c588abb29fc0fd3bc72d29214e1d7
<mask token>


class CArgs:

    def __init__(self):
        self.NetServer = None
        self.NetPort = None
        self.GameName = None
        self.NetAlias = None
        self.Usage = '--- Pydarts Help Page ---\n'
        self.Usage += '\n[ Main options ]\n'
        self.Usage += '--help | -h\t\tprint this help page\n'
        self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
        self.Usage += """--debuglevel=		verbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors
"""
        self.Usage += (
            '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
            )
        self.Usage += (
            '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n')
        self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
        self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
        self.Usage += """--config=		use an alternate config file (relative to your $HOME/.pydarts config folder)
"""
        self.Usage += """--scoreonlogo		display score on logo instead of displaying it on the middle of the screen
"""
        self.Usage += '\n[ Network options ]\n'
        self.Usage += '--netgamename=\t\tpreset network game name\n'
        self.Usage += (
            '--servername=\t\tpreset network game server or ip address\n')
        self.Usage += """--serveralias=		preset network server alias (usefull to send to master server a public IP / behind NAT)
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Servers options ]\n'
        self.Usage += """--listen=		listening interface (server & master server only). Default is eth0.
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Advanced options ]\n'
        self.Usage += """--locale=		Select pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)
"""
        self.Usage += '--bypass-stats\t\tbypass stats updates\n'
        self.Usage += '--noserial\t\tbypass serial connexion test\n'
        self.Usage += '--serialport=\t\tspecify serial port to use\n'
        self.Usage += '--serialspeed=\t\tspecify serial speed\n'
        self.Usage += (
            '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
            )
        self.Usage += (
            '--animationduration=\tduration for every round animation (ms default:5)\n'
            )
        self.Usage += """--forcecalibration=	force board calibration even if config file already exists
"""
        self.Usage += """** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):
"""
        self.Usage += """--gametype=		preset game type : only "local" is supported for now. No default.
"""
        self.Usage += (
            '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
            )
        self.Usage += """--localplayers=		preset players name, comma separated, lower case. No Default.
"""
        self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'
        self.shortopts = 'hN:S:P:o:a:'
        self.longopts = ['serveralias=', 'localplayers=', 'selectedgame=',
            'gametype=', 'masterserver=', 'masterport=', 'serverport=',
            'servername=', 'netgamename=', 'locale=', 'debuglevel=',
            'soundvolume=', 'solo=', 'colorset=', 'resx=', 'resy=',
            'listen=', 'config=', 'blinktime=', 'releasedartstime=',
            'nbcol=', 'espeakpath=', 'serialspeed=', 'serialport=',
            'animationduration=', 'noserial', 'help', 'bypass-stats',
            'fullscreen', 'mastertest', 'scoreonlogo', 'forcecalibration']
        try:
            self.opts, self.args = getopt.getopt(sys.argv[1:], self.
                shortopts, self.longopts)
        except getopt.GetoptError as err:
            print(str(err))
            sys.exit(1)
        if self.GetParamValue2('help') or self.GetParamValue2('h'):
            print(self.Usage)
            sys.exit(0)
    <mask token>

    def SetLogs(self, Logs):
        self.Logs = Logs

    def GetArgument(self, arg):
        if arg in self.args:
            return True

    def GetParamValue(self, parameter):
        return_code = False
        for opt, arg in self.opts:
            if opt == parameter:
                if arg == '':
                    return_code = True
                else:
                    return_code = arg
        return return_code

    def GetParamValue2(self, p):
        simple = '-{}'.format(p)
        double = '--{}'.format(p)
        for opt, arg in self.opts:
            if simple == opt or double == opt:
                if arg == '':
                    return True
                else:
                    return arg
        return False

    def NetRequested(self):
        if self.GetParamValue2('netgamename') != False or self.GetParamValue2(
            'localplayers') != False:
            return True
        else:
            return False

    def NetGameName(self):
        if self.GetParamValue2('netgamename') != False:
            self.GameName = self.GetParamValue2('netgamename')
        else:
            return False
        return self.GameName

    def GetNetServer(self):
        self.NetServer = self.GetParamValue2('servername')
        return self.NetServer
    <mask token>
    <mask token>
    <mask token>

<mask token>


class CArgs:

    def __init__(self):
        self.NetServer = None
        self.NetPort = None
        self.GameName = None
        self.NetAlias = None
        self.Usage = '--- Pydarts Help Page ---\n'
        self.Usage += '\n[ Main options ]\n'
        self.Usage += '--help | -h\t\tprint this help page\n'
        self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
        self.Usage += """--debuglevel=		verbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors
"""
        self.Usage += (
            '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
            )
        self.Usage += (
            '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n')
        self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
        self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
        self.Usage += """--config=		use an alternate config file (relative to your $HOME/.pydarts config folder)
"""
        self.Usage += """--scoreonlogo		display score on logo instead of displaying it on the middle of the screen
"""
        self.Usage += '\n[ Network options ]\n'
        self.Usage += '--netgamename=\t\tpreset network game name\n'
        self.Usage += (
            '--servername=\t\tpreset network game server or ip address\n')
        self.Usage += """--serveralias=		preset network server alias (usefull to send to master server a public IP / behind NAT)
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Servers options ]\n'
        self.Usage += """--listen=		listening interface (server & master server only). Default is eth0.
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Advanced options ]\n'
        self.Usage += """--locale=		Select pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)
"""
        self.Usage += '--bypass-stats\t\tbypass stats updates\n'
        self.Usage += '--noserial\t\tbypass serial connexion test\n'
        self.Usage += '--serialport=\t\tspecify serial port to use\n'
        self.Usage += '--serialspeed=\t\tspecify serial speed\n'
        self.Usage += (
            '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
            )
        self.Usage += (
            '--animationduration=\tduration for every round animation (ms default:5)\n'
            )
        self.Usage += """--forcecalibration=	force board calibration even if config file already exists
"""
        self.Usage += """** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):
"""
        self.Usage += """--gametype=		preset game type : only "local" is supported for now. No default.
"""
        self.Usage += (
            '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
            )
        self.Usage += """--localplayers=		preset players name, comma separated, lower case. No Default.
"""
        self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'
        self.shortopts = 'hN:S:P:o:a:'
        self.longopts = ['serveralias=', 'localplayers=', 'selectedgame=',
            'gametype=', 'masterserver=', 'masterport=', 'serverport=',
            'servername=', 'netgamename=', 'locale=', 'debuglevel=',
            'soundvolume=', 'solo=', 'colorset=', 'resx=', 'resy=',
            'listen=', 'config=', 'blinktime=', 'releasedartstime=',
            'nbcol=', 'espeakpath=', 'serialspeed=', 'serialport=',
            'animationduration=', 'noserial', 'help', 'bypass-stats',
            'fullscreen', 'mastertest', 'scoreonlogo', 'forcecalibration']
        try:
            self.opts, self.args = getopt.getopt(sys.argv[1:], self.
                shortopts, self.longopts)
        except getopt.GetoptError as err:
            print(str(err))
            sys.exit(1)
        if self.GetParamValue2('help') or self.GetParamValue2('h'):
            print(self.Usage)
            sys.exit(0)
    <mask token>

    def SetLogs(self, Logs):
        self.Logs = Logs

    def GetArgument(self, arg):
        if arg in self.args:
            return True

    def GetParamValue(self, parameter):
        return_code = False
        for opt, arg in self.opts:
            if opt == parameter:
                if arg == '':
                    return_code = True
                else:
                    return_code = arg
        return return_code

    def GetParamValue2(self, p):
        simple = '-{}'.format(p)
        double = '--{}'.format(p)
        for opt, arg in self.opts:
            if simple == opt or double == opt:
                if arg == '':
                    return True
                else:
                    return arg
        return False

    def NetRequested(self):
        if self.GetParamValue2('netgamename') != False or self.GetParamValue2(
            'localplayers') != False:
            return True
        else:
            return False

    def NetGameName(self):
        if self.GetParamValue2('netgamename') != False:
            self.GameName = self.GetParamValue2('netgamename')
        else:
            return False
        return self.GameName

    def GetNetServer(self):
        self.NetServer = self.GetParamValue2('servername')
        return self.NetServer
    <mask token>

    def GetNetPort(self):
        self.NetPort = self.GetParamValue2('serverport')
        return self.NetPort

    def get_ip_address(self, ifname):
        ifaces = ni.interfaces()
        self.Logs.Log('DEBUG', 'Found thoses interfaces : {}'.format(ifaces))
        try:
            self.Logs.Log('DEBUG',
                'Searching address matching interface name : {}'.format(ifname)
                )
            ipaddress = ni.ifaddresses(ifname)[AF_INET][0]['addr']
            return ipaddress
        except Exception as e:
            self.Logs.Log('WARNING',
                'Ip lookup from interface name ({}) failed. Trying from IP.'
                .format(ifname))
            for iface in ifaces:
                try:
                    ipaddress = ni.ifaddresses(iface)[AF_INET][0]['addr']
                except:
                    ipaddress = None
                if ipaddress == ifname:
                    return ipaddress
        return None

<mask token>
try:
    from netifaces import AF_INET, AF_INET6
    import netifaces as ni
except:
    print(
        '[FATAL] Please install required "netiface" package. Refer to wiki if required.'
        )


class CArgs:

    def __init__(self):
        self.NetServer = None
        self.NetPort = None
        self.GameName = None
        self.NetAlias = None
        self.Usage = '--- Pydarts Help Page ---\n'
        self.Usage += '\n[ Main options ]\n'
        self.Usage += '--help | -h\t\tprint this help page\n'
        self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
        self.Usage += """--debuglevel=		verbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors
"""
        self.Usage += (
            '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
            )
        self.Usage += (
            '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n')
        self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
        self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
        self.Usage += """--config=		use an alternate config file (relative to your $HOME/.pydarts config folder)
"""
        self.Usage += """--scoreonlogo		display score on logo instead of displaying it on the middle of the screen
"""
        self.Usage += '\n[ Network options ]\n'
        self.Usage += '--netgamename=\t\tpreset network game name\n'
        self.Usage += (
            '--servername=\t\tpreset network game server or ip address\n')
        self.Usage += """--serveralias=		preset network server alias (usefull to send to master server a public IP / behind NAT)
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Servers options ]\n'
        self.Usage += """--listen=		listening interface (server & master server only). Default is eth0.
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Advanced options ]\n'
        self.Usage += """--locale=		Select pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)
"""
        self.Usage += '--bypass-stats\t\tbypass stats updates\n'
        self.Usage += '--noserial\t\tbypass serial connexion test\n'
        self.Usage += '--serialport=\t\tspecify serial port to use\n'
        self.Usage += '--serialspeed=\t\tspecify serial speed\n'
        self.Usage += (
            '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
            )
        self.Usage += (
            '--animationduration=\tduration for every round animation (ms default:5)\n'
            )
        self.Usage += """--forcecalibration=	force board calibration even if config file already exists
"""
        self.Usage += """** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):
"""
        self.Usage += """--gametype=		preset game type : only "local" is supported for now. No default.
"""
        self.Usage += (
            '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
            )
        self.Usage += """--localplayers=		preset players name, comma separated, lower case. No Default.
"""
        self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'
        self.shortopts = 'hN:S:P:o:a:'
        self.longopts = ['serveralias=', 'localplayers=', 'selectedgame=',
            'gametype=', 'masterserver=', 'masterport=', 'serverport=',
            'servername=', 'netgamename=', 'locale=', 'debuglevel=',
            'soundvolume=', 'solo=', 'colorset=', 'resx=', 'resy=',
            'listen=', 'config=', 'blinktime=', 'releasedartstime=',
            'nbcol=', 'espeakpath=', 'serialspeed=', 'serialport=',
            'animationduration=', 'noserial', 'help', 'bypass-stats',
            'fullscreen', 'mastertest', 'scoreonlogo', 'forcecalibration']
        try:
            self.opts, self.args = getopt.getopt(sys.argv[1:], self.
                shortopts, self.longopts)
        except getopt.GetoptError as err:
            print(str(err))
            sys.exit(1)
        if self.GetParamValue2('help') or self.GetParamValue2('h'):
            print(self.Usage)
            sys.exit(0)

    def GetGameType(self, gametype):
        if str(gametype) in 'local':
            return gametype
        else:
            return False

    def SetLogs(self, Logs):
        self.Logs = Logs

    def GetArgument(self, arg):
        if arg in self.args:
            return True

    def GetParamValue(self, parameter):
        return_code = False
        for opt, arg in self.opts:
            if opt == parameter:
                if arg == '':
                    return_code = True
                else:
                    return_code = arg
        return return_code

    def GetParamValue2(self, p):
        simple = '-{}'.format(p)
        double = '--{}'.format(p)
        for opt, arg in self.opts:
            if simple == opt or double == opt:
                if arg == '':
                    return True
                else:
                    return arg
        return False

    def NetRequested(self):
        if self.GetParamValue2('netgamename') != False or self.GetParamValue2(
            'localplayers') != False:
            return True
        else:
            return False

    def NetGameName(self):
        if self.GetParamValue2('netgamename') != False:
            self.GameName = self.GetParamValue2('netgamename')
        else:
            return False
        return self.GameName

    def GetNetServer(self):
        self.NetServer = self.GetParamValue2('servername')
        return self.NetServer

    def GetNetAlias(self):
        if self.GetParamValue2('serveralias'):
            self.NetAlias = self.GetParamValue2('serveralias')
        elif self.GetParamValue2('servername'):
            self.NetAlias = self.GetParamValue2('servername')
        else:
            return False
        return self.NetAlias

    def GetNetPort(self):
        self.NetPort = self.GetParamValue2('serverport')
        return self.NetPort

    def get_ip_address(self, ifname):
        ifaces = ni.interfaces()
        self.Logs.Log('DEBUG', 'Found thoses interfaces : {}'.format(ifaces))
        try:
            self.Logs.Log('DEBUG',
                'Searching address matching interface name : {}'.format(ifname)
                )
            ipaddress = ni.ifaddresses(ifname)[AF_INET][0]['addr']
            return ipaddress
        except Exception as e:
            self.Logs.Log('WARNING',
                'Ip lookup from interface name ({}) failed. Trying from IP.'
                .format(ifname))
            for iface in ifaces:
                try:
                    ipaddress = ni.ifaddresses(iface)[AF_INET][0]['addr']
                except:
                    ipaddress = None
                if ipaddress == ifname:
                    return ipaddress
        return None

import sys, getopt, struct
try:
    from netifaces import AF_INET, AF_INET6
    import netifaces as ni
except:
    print(
        '[FATAL] Please install required "netiface" package. Refer to wiki if required.'
        )


class CArgs:

    def __init__(self):
        self.NetServer = None
        self.NetPort = None
        self.GameName = None
        self.NetAlias = None
        self.Usage = '--- Pydarts Help Page ---\n'
        self.Usage += '\n[ Main options ]\n'
        self.Usage += '--help | -h\t\tprint this help page\n'
        self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
        self.Usage += """--debuglevel=		verbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors
"""
        self.Usage += (
            '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
            )
        self.Usage += (
            '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n')
        self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
        self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
        self.Usage += """--config=		use an alternate config file (relative to your $HOME/.pydarts config folder)
"""
        self.Usage += """--scoreonlogo		display score on logo instead of displaying it on the middle of the screen
"""
        self.Usage += '\n[ Network options ]\n'
        self.Usage += '--netgamename=\t\tpreset network game name\n'
        self.Usage += (
            '--servername=\t\tpreset network game server or ip address\n')
        self.Usage += """--serveralias=		preset network server alias (usefull to send to master server a public IP / behind NAT)
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Servers options ]\n'
        self.Usage += """--listen=		listening interface (server & master server only). Default is eth0.
"""
        self.Usage += (
            '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
            )
        self.Usage += (
            '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
            )
        self.Usage += '\n[ Advanced options ]\n'
        self.Usage += """--locale=		Select pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)
"""
        self.Usage += '--bypass-stats\t\tbypass stats updates\n'
        self.Usage += '--noserial\t\tbypass serial connexion test\n'
        self.Usage += '--serialport=\t\tspecify serial port to use\n'
        self.Usage += '--serialspeed=\t\tspecify serial speed\n'
        self.Usage += (
            '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
            )
        self.Usage += (
            '--animationduration=\tduration for every round animation (ms default:5)\n'
            )
        self.Usage += """--forcecalibration=	force board calibration even if config file already exists
"""
        self.Usage += """** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):
"""
        self.Usage += """--gametype=		preset game type : only "local" is supported for now. No default.
"""
        self.Usage += (
            '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
            )
        self.Usage += """--localplayers=		preset players name, comma separated, lower case. No Default.
"""
        self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'
        self.shortopts = 'hN:S:P:o:a:'
        self.longopts = ['serveralias=', 'localplayers=', 'selectedgame=',
            'gametype=', 'masterserver=', 'masterport=', 'serverport=',
            'servername=', 'netgamename=', 'locale=', 'debuglevel=',
            'soundvolume=', 'solo=', 'colorset=', 'resx=', 'resy=',
            'listen=', 'config=', 'blinktime=', 'releasedartstime=',
            'nbcol=', 'espeakpath=', 'serialspeed=', 'serialport=',
            'animationduration=', 'noserial', 'help', 'bypass-stats',
            'fullscreen', 'mastertest', 'scoreonlogo', 'forcecalibration']
        try:
            self.opts, self.args = getopt.getopt(sys.argv[1:], self.
                shortopts, self.longopts)
        except getopt.GetoptError as err:
            print(str(err))
            sys.exit(1)
        if self.GetParamValue2('help') or self.GetParamValue2('h'):
            print(self.Usage)
            sys.exit(0)

    def GetGameType(self, gametype):
        if str(gametype) in 'local':
            return gametype
        else:
            return False

    def SetLogs(self, Logs):
        self.Logs = Logs

    def GetArgument(self, arg):
        if arg in self.args:
            return True

    def GetParamValue(self, parameter):
        return_code = False
        for opt, arg in self.opts:
            if opt == parameter:
                if arg == '':
                    return_code = True
                else:
                    return_code = arg
        return return_code

    def GetParamValue2(self, p):
        simple = '-{}'.format(p)
        double = '--{}'.format(p)
        for opt, arg in self.opts:
            if simple == opt or double == opt:
                if arg == '':
                    return True
                else:
                    return arg
        return False

    def NetRequested(self):
        if self.GetParamValue2('netgamename') != False or self.GetParamValue2(
            'localplayers') != False:
            return True
        else:
            return False

    def NetGameName(self):
        if self.GetParamValue2('netgamename') != False:
            self.GameName = self.GetParamValue2('netgamename')
        else:
            return False
        return self.GameName

    def GetNetServer(self):
        self.NetServer = self.GetParamValue2('servername')
        return self.NetServer

    def GetNetAlias(self):
        if self.GetParamValue2('serveralias'):
            self.NetAlias = self.GetParamValue2('serveralias')
        elif self.GetParamValue2('servername'):
            self.NetAlias = self.GetParamValue2('servername')
        else:
            return False
        return self.NetAlias

    def GetNetPort(self):
        self.NetPort = self.GetParamValue2('serverport')
        return self.NetPort

    def get_ip_address(self, ifname):
        ifaces = ni.interfaces()
        self.Logs.Log('DEBUG', 'Found thoses interfaces : {}'.format(ifaces))
        try:
            self.Logs.Log('DEBUG',
                'Searching address matching interface name : {}'.format(ifname)
                )
            ipaddress = ni.ifaddresses(ifname)[AF_INET][0]['addr']
            return ipaddress
        except Exception as e:
            self.Logs.Log('WARNING',
                'Ip lookup from interface name ({}) failed. Trying from IP.'
                .format(ifname))
            for iface in ifaces:
                try:
                    ipaddress = ni.ifaddresses(iface)[AF_INET][0]['addr']
                except:
                    ipaddress = None
                if ipaddress == ifname:
                    return ipaddress
        return None

# To use exit...
import sys,getopt,struct
try:
   from netifaces import AF_INET, AF_INET6
   import netifaces as ni
except:
   print("[FATAL] Please install required \"netiface\" package. Refer to wiki if required.")

class CArgs:
   def __init__(self):
      # Init
      self.NetServer=None
      self.NetPort=None
      self.GameName=None
      self.NetAlias=None
      #
      # Usage Message
      #
      self.Usage = '--- Pydarts Help Page ---\n'
      self.Usage += '\n[ Main options ]\n'
      self.Usage += '--help | -h\t\tprint this help page\n'
      self.Usage += '--soundvolume=\t\tset sound volume (%)\n'
      self.Usage += '--debuglevel=\t\tverbosity level (1-4) 1:Debug 2:Warnings 3:Errors 4:Fatal errors\n'
      self.Usage += '--solo=\t\t\ttime between players (s) (0 means you need to push PLAYER)\n'
      self.Usage += '--fullscreen\t\tenable fullscreen (bypass resx and resy)\n'
      self.Usage += '--resx=\t\t\tX resolution (pixels)\n'
      self.Usage += '--resy=\t\t\tY resolution (pixels)\n'
      self.Usage += '--config=\t\tuse an alternate config file (relative to your $HOME/.pydarts config folder)\n'
      self.Usage += '--scoreonlogo\t\tdisplay score on logo instead of displaying it on the middle of the screen\n'
      self.Usage += '\n[ Network options ]\n'
      self.Usage += '--netgamename=\t\tpreset network game name\n'
      self.Usage += '--servername=\t\tpreset network game server or ip address\n'
      self.Usage += '--serveralias=\t\tpreset network server alias (usefull to send to master server a public IP / behind NAT)\n'
      self.Usage += '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
      self.Usage += '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
      self.Usage += '\n[ Servers options ]\n'
      self.Usage += '--listen=\t\tlistening interface (server & master server only). Default is eth0.\n'
      self.Usage += '--serverport=\t\tlisten or join server on that port. (Default 5005).\n'
      self.Usage += '--masterport=\t\tlisten or join master server on that port (default 5006)\n'
      self.Usage += '\n[ Advanced options ]\n'
      self.Usage += '--locale=\t\tSelect pydarts interface language (en_GB, fr_FR, es_ES, de_DE... more languages soon !)\n'      
      self.Usage += '--bypass-stats\t\tbypass stats updates\n'
      self.Usage += '--noserial\t\tbypass serial connexion test\n'
      self.Usage += '--serialport=\t\tspecify serial port to use\n'
      self.Usage += '--serialspeed=\t\tspecify serial speed\n'
      self.Usage += '--mastertest\t\tcreate fake games in MasterServer (test purpose)\n'
      self.Usage += '--animationduration=\tduration for every round animation (ms default:5)\n'
      self.Usage += '--forcecalibration=\tforce board calibration even if config file already exists\n'
      self.Usage += '** Usage of Direct-Play Mode (play without using menus - please refer to the wiki):\n'
      self.Usage += '--gametype=\t\tpreset game type : only "local" is supported for now. No default.\n'
      self.Usage += '--selectedgame=\t\tpreset game selection (case sensitive). No default.\n'
      self.Usage += '--localplayers=\t\tpreset players name, comma separated, lower case. No Default.\n'
      self.Usage += '\t\t\texemple : --localplayers=foo,bar\n'


      # LIST HERE Option available for CLI
      self.shortopts='hN:S:P:o:a:'
      self.longopts=[      'serveralias=',
                           'localplayers=',
                           'selectedgame=',
                           'gametype=',
                           'masterserver=',
                           'masterport=',
                           'serverport=',
                           'servername=',
                           'netgamename=',
                           'locale=',
                           'debuglevel=',
                           'soundvolume=',
                           'solo=',
                           'colorset=',
                           'resx=',
                           'resy=',
                           'listen=',
                           'config=',
                           'blinktime=',
                           'releasedartstime=',
                           'nbcol=',
                           'espeakpath=',
                           'serialspeed=',
                           'serialport=',
                           'animationduration=',
                           'noserial',
                           'help',
                           'bypass-stats',
                           'fullscreen',
                           'mastertest',
                           'scoreonlogo',
                           'forcecalibration'
                     ]
      #
      #debug - print sys.argv
      try:
         self.opts,self.args = getopt.getopt(sys.argv[1:],self.shortopts,self.longopts) # Option waiting for a value are followed by a :
      except getopt.GetoptError as err: # Print a clean message in case of unrecognized option
         print(str(err)) # will print something like "option -a not recognized"
         sys.exit(1)
      if self.GetParamValue2('help') or self.GetParamValue2('h'):
         print(self.Usage)
         sys.exit(0)

#
# To filter game type
#

   def GetGameType(self,gametype):
      if str(gametype) in ('local'):#For now, only local is allowed by command line
         return gametype
      else:
         return False
#
# To give Logs objects after creation
#

   def SetLogs(self,Logs):
      self.Logs=Logs

#
# Return true if an arguement is present
#
   def GetArgument(self,arg):
      #print self.args
      if arg in self.args:
         return True
#
# Return the value of a parameter, true if the parameter has no value, false otherwise
#
   def GetParamValue(self,parameter):
      return_code = False
      for opt,arg in self.opts:
         if opt == parameter:
            if arg=='':
               return_code = True
            else:
               return_code = arg
      return return_code

#
# New way to get couple param/value
#
   def GetParamValue2(self,p):
      simple="-{}".format(p)
      double="--{}".format(p)
      for opt,arg in self.opts:
         if simple==opt or double==opt:
            if arg=='':
               return True
            else:
               return arg
      return False

#
# Give Network Status
#
   def NetRequested(self):
      if self.GetParamValue2('netgamename') != False or self.GetParamValue2('localplayers') != False:
         return True
      else:
         return False

#
# Give name of requested net game
#
   def NetGameName(self):
      if self.GetParamValue2('netgamename') != False:
         self.GameName=self.GetParamValue2('netgamename')
      else:
         return False
      #print(self.GameName)
      return self.GameName
         
#
# Return Network Server
#
   def GetNetServer(self):
      self.NetServer=self.GetParamValue2('servername')
      return self.NetServer
      
#
# Return Network Server
#
   def GetNetAlias(self):
      if self.GetParamValue2('serveralias'):
         self.NetAlias=self.GetParamValue2('serveralias')
      elif self.GetParamValue2('servername'):
         self.NetAlias=self.GetParamValue2('servername')
      else:
         return False
      return self.NetAlias

#
# Return Network Server port
#
   def GetNetPort(self):
      self.NetPort=self.GetParamValue2('serverport')
      return self.NetPort


#######################
# SERVERS METHODS
#######################

#
# Get ip address from either interface name or ipaddress (search if an ip match on system)
#
   def get_ip_address(self,ifname):
      # Interfaces lookup
      ifaces = ni.interfaces()
      # Print result
      self.Logs.Log("DEBUG","Found thoses interfaces : {}".format(ifaces))
      # Try to find ipaddress from interface name
      try:
         self.Logs.Log("DEBUG","Searching address matching interface name : {}".format(ifname))
         ipaddress = ni.ifaddresses(ifname)[AF_INET][0]['addr']
         # return the ip associated if lookup if a success
         return ipaddress
      # If it fails
      except Exception as e:
         # Print warning
         self.Logs.Log("WARNING","Ip lookup from interface name ({}) failed. Trying from IP.".format(ifname))
         # In case of IP adress given as interface
         for iface in ifaces:
            # Iter all ifaces
            try:
               # Get IP from iface
               ipaddress=ni.ifaddresses(iface)[AF_INET][0]['addr']
            # if it fails print error
            except:
               ipaddress=None
            # Compare ip address to arg given, if it match, so the argument was an ip address
            if ipaddress==ifname:
               # Return IP given so it can use it to listen
               return ipaddress
      # If everything failed, return None
      return None
