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, options):#{{{
56 self.cli_options = dict({})
57 self.cli_args = dict({})
58 self.info = dict(script_info)
59 self.options = list(options + OPTS)
62 def declare_opts(self):#{{{
64 Declare options in OptionParser object.
66 version = self.info.get('version')
67 prog = self.info.get('prog_name')
68 des = self.info.get('description')
69 self.parser = OptionParser(version=version,
73 con_id, passwd = get_mpd_environ()
74 # Add all options declare in OPTS
75 for opt in self.options:
76 opt_names = opt.pop('sw')
77 self.parser.add_option(*opt_names, **opt)
78 #set defaults for host/port according to env var
79 dest = opt.get('dest')
81 self.parser.set_defaults(host=con_id.get('host'))
83 self.parser.set_defaults(port=con_id.get('port'))#}}}
86 """declare options, parse command line"""
88 (self.cli_options, self.cli_args) = self.parser.parse_args()
95 if __name__ == '__main__':
99 # vim: ai ts=4 sw=4 sts=4 expandtab