Add --config cli option

This commit is contained in:
Thomas Scholtes 2014-01-29 23:38:05 +01:00
parent e628e33d78
commit d75f6667e1
5 changed files with 26 additions and 0 deletions

View file

@ -767,9 +767,15 @@ def _raw_main(args):
help="destination music directory")
parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
help='print debugging information')
parser.add_option('-c', '--config', dest='config',
help='path to configuration file')
# Parse the command-line!
options, subcommand, suboptions, subargs = parser.parse_args(args)
if hasattr(options, 'config') and options.config != None:
config_path = options.config
del options.config
config.add_file(config_path)
config.set_args(options)
# Open library file.

View file

@ -687,6 +687,11 @@ class Configuration(RootView):
os.makedirs(appdir)
return appdir
def add_file(self, filename):
"""Parses the file as YAML and adds it to the configuration sources
"""
self.add(ConfigSource(load_yaml(filename), filename))
class LazyConfig(Configuration):
"""A Configuration at reads files on demand when it is first
accessed. This is appropriate for using as a global config object at

View file

@ -301,6 +301,7 @@ import ...``.
* ``-d DIRECTORY``: specify the library root directory.
* ``-v``: verbose mode; prints out a deluge of debugging information. Please use
this flag when reporting bugs.
* ``-c /path/to/file``: read the configuration from this file.
.. only:: man

View file

@ -13,6 +13,9 @@ every aspect of its operation. To configure beets, you'll edit a file called
* If you prefer a different location, set the ``BEETSDIR`` environment variable
to a path; beets will then look for a ``config.yaml`` in that directory.
Additionally, you can specify a different configuration file to load by adding
the ``--config /path/to/file`` option to the beets command line.
The config file uses `YAML`_ syntax. You can use the full power of YAML, but
most configuration options are simple key/value pairs. This means your config
file will look like this::

View file

@ -472,6 +472,7 @@ class ConfigTest(_common.TestCase):
super(ConfigTest, self).setUp()
self.io.install()
self.test_cmd = ui.Subcommand('test', help='test')
self.test_cmd.func = lambda *_: None
commands.default_commands.append(self.test_cmd)
def tearDown(self):
super(ConfigTest, self).tearDown()
@ -516,6 +517,16 @@ class ConfigTest(_common.TestCase):
library: /xxx/yyy/not/a/real/path
""", func)
def test_cli_config_option(self):
"""Read config from file passed on the command line with
``--config file``
"""
config_path = os.path.join(self.temp_dir, 'config.yml')
with open(config_path, 'w') as file:
file.write('anoption: value')
ui.main(['--config', config_path, 'test'])
self.assertEqual(config['anoption'].get(), 'value')
def test_replacements_parsed(self):
def func(lib, opts, args):
replacements = lib.replacements