Skip to content

Commit fc2b390

Browse files
committed
Simplified some code.
1 parent 26ef9c9 commit fc2b390

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

cmd2/command_set.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class MyCommandSet(CommandSet[MyCustomApp]):
5555
5656
:raises CommandSetRegistrationError: if CommandSet is not registered.
5757
"""
58-
if (cmd := self._cmd_internal) is not None:
59-
return cmd
60-
raise CommandSetRegistrationError('This CommandSet is not registered')
58+
if self._cmd_internal is None:
59+
raise CommandSetRegistrationError('This CommandSet is not registered')
60+
return self._cmd_internal
6161

6262
def on_register(self, cmd: CmdT) -> None:
6363
"""First step to registering a CommandSet, called by cmd2.Cmd.
@@ -69,10 +69,9 @@ def on_register(self, cmd: CmdT) -> None:
6969
:param cmd: The cmd2 main application
7070
:raises CommandSetRegistrationError: if CommandSet is already registered.
7171
"""
72-
if self._cmd_internal is None:
73-
self._cmd_internal = cmd
74-
else:
72+
if self._cmd_internal is not None:
7573
raise CommandSetRegistrationError('This CommandSet has already been registered')
74+
self._cmd_internal = cmd
7675

7776
def on_registered(self) -> None:
7877
"""2nd step to registering, called by cmd2.Cmd after a CommandSet is registered and all its commands have been added.
@@ -110,13 +109,12 @@ def add_settable(self, settable: Settable) -> None:
110109
:param settable: Settable object being added
111110
"""
112111
if (cmd := self._cmd_internal) is not None:
113-
if not cmd.always_prefix_settables:
114-
if settable.name in cmd.settables and settable.name not in self._settables:
115-
raise KeyError(f'Duplicate settable: {settable.name}')
116-
else:
117-
prefixed_name = f'{self._settable_prefix}.{settable.name}'
118-
if prefixed_name in cmd.settables and settable.name not in self._settables:
119-
raise KeyError(f'Duplicate settable: {settable.name}')
112+
# Determine the name to check for collisions in the main app
113+
check_name = settable.name if not cmd.always_prefix_settables else f'{self._settable_prefix}.{settable.name}'
114+
115+
if check_name in cmd.settables and settable.name not in self._settables:
116+
raise KeyError(f'Duplicate settable: {settable.name}')
117+
120118
self._settables[settable.name] = settable
121119

122120
def remove_settable(self, name: str) -> None:

0 commit comments

Comments
 (0)