I currently have a process executor which executes a script along with parameters. The script and parameters are user provided.
execute_script(script_name: str, parameters: str) -> None :
// Some logic
subprocess.Popen( ....)
The user provides both script name and parameters as a string. The execute_script(...)
is a backend function which gets eventually invoked by an API exposed to the user. So, even though the parameters are executed as command-line arguments, they are not received as command-line arguments, but rather string variables.
For example:
- script_name="sendEmail", parameters="--subject 'URGENT' --to 'xyz@redif.com, abc@hotmail.com'"
- script_name="calculatePerformance", parameters="--revenue 193456343 --tax_per 32543 --operating_cost 3452564.256 --includeVacationDays"
- script_name="generate_report", parameters"--duration 'quarterly' --dept 'Finance'"
I do not have control over the parameters being sent by yer user. We have a lot of such script and they keep increasing. So keeping a track of potential parameters per script is not feasible.
I need to ensure that only a permitted values are used for certain parameters when used in conjunction with particular scripts. For Example:
- the duration can ONLY be quarterly or monthly if script is generate_report. For any other script, any value can be provided.
I want to convert the parameters provided to the function from a string to a map, so that it is much more easier to extract and validate values for a script parameter.
Using space as delimiter to split and parse the string does not seem to be cleanest approach. Is there a more compact way to convert the parameters from string to map ? Alternatively, any other suggestions for parsing the string?