Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

Improve CommandBase.register_sub_command to use the subcommand's Comm… #725

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions neo/Prompt/CommandBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ def command_desc(self):
def execute_sub_command(self, id, arguments):
self.__sub_commands[id].execute(arguments)

# ids: can be either a string or a list of strings.
def register_sub_command(self, ids, sub_command):
if isinstance(ids, list):
for id in ids:
self.__register_sub_command(id, sub_command)
else:
self.__register_sub_command(ids, sub_command)
def register_sub_command(self, sub_command, additional_ids=[]):
"""
Register a command as a subcommand.
It will have it's CommandDesc.command string used as id. Additional ids can be provided.

Args:
sub_command (CommandBase): Subcommand to register.
additional_ids (List[str]): List of additional ids. Can be empty.
"""
self.__register_sub_command(sub_command, sub_command.command_desc().command)
for id in additional_ids:
self.__register_sub_command(sub_command, id)

def __register_sub_command(self, id, sub_command):
def __register_sub_command(self, sub_command, id):
if id in self.__sub_commands:
raise ValueError(f"{id} is already a subcommand of {self.command_desc().command}.")
if sub_command.__parent_command and sub_command.__parent_command != self:
Expand Down
12 changes: 5 additions & 7 deletions neo/Prompt/Commands/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class CommandWallet(CommandBase):
def __init__(self):
super().__init__()

self.register_sub_command('create', CommandWalletCreate())
self.register_sub_command(['v', '--v', 'verbose'], CommandWalletVerbose())
self.register_sub_command('migrate', CommandWalletMigrate())
self.register_sub_command('create_addr', CommandWalletCreateAddress())
self.register_sub_command(CommandWalletCreate())
self.register_sub_command(CommandWalletVerbose(), ['v', '--v'])
self.register_sub_command(CommandWalletMigrate())
self.register_sub_command(CommandWalletCreateAddress())

def command_desc(self):
return CommandDesc('wallet', 'manage wallets')
Expand Down Expand Up @@ -65,8 +65,7 @@ class CommandWalletCreate(CommandBase):
def __init__(self):
super().__init__()

@classmethod
def execute(cls, arguments):
def execute(self, arguments):
path = get_arg(arguments, 0)

if path:
Expand Down Expand Up @@ -105,7 +104,6 @@ def execute(cls, arguments):
else:
print("Please specify a path")

@classmethod
def command_desc(self):
p1 = ParameterDesc('path', 'path to store the wallet file')
return CommandDesc('create', 'creates a new NEO wallet address', [p1])
Expand Down