Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to analyse .exe parameters inside the program?

Tags:

java

python

c#

.net

I have a program that can have a lot of parameters (we have over +30 differents options).

Example: myProgram.exe -t alpha 1 -prod 1 2 -sleep 200

This is 3 Commands (from command pattern object at the end) that each contain some parameters. Inside the code we parse all command (start with -) and get a list of string (split all space) for the parameters. So in fact, we have : string-->Collection of String parameters for each command.

For the moment, we use string comparison and we can get the whole thing works (instance the concrete command and return the ICommand interface). The problem is we require to do a lot of IF everytime to get the good command.

Do you have some pattern that can be used to extract all parameters from an EXE without using a lot of IF?

The code is in C# but I think the logic can be any other language too...

like image 637
Patrick Desjardins Avatar asked Nov 29 '22 21:11

Patrick Desjardins


2 Answers

(Well, since this is tagged with Python):

We use Python's optparse module for this purpose. It has a much friendlier API than lots of ifs.

like image 140
Ali Afshar Avatar answered Dec 05 '22 18:12

Ali Afshar


Create a hash table which stores function pointers (in C# that'd be delegates) for handling each of the parameters, keyed using the parameter text. Then you just go through the command line in a loop and make calls to delegates based on what comes out of hash table lookups.

like image 35
SoapBox Avatar answered Dec 05 '22 18:12

SoapBox