1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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/>.
22 from argparse import (ArgumentParser, SUPPRESS)
25 from .utils import Wfile, Rfile, Wdir
28 MPD_sima automagicaly queue new tracks in MPD playlist.
29 Command line options override their equivalent in configuration file."""
32 def clean_dict(to_clean):
33 """Remove items which values are set to None/False"""
34 for k in list(to_clean.keys()):
35 if not to_clean.get(k):
40 # pop out 'sw' value before creating Parser object.
42 # If an option has to override its dual in conf file, the destination
43 # identifier "dest" is to be named after that option in the conf file.
44 # The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
45 # is looking for command line option names identical to config file option
46 # name it is meant to override.
53 'help': 'file to log message to, default is stdout/stderr'},
55 'sw':['-v', '--log-level'],
58 'choices': ['debug', 'info', 'warning', 'error'],
59 'help': 'Log messages verbosity, default is info'},
61 'sw': ['-p', '--pid'],
64 'help': 'file to save PID to, default is not to store pid'},
66 'sw': ['-d', '--daemon'],
68 'action': 'store_true',
69 'help': 'Daemonize process.'},
71 'sw': ['-S', '--host'],
73 'help': 'Host MPD in running on (IP or FQDN)'},
75 'sw': ['-P', '--port'],
78 'help': 'Port MPD in listening on'},
80 'sw':['-c', '--config'],
83 'help': 'Configuration file to load'},
85 'sw':['--generate-config'],
86 'dest': 'generate_config',
87 'action': 'store_true',
88 'help': 'Generate a sample configuration file to stdout according to the current\
89 configuration. You can put other options with this one to get them in\
90 the generated configuration.'},
92 'sw':['--var-dir', '--var_dir'],
95 'help': 'Directory to store var content (ie. database, cache)'},
97 'sw': ['--create-db'],
98 'action': 'store_true',
100 'help': '''Create database and exit, use destination
101 specified in --var-dir or standard location.'''},
103 'sw':['--queue-mode', '-q'],
104 'dest': 'queue_mode',
105 'choices': ['track', 'top', 'album'],
106 #'help': 'Queue mode in [track, top, album]',
109 'sw':['--purge-history'],
110 'action': 'store_true',
111 'dest': 'do_purge_history',
117 """Command line management.
120 def __init__(self, script_info,):
122 self.info = dict(script_info)
123 self.options = dict()
126 def declare_opts(self):
128 Declare options in ArgumentParser object.
130 self.parser = ArgumentParser(description=DESCRIPTION,
131 prog=self.info.get('prog'),
132 epilog='Happy Listening',
134 self.parser.add_argument('--version', action='version',
135 version='%(prog)s {version}'.format(**self.info))
136 # Add all options declare in OPTS
138 opt_names = opt.pop('sw')
139 self.parser.add_argument(*opt_names, **opt)
143 Look for env. var and parse command line.
146 options = vars(self.parser.parse_args())
147 # Set log file to os.devnull in daemon mode to avoid logging to
149 # TODO: Probably useless. To be checked
150 #if options.__dict__.get('daemon', False) and \
151 # not options.__dict__.get('logfile', False):
152 # options.__dict__['logfile'] = devnull
153 self.options.update(options)
154 clean_dict(self.options)
158 # vim: ai ts=4 sw=4 sts=4 expandtab