diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 621f150ee..5ddfe64a6 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -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. diff --git a/beets/util/confit.py b/beets/util/confit.py index 4f4410c73..d94039f67 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -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 diff --git a/docs/reference/cli.rst b/docs/reference/cli.rst index a37faf389..e70572041 100644 --- a/docs/reference/cli.rst +++ b/docs/reference/cli.rst @@ -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 diff --git a/docs/reference/config.rst b/docs/reference/config.rst index c61d46784..fdf067974 100644 --- a/docs/reference/config.rst +++ b/docs/reference/config.rst @@ -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:: diff --git a/test/test_ui.py b/test/test_ui.py index 041021d80..40d767d0b 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -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