1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009, 2010, 2011, 2012, 2013 Jack Kaliko <kaliko@azylum.org>
5 # This file is part of sima
7 # sima is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # sima is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with sima. If not, see <http://www.gnu.org/licenses/>.
23 from argparse import (ArgumentParser, SUPPRESS)
26 from .utils import Obsolete, Wfile, Rfile, Wdir
28 USAGE = """USAGE: %prog [--help] [options]"""
30 sima automagicaly queue new tracks in MPD playlist.
31 All command line options will override their equivalent in configuration
36 def clean_dict(to_clean):
37 """Remove items which values are set to None/False"""
38 for k in list(to_clean.keys()):
39 if not to_clean.get(k):
44 # pop out 'sw' value before creating Parser object.
46 # If an option has to override its dual in conf file, the destination
47 # identifier "dest" is to be named after that option in the conf file.
48 # The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
49 # is looking for command line option names identical to config file option
50 # name it is meant to override.
57 'help': 'file to log message to, default is stdout/stderr'},
59 'sw':['-v', '--log-level'],
62 'choices': ['debug', 'info', 'warning', 'error'],
63 'help': 'file to log message to, default is stdout/stderr'},
65 'sw': ['-p', '--pid'],
68 'help': 'file to save PID to, default is not to store pid'},
70 'sw': ['-d', '--daemon'],
72 'action': 'store_true',
73 'help': 'Daemonize process.'},
75 'sw': ['-S', '--host'],
77 'help': 'Host MPD in running on (IP or FQDN)'},
79 'sw': ['-P', '--port'],
82 'help': 'Port MPD in listening on'},
84 'sw':['-c', '--config'],
87 'help': 'Configuration file to load'},
92 'help': 'Directory to store var content (ie. database)'},
94 'sw': ['--create-db'],
95 'action': 'store_true',
97 'help': '''Create database and exit, use destination
98 specified in --var_dir or standard location.'''},
100 'sw':['--queue-mode', '-q'],
101 'dest': 'queue_mode',
102 'choices': ['track', 'top', 'album'],
103 #'help': 'Queue mode in [track, top, album]',
106 'sw':['--purge_history'],
107 'action': 'store_true',
108 'dest': 'do_purge_history',
113 class StartOpt(object):
114 """Command line management.
117 def __init__(self, script_info,):
118 self.info = dict(script_info)
119 self.options = dict()
122 def declare_opts(self):
124 Declare options in ArgumentParser object.
126 self.parser = ArgumentParser(description=DESCRIPTION,
127 usage='%(prog)s [options]',
129 epilog='Happy Listening',
131 self.parser.add_argument('--version', action='version',
132 version='%(prog)s {version}'.format(**self.info))
133 # Add all options declare in OPTS
135 opt_names = opt.pop('sw')
136 self.parser.add_argument(*opt_names, **opt)
140 Look for env. var and parse command line.
143 options = vars(self.parser.parse_args())
144 # Set log file to os.devnull in daemon mode to avoid logging to
146 # TODO: Probably useless. To be checked
147 #if options.__dict__.get('daemon', False) and \
148 # not options.__dict__.get('logfile', False):
149 # options.__dict__['logfile'] = devnull
150 self.options.update(options)
151 clean_dict(self.options)
155 # vim: ai ts=4 sw=4 sts=4 expandtab