commands: config: add --list option

This prints all existing config options and their values.
It can be combined with the --system, --global, and --local options
to just list the contents of those files.

Signed-off-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Marti Bolivar 2019-05-24 21:11:30 -06:00
parent 79e7b5b3e1
commit 06683b791c
2 changed files with 56 additions and 2 deletions

View File

@ -46,6 +46,9 @@ To get a value for <name>, type:
To set a value for <name>, type: To set a value for <name>, type:
west config <name> <value> west config <name> <value>
To list all options and their values:
west config -l
''' '''
CONFIG_EPILOG = '''\ CONFIG_EPILOG = '''\
@ -90,6 +93,9 @@ class Config(WestCommand):
description=self.description, description=self.description,
epilog=CONFIG_EPILOG) epilog=CONFIG_EPILOG)
parser.add_argument('-l', '--list', action='store_true',
help='list all options and their values')
group = parser.add_argument_group( group = parser.add_argument_group(
'configuration file to use (give at most one)') 'configuration file to use (give at most one)')
group.add_argument('--system', dest='configfile', nargs=0, action=Once, group.add_argument('--system', dest='configfile', nargs=0, action=Once,
@ -99,7 +105,7 @@ class Config(WestCommand):
group.add_argument('--local', dest='configfile', nargs=0, action=Once, group.add_argument('--local', dest='configfile', nargs=0, action=Once,
help="this installation's file") help="this installation's file")
parser.add_argument('name', parser.add_argument('name', nargs='?',
help='''config option in section.key format; help='''config option in section.key format;
e.g. "foo.bar" is section "foo", key "bar"''') e.g. "foo.bar" is section "foo", key "bar"''')
parser.add_argument('value', nargs='?', help='value to set "name" to') parser.add_argument('value', nargs='?', help='value to set "name" to')
@ -107,11 +113,28 @@ class Config(WestCommand):
return parser return parser
def do_run(self, args, user_args): def do_run(self, args, user_args):
if args.value is None: if args.list:
if args.name:
self.parser.error('-l cannot be combined with name argument')
elif not args.name:
self.parser.error('missing argument name '
'(to list all options and values, use -l)')
if args.list:
self.list(args)
elif args.value is None:
self.read(args) self.read(args)
else: else:
self.write(args) self.write(args)
def list(self, args):
cfg = configparser.ConfigParser()
what = args.configfile or ALL
read_config(configfile=what, config=cfg)
for s in cfg.sections():
for k, v in cfg[s].items():
log.inf('{}.{}={}'.format(s, k, v))
def read(self, args): def read(self, args):
section, key = self._sk(args) section, key = self._sk(args)
cfg = configparser.ConfigParser() cfg = configparser.ConfigParser()

View File

@ -277,3 +277,34 @@ def test_unset_config():
with pytest.raises(subprocess.CalledProcessError) as e: with pytest.raises(subprocess.CalledProcessError) as e:
cmd('-v config pytest.missing') cmd('-v config pytest.missing')
assert 'pytest.missing is unset' in str(e) assert 'pytest.missing is unset' in str(e)
def test_no_args():
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config')
assert 'missing argument name' in str(e)
def test_list():
def sorted_list(other_args=''):
return list(sorted(cmd('config -l ' + other_args).splitlines()))
with pytest.raises(subprocess.CalledProcessError) as e:
cmd('config -l pytest.foo')
assert '-l cannot be combined with name argument' in str(e)
assert cmd('config -l').strip() == ''
cmd('config pytest.foo who')
assert sorted_list() == ['pytest.foo=who']
cmd('config pytest.bar what')
assert sorted_list() == ['pytest.bar=what',
'pytest.foo=who']
cmd('config --global pytest.baz where')
assert sorted_list() == ['pytest.bar=what',
'pytest.baz=where',
'pytest.foo=who']
assert sorted_list('--system') == []
assert sorted_list('--global') == ['pytest.baz=where']
assert sorted_list('--local') == ['pytest.bar=what',
'pytest.foo=who']