I am stuck with something and I cannot seem to find a solution to this.
The JSON array below needs to be parsed and all the arguments appended in a command up untill the deepest level.
{
"commands": {
"dth": {
"authorization": [],
"arguments": {
"user": {
"description": "This ia the first command description",
"authorization": [],
"required": true,
"hasValue": false,
"arguments": {
"create": {
"required": true,
"hasValue": false,
"arguments": {
"-p": {
"required": true,
"hasValue": true
},
"-u": {
"required": true,
"hasValue": true,
"arguments": {
"-n": {
"required": true,
"hasValue": true
}
}
}
}
},
"update": {
"required": true,
"hasValue": false,
"arguments": {
"-p": {
"required": true,
"hasValue": true
},
"-u": {
"required": true,
"hasValue": true,
"arguments": {
"-n": {
"required": true,
"hasValue": false
}
}
}
}
},
"delete": {
"required": true,
"hasValue": false,
"arguments": {
"-u": {
"required": true,
"hasValue": true
}
}
},
"search": {
"required": false,
"hasValue": true
}
}
},
"data": {
"hasValue": true,
"required": true
}
}
},
"aloha": {
"authorization": [],
"arguments": {
"save": {
"required": true,
"hasValue": true
}
}
}
}
}
The commands generated should look something like this:
/dth user create -n {value} -u {value} -p {value}
I tried this and it did not work, it created duplicates and I am unsure how to properly and efficiently do it.
def append_belongs_to_arguments(command, argument_strings, command_details):
command_list = []
if 'belongsTo' in command_details:
for belongs_to in command_details['belongsTo']:
argument_strings.append(belongs_to)
nested_command_list = generate_commands(command_details['arguments'], prefix=command,
parent_key=belongs_to)
command_list.extend(nested_command_list)
# Recursive call to handle nested arguments
append_belongs_to_arguments(command, argument_strings, command_details['arguments'][belongs_to])
return command_list
def generate_commands(commands, prefix='', command_list=None, parent_key=None):
if command_list is None:
command_list = []
for command, details in commands.items():
if prefix:
if parent_key:
full_command = f"{prefix} {parent_key} {command}"
else:
full_command = f"{prefix} {command}"
else:
full_command = command
if 'arguments' in details:
arguments = details['arguments']
argument_strings = []
for argument, argument_details in arguments.items():
if 'hasValue' in argument_details and argument_details['hasValue']:
argument_strings.append(f"{argument} {{value}}")
else:
argument_strings.append(argument)
if 'arguments' in argument_details:
nested_command_list = generate_commands(argument_details['arguments'], prefix=full_command,
parent_key=argument)
command_list.extend(nested_command_list)
append_belongs_to_arguments(full_command, argument_strings, argument_details)
append_belongs_to_arguments(full_command, argument_strings, details)
if len(argument_strings) == len(arguments):
command_string = f"{full_command} {' '.join(argument_strings)}"
command_list.append(command_string)
if 'hasValue' in details and details['hasValue']:
full_command = full_command + ' {value}'
# Add the command if it has arguments or if it's the initial command
if command_list and (details.get('arguments') or not prefix) and full_command not in command_list:
command_list.append(full_command)
return command_list
Thanks!