1772
1772
class cmd_config(commands.Command):
1773
1773
__doc__ = """Display, set or remove a configuration option.
1775
Display the MATCHING configuration options mentioning their scope (the
1776
configuration file they are defined in). The active value that bzr will
1777
take into account is the first one displayed.
1775
Display the active value for a given option.
1777
If --all is specified, NAME is interpreted as a regular expression and all
1778
matching options are displayed mentioning their scope. The active value
1779
that bzr will take into account is the first one displayed for each option.
1781
If no NAME is given, --all .* is implied.
1779
1783
Setting a value is achieved by using name=value without spaces. The value
1780
1784
is set in the most relevant scope and can be checked by displaying the
1785
takes_args = ['matching?']
1788
takes_args = ['name?']
1787
1790
takes_options = [
1791
1794
commands.Option('scope', help='Reduce the scope to the specified'
1792
1795
' configuration file',
1797
commands.Option('all',
1798
help='Display all the defined values for the matching options.',
1794
1800
commands.Option('remove', help='Remove the option from'
1795
1801
' the configuration file'),
1798
1804
@commands.display_command
1799
def run(self, matching=None, directory=None, scope=None, remove=False):
1805
def run(self, name=None, all=False, directory=None, scope=None,
1800
1807
if directory is None:
1801
1808
directory = '.'
1802
1809
directory = urlutils.normalize_url(directory)
1803
if matching is None:
1804
self._show_config('*', directory)
1811
raise errors.BzrError(
1812
'--all and --remove are mutually exclusive.')
1814
# Delete the option in the given scope
1815
self._remove_config_option(name, directory, scope)
1817
# Defaults to all options
1818
self._show_matching_options('.*', directory, scope)
1807
self._remove_config_option(matching, directory, scope)
1821
name, value = name.split('=', 1)
1823
# Display the option(s) value(s)
1825
self._show_matching_options(name, directory, scope)
1827
self._show_value(name, directory, scope)
1809
pos = matching.find('=')
1811
self._show_config(matching, directory)
1813
self._set_config_option(matching[:pos], matching[pos+1:],
1830
raise errors.BzrError(
1831
'Only one option can be set.')
1832
# Set the option value
1833
self._set_config_option(name, value, directory, scope)
1816
1835
def _get_configs(self, directory, scope=None):
1817
1836
"""Iterate the configurations specified by ``directory`` and ``scope``.
1838
1857
yield LocationConfig(directory)
1839
1858
yield GlobalConfig()
1841
def _show_config(self, matching, directory):
1842
# Turn the glob into a regexp
1843
matching_re = re.compile(fnmatch.translate(matching))
1860
def _show_value(self, name, directory, scope):
1862
for c in self._get_configs(directory, scope):
1865
for (oname, value, section, conf_id) in c._get_options():
1867
# Display only the first value and exit
1868
self.outf.write('%s\n' % (value))
1872
raise errors.NoSuchConfigOption(name)
1874
def _show_matching_options(self, name, directory, scope):
1875
name = re.compile(name)
1876
# We want any error in the regexp to be raised *now* so we need to
1877
# avoid the delay introduced by the lazy regexp.
1878
name._compile_and_collapse()
1844
1879
cur_conf_id = None
1845
for c in self._get_configs(directory):
1846
for (name, value, section, conf_id) in c._get_options():
1847
if matching_re.search(name):
1880
for c in self._get_configs(directory, scope):
1881
for (oname, value, section, conf_id) in c._get_options():
1882
if name.search(oname):
1848
1883
if cur_conf_id != conf_id:
1884
# Explain where the options are defined
1849
1885
self.outf.write('%s:\n' % (conf_id,))
1850
1886
cur_conf_id = conf_id
1851
self.outf.write(' %s = %s\n' % (name, value))
1887
self.outf.write(' %s = %s\n' % (oname, value))
1853
1889
def _set_config_option(self, name, value, directory, scope):
1854
1890
for conf in self._get_configs(directory, scope):
1858
1894
raise errors.NoSuchConfig(scope)
1860
1896
def _remove_config_option(self, name, directory, scope):
1898
raise errors.BzrCommandError(
1899
'--remove expects an option to remove.')
1861
1900
removed = False
1862
1901
for conf in self._get_configs(directory, scope):
1863
1902
for (section_name, section, conf_id) in conf._get_sections():