How to handle command line parameters
https://softwareengineering.stackexchange.com/questions/388065
-
21-02-2021 - |
Question
I am writing a gui that is supposed to replace a fairly large shellscript in order to make the usage easier for not-so-technical people.
The script calls a certain program (saga_cmd
) multiple times with different parameters, i.e.
saga_cmd grid_calculus 8 -GRIDS "TriGridResult.sgrd;TpiGridResult.sgrd;SlopeResultGrid.sgrd" -RESULT "ResultGrid3".sgrd
saga_cmd grid_calculus 9 -GRIDS "ResultGrid3.sgrd;DepthResultGrid.sgrd" -RESULT "ResultGrid".sgrd
where the second call relies on the output of the first call. There also are other parameters than filenames.
I can not change the structure of the script, but only put this into a gui where the parameters can be changed easily. The gui will be a Qt widgets application (c++). I found some helpful tips about the gui itself here, but I'm not sure how to implement the system calls in a good way and am looking for some kind of pattern. My searches led me to the Command design pattern, but it does not seem to fit, I thought of something like a builder pattern but as a class that would allow something like
string sagaLibrary;
int sagaTool;
...
sagaCmd().addParam(sagaLibrary).addParam(sagaTool).execute();
This is mostly a string builder, but you may get the idea. Surely there is a well-tried way on how to do this? Should there be functions like addParamSagaLib
and so on instead of a generic one? These could also handle parameters like -GRID
where there's a parameter name and the parameter itself in a better way I think.
Clarification: The user is allowed to change the values of the parameters and sometimes can choose to omit some of them, but the structure of each call is defined and must not be changed.
Solution
I would use a builder pattern with methods for each specific argument e.g., builder.grids(...)
. If there is a small set of arguments required for each call, I might make a static "create" method with those arguments.
public static Builder Builder.create(String gridField, String resultFile)
The Builder then creates an object that knows how to actually execute the program for the given arguments.
OTHER TIPS
Disclaimer: It is not any well established solution for the problem, only just my raw idea.
How about something like simplified MVC?
The
Model
of a command would store an information, what parameters a command can have.The
Model
of a parameter will hold a displayed name, generated name and some constraint of possible values. It will also hold an information, if it can be skipped by the user.Views
will define how to draw appropriate GUI for eachModel
.Controllers
will handle user interaction/will validate if user input is correct.Eventually your algorithm of generating the command line would be to iterate over all parameter
Models
through commandModel
, generate each parameter respectively and join them together.