]> kaliko git repositories - mpd-goodies.git/blobdiff - lib/startop.py
* Enhanced description for MPD host and PORT defaults values
[mpd-goodies.git] / lib / startop.py
index 0ede89a20d0b2900564626796903ba459973763d..b73160b3aaf742656d8985efa96ccafce3b28611 100644 (file)
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+from locale import getpreferredencoding
 from optparse import (OptionParser, OptionValueError, SUPPRESS_HELP)
 from os import environ
 
@@ -11,12 +12,12 @@ OPTS = list([
         'sw': ['-S', '--hostname'],
         'type': 'string',
         'dest': 'host',
-        'help': 'Hostname MPD in running on'},
+        'help': 'Hostname MPD in running on (default: localhost or MPD_HOST if set)'},
     {
         'sw': ['-P', '--port'],
         'type': 'int',
         'dest': 'port',
-        'help': 'Port MPD in listening on'},
+        'help': 'Port MPD in listening on (default: 6600 or MPD_PORT if set)'},
 ])
 #}}}
 
@@ -51,14 +52,27 @@ class StartOpt(object):
     """
     """
 
-    def __init__(self, script_info, options):#{{{
+    def __init__(self, script_info, child_options):#{{{
+        # Strong assumption?
+        self.localencoding = 'utf8'
         self.parser = None
         self.cli_options = dict({})
+        # TODO: dict ?!!? isn't it a list instead?
         self.cli_args = dict({})
         self.info = dict(script_info)
-        self.options = list(options + OPTS)
+        # options allows to add new cli options within child objects calling
+        # parent __init__()
+        self.options = list(child_options + OPTS)
         self.main()#}}}
 
+    def _get_encoding(self):#{{{
+        """Get local encoding"""
+        self.localencoding = getpreferredencoding()#}}}
+
+    def _u8_convert(self, string):
+        """Convert CLI input string to UTF8 (mpd standart)"""
+        return unicode(string, self.localencoding).encode('UTF-8')
+
     def declare_opts(self):#{{{
         """
         Declare options in OptionParser object.
@@ -66,8 +80,12 @@ class StartOpt(object):
         version = self.info.get('version')
         prog = self.info.get('prog_name')
         des = self.info.get('description')
+        if 'usage' in self.info:
+            usage = self.info.get('usage')
+        else:
+            usage = USAGE
         self.parser = OptionParser(version=version,
-                                   usage=USAGE,
+                                   usage=usage,
                                    prog=prog,
                                    description=des)
         con_id, passwd = get_mpd_environ()
@@ -85,6 +103,7 @@ class StartOpt(object):
     def main(self):
         """declare options, parse command line"""
         self.declare_opts()
+        self._get_encoding()
         (self.cli_options, self.cli_args) = self.parser.parse_args()