Skip to content

Commit

Permalink
Updated the method to parse options without values
Browse files Browse the repository at this point in the history
  • Loading branch information
midays committed Feb 18, 2024
1 parent 3903227 commit b723f58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/models/configuration/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def from_dict(obj: Any) -> "Options":
input = from_union([lambda x: from_list(from_str, x), from_none], obj.get("input"))
cli = from_union([from_str, from_none], obj.get("cli"))
target = from_union([lambda x: from_list(from_str, x), from_none], obj.get("target"))
overwrite = from_union([from_none, lambda x: from_stringified_bool(from_str(x))], obj.get("overwrite"))
overwrite = from_union([from_none, from_bool, lambda x: from_stringified_bool(from_str(x)) if isinstance(x, str) else x], obj.get("overwrite"))
analyze_known_libraries = from_union([from_none, from_bool, lambda x: from_stringified_bool(from_str(x)) if isinstance(x, str) else x], obj.get("analyze-known-libraries"))
source = from_union([lambda x: from_list(from_str, x), from_none], obj.get("source"))
return Options(output, input, cli, target, overwrite, source, analyze_known_libraries)
Expand All @@ -79,7 +79,7 @@ def to_dict(self) -> dict:
if self.source is not None:
result["source"] = from_union([lambda x: from_list(from_str, x), from_none], self.source)
if self.analyze_known_libraries is not None:
result["analyze_known_libraries"] = from_union(
result["analyze-known-libraries"] = from_union(
[lambda x: from_none((lambda x: is_type(type(None), x))(x)), lambda x: from_str((lambda x: str((lambda x: is_type(bool, x))(x)).lower())(x))],
self.analyze_known_libraries,
)
Expand Down
17 changes: 14 additions & 3 deletions src/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,36 @@ def parse_log_string(log_string):

def parse_kantra_cli_command(command):
"""
Parse the kantra cli string and returns a dictionary with keys as command flags
Parse the kantra cli string and returns a dictionary with keys as command flags.
Flags without associated values are categorized under 'advanced_options'.
"""
command_items = shlex.split(command)
command_items = command_items[1:]
cmd_map = defaultdict(list)
last_was_flag = False
checked_advanced_options = []

for item in command_items:
if item.startswith("--"):
if last_was_flag:
checked_advanced_options.append(current_flag)
current_flag = item[2:]
last_was_flag = True
else:
if last_was_flag:
cmd_map[current_flag].append(item)
last_was_flag = False
for key, value in cmd_map.items():
if len(value) == 1:

if last_was_flag:
checked_advanced_options.append(current_flag)

for key, value in list(cmd_map.items()):
if isinstance(value, list) and len(value) == 1:
cmd_map[key] = value[0]

if checked_advanced_options:
cmd_map['advanced_options'] = checked_advanced_options

return dict(cmd_map)


Expand Down

0 comments on commit b723f58

Please sign in to comment.