Skip to content

Commit e62e4e1

Browse files
matejmatuskakarolinku
authored andcommitted
cli: Add possibility to specify aliases for CLI option
The existing 'name' and 'short_name' parameters are kept as is for backwards compatibility. Also a dest parameter is added for specifying the name of the attribute to be added to the parsed args object. The change is backwards compatible, bump framework-version from 6.1 to 6.2. Jira: RHEL-110563 (related)
1 parent 209a1db commit e62e4e1

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

leapp/utils/clicmd.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,22 @@ def _add_opt(self, *args, **kwargs):
202202
internal = kwargs.pop('internal', {})
203203
self._options.append((args, kwargs, internal))
204204

205-
def add_option(self, name, short_name='', help='', # noqa; pylint: disable=redefined-builtin
206-
is_flag=False, inherit=False, value_type=str, wrapped=None, action=None, metavar=None,
207-
choices=None, default=None):
205+
def add_option( # noqa; pylint: disable=redefined-builtin, too-many-arguments
206+
self,
207+
name,
208+
short_name="",
209+
help="",
210+
is_flag=False,
211+
inherit=False,
212+
value_type=str,
213+
wrapped=None,
214+
action=None,
215+
metavar=None,
216+
choices=None,
217+
default=None,
218+
aliases=None,
219+
dest=None,
220+
):
208221
"""
209222
Add an option
210223
@@ -230,16 +243,39 @@ def add_option(self, name, short_name='', help='', # noqa; pylint: disable=rede
230243
:type choices: list
231244
:param default: default value of the argument if nothing is specified
232245
:type default: any
246+
:param aliases: Aliases for the option, appended after short_name and name in help output
247+
:type aliases: list[str]
248+
:param dest: The name of the attribute to be added to the parsed args object
249+
:type dest: str
233250
:return: self
234251
"""
235252
name = name.lstrip('-')
236253
names = ['--' + name]
237254
kwargs = {}
238255
if short_name:
239-
short_name = short_name.lstrip('-')
240-
if len(short_name) != 1:
241-
raise CommandDefinitionError("Short name should be one letter only")
242-
names.insert(0, '-' + short_name)
256+
stripped = short_name.lstrip('-')
257+
if len(stripped) != 1:
258+
msg = "Short name option should be one letter only (excluding '-'), but received: {}"
259+
raise CommandDefinitionError(msg.format(short_name))
260+
names.insert(0, '-' + stripped)
261+
262+
aliases = aliases or []
263+
for alias in aliases:
264+
if alias.startswith('--'):
265+
names.append('--' + alias.strip('-'))
266+
elif alias.startswith('-'):
267+
stripped = alias.strip('-')
268+
if len(stripped) != 1:
269+
msg = "Short name option should be one letter only (excluding '-'), but received: {}"
270+
raise CommandDefinitionError(msg.format(alias))
271+
names.append('-' + stripped)
272+
else:
273+
# no way to distinguish whether it's a short or long option if no leading dashes,
274+
# decide based on length
275+
if len(alias) == 1:
276+
names.append('-' + alias)
277+
else:
278+
names.append('--' + alias)
243279
if not action:
244280
action = 'store'
245281
if is_flag:
@@ -252,6 +288,9 @@ def add_option(self, name, short_name='', help='', # noqa; pylint: disable=rede
252288
kwargs['choices'] = choices
253289
if default is not None:
254290
kwargs['default'] = default
291+
if dest:
292+
kwargs['dest'] = dest
293+
255294
self._add_opt(*names, help=help, # noqa; pylint: disable=redefined-builtin
256295
action=action, internal={'wrapped': wrapped, 'inherit': inherit}, **kwargs)
257296
return self

packaging/leapp.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# This is kind of help for more flexible development of leapp repository,
1414
# so people do not have to wait for new official release of leapp to ensure
1515
# it is installed/used the compatible one.
16-
%global framework_version 6.1
16+
%global framework_version 6.2
1717

1818
# IMPORTANT: everytime the requirements are changed, increment number by one
1919
# - same for Provides in deps subpackage

0 commit comments

Comments
 (0)