1 # -*- coding: utf-8 -*-
3 from optparse import (OptionParser, OptionValueError, SUPPRESS_HELP)
7 # pop out 'sw' value before creating OptionParser object.
11 'sw': ['-S', '--hostname'],
14 'help': 'Hostname MPD in running on'},
16 'sw': ['-P', '--port'],
19 'help': 'Port MPD in listening on'},
23 USAGE = u"""USAGE: %prog [--help] [options]"""
26 def get_mpd_environ():#{{{
28 Retrieve MPD env. var.
30 con_id = dict({'host': 'localhost',
33 mpd_host_env = environ.get('MPD_HOST')
34 mpd_port = environ.get('MPD_PORT')
37 # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
38 mpd_host_env = mpd_host_env.split('@')
39 mpd_host_env.reverse()
40 if len(mpd_host_env[0]) > 0:
41 con_id.update({'host': mpd_host_env[0]})
42 if len(mpd_host_env) > 1:
43 print 'passwd set in MPD_HOST'
44 passwd = mpd_host_env[1]
46 con_id.update({'port': int(mpd_port)})
47 return (con_id, passwd)#}}}
50 class StartOpt(object):
54 def __init__(self, script_info, child_options):#{{{
56 self.cli_options = dict({})
57 self.cli_args = dict({})
58 self.info = dict(script_info)
59 # options allows to add new cli options within child objects calling
61 self.options = list(child_options + OPTS)
64 def declare_opts(self):#{{{
66 Declare options in OptionParser object.
68 version = self.info.get('version')
69 prog = self.info.get('prog_name')
70 des = self.info.get('description')
71 if 'usage' in self.info:
72 usage = self.info.get('usage')
75 self.parser = OptionParser(version=version,
79 con_id, passwd = get_mpd_environ()
80 # Add all options declare in OPTS
81 for opt in self.options:
82 opt_names = opt.pop('sw')
83 self.parser.add_option(*opt_names, **opt)
84 #set defaults for host/port according to env var
85 dest = opt.get('dest')
87 self.parser.set_defaults(host=con_id.get('host'))
89 self.parser.set_defaults(port=con_id.get('port'))#}}}
92 """declare options, parse command line"""
94 (self.cli_options, self.cli_args) = self.parser.parse_args()
101 if __name__ == '__main__':
105 # vim: ai ts=4 sw=4 sts=4 expandtab