Skip to content

Commit

Permalink
Merge pull request #271 from kairu-ms/fix-arg-groups-null
Browse files Browse the repository at this point in the history
Fix bugs while command has no arguments.
  • Loading branch information
kairu-ms authored Jul 21, 2023
2 parents 36b49f0 + acc4afb commit fb3789f
Showing 1 changed file with 57 additions and 47 deletions.
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

0 comments on commit fb3789f

Please sign in to comment.