Skip to content

Commit 3cf5a27

Browse files
committed
app: config: Add support for appending to the config string
In some cases, and specifically in the manifest.group-filter and manifest.project-filter options, it is sometimes useful to be able to append to a value instead of replacing it completely. For example, assuming one wants to add to an existing group filter, without this patch the user needs to do: (assuming the group filter is currently +unstable,-optional, and the user wants to add +extras). > west config manifest.group-filter > west config manifest.group-filter +unstable,-optional,+extras With this patch instead: > west config -a manifest.group-filter ,+extras Signed-off-by: Carles Cufi <[email protected]>
1 parent 0fbb54c commit 3cf5a27

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/west/app/config.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
To set a value for <name>, type:
4949
west config <name> <value>
5050
51+
To append to a value for <name>, type:
52+
west config -a <name> <value>
53+
A value must exist in the selected configuration file in order to be able
54+
to append to it.
55+
5156
To list all options and their values:
5257
west config -l
5358
@@ -64,7 +69,7 @@
6469

6570
CONFIG_EPILOG = '''\
6671
If the configuration file to use is not set, reads use all three in
67-
precedence order, and writes use the local file.'''
72+
precedence order, and writes (including appends) use the local file.'''
6873

6974
ALL = ConfigFile.ALL
7075
SYSTEM = ConfigFile.SYSTEM
@@ -88,12 +93,19 @@ def do_add_parser(self, parser_adder):
8893
description=self.description,
8994
epilog=CONFIG_EPILOG)
9095

91-
parser.add_argument('-l', '--list', action='store_true',
96+
group = parser.add_argument_group(
97+
"action to perform (give at most one)"
98+
).add_mutually_exclusive_group()
99+
100+
101+
group.add_argument('-l', '--list', action='store_true',
92102
help='list all options and their values')
93-
parser.add_argument('-d', '--delete', action='store_true',
103+
group.add_argument('-d', '--delete', action='store_true',
94104
help='delete an option in one config file')
95-
parser.add_argument('-D', '--delete-all', action='store_true',
105+
group.add_argument('-D', '--delete-all', action='store_true',
96106
help="delete an option everywhere it's set")
107+
group.add_argument('-a', '--append', action='store_true',
108+
help='append to an existing value')
97109

98110
group = parser.add_argument_group(
99111
"configuration file to use (give at most one)"
@@ -121,20 +133,20 @@ def do_run(self, args, user_args):
121133
if args.list:
122134
if args.name:
123135
self.parser.error('-l cannot be combined with name argument')
124-
elif delete:
125-
self.parser.error('-l cannot be combined with -d or -D')
126136
elif not args.name:
127137
self.parser.error('missing argument name '
128138
'(to list all options and values, use -l)')
129-
elif args.delete and args.delete_all:
130-
self.parser.error('-d cannot be combined with -D')
139+
elif args.value is None and args.append:
140+
self.parser.error('-a requires a value')
131141

132142
if args.list:
133143
self.list(args)
134144
elif delete:
135145
self.delete(args)
136146
elif args.value is None:
137147
self.read(args)
148+
elif args.append:
149+
self.append(args)
138150
else:
139151
self.write(args)
140152

@@ -179,6 +191,16 @@ def read(self, args):
179191
self.dbg(f'{args.name} is unset')
180192
raise CommandError(returncode=1)
181193

194+
def append(self, args):
195+
self.check_config(args.name)
196+
what = args.configfile or LOCAL
197+
value = self.config.get(args.name, configfile=what)
198+
if value is None:
199+
self.die(f'option {args.name} not found in {what.name.lower()} '
200+
'configuration file')
201+
args.value = value + args.value
202+
self.write(args)
203+
182204
def write(self, args):
183205
self.check_config(args.name)
184206
what = args.configfile or LOCAL

0 commit comments

Comments
 (0)