Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs while command has no arguments. #271

Merged
merged 1 commit into from
Jul 21, 2023
Merged
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
104 changes: 57 additions & 47 deletions src/aaz_dev/command/controller/cfg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ def find_arg_in_command(cls, command, idx):
idx = cls.arg_idx_to_list(idx)
assert isinstance(idx, list), f"invalid arg_idx type: {type(idx)}"

for arg_group in command.arg_groups:
arg = cls.find_arg_in_group(arg_group, idx)
if arg:
return arg
if command.arg_groups:
for arg_group in command.arg_groups:
arg = cls.find_arg_in_group(arg_group, idx)
if arg:
return arg

return None

def find_arg_parent(self, *cmd_names, idx):
Expand All @@ -212,9 +214,10 @@ def find_arg_parent_in_command(cls, command, idx):
assert isinstance(idx, list), f"invalid arg_idx type: {type(idx)}"

if len(idx) == 1:
for arg_group in command.arg_groups:
if cls.find_arg_in_group(arg_group, idx) is not None:
return None, arg_group
if command.arg_groups:
for arg_group in command.arg_groups:
if cls.find_arg_in_group(arg_group, idx) is not None:
return None, arg_group
else:
parent_idx = idx[:-1]
parent_arg = cls.find_arg_in_command(command, idx=parent_idx)
Expand Down Expand Up @@ -313,18 +316,20 @@ def arg_filter(_parent, _arg, _arg_idx, _arg_var):
return (_parent, None, None, None), True
return None, False

for arg_group in command.arg_groups:
matches = [match for match in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter
)]
if not matches:
continue
assert len(matches) == 1

parent, arg, arg_idx, arg_var = matches[0]
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
return parent, arg, arg_idx
if command.arg_groups:
for arg_group in command.arg_groups:
matches = [match for match in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter
)]
if not matches:
continue
assert len(matches) == 1

parent, arg, arg_idx, arg_var = matches[0]
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
return parent, arg, arg_idx

return None, None, None

@classmethod
Expand Down Expand Up @@ -393,18 +398,20 @@ def arg_filter(_parent, _arg, _arg_idx, _arg_var):
return (_parent, _arg, _arg_idx, _arg_var), True
return None, False

for arg_group in command.arg_groups:
matches = [match for match in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter
)]
if not matches:
continue
assert len(matches) == 1

parent, arg, arg_idx, arg_var = matches[0]
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
return parent, arg, arg_idx, arg_var
if command.arg_groups:
for arg_group in command.arg_groups:
matches = [match for match in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter
)]
if not matches:
continue
assert len(matches) == 1

parent, arg, arg_idx, arg_var = matches[0]
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
return parent, arg, arg_idx, arg_var

return None, None, None, None

def iter_arg_cls_definition(self, *cmd_names, cls_name_prefix=None):
Expand All @@ -431,11 +438,12 @@ def arg_filter(_parent, _arg, _arg_idx, _arg_var):
return (_parent, _arg, _arg_idx, _arg_var), False
return None, False

for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in cls._iter_args_in_group(arg_group, arg_filter=arg_filter):
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var
if command.arg_groups:
for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in cls._iter_args_in_group(arg_group, arg_filter=arg_filter):
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var

def iter_arg_cls_reference(self, *cmd_names, cls_name):
command = self.find_command(*cmd_names)
Expand All @@ -457,22 +465,24 @@ def arg_filter(_parent, _arg, _arg_idx, _arg_var):
return (_parent, _arg, _arg_idx, _arg_var), False
return None, False

for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter):
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var
if command.arg_groups:
for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in cls._iter_args_in_group(
arg_group, arg_filter=arg_filter):
if arg:
arg_idx = cls.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var

def iter_args_in_command(self, command):
def arg_filter(_parent, _arg, _arg_idx, _arg_var):
return (_parent, _arg, _arg_idx, _arg_var), False

for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in self._iter_args_in_group(arg_group, arg_filter=arg_filter):
if arg:
arg_idx = self.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var
if command.arg_groups:
for arg_group in command.arg_groups:
for parent, arg, arg_idx, arg_var in self._iter_args_in_group(arg_group, arg_filter=arg_filter):
if arg:
arg_idx = self.arg_idx_to_str(arg_idx)
yield parent, arg, arg_idx, arg_var

# TODO: build arg_idx in command link call
@classmethod
Expand Down