How to deal with user prompt/command line/input file

Hi Guys,

I've a problem...

I'm developing a software tool which can be started in the following way...

1. ./myProgram
2. ./myProgram -op1 arg1 -op2 arg2 -flag3 arg31 arg32 ...
3. ./myProgram nameFile
... /*other cases*/

in case 1 the program will ask the user to insert a set of parameter.
in case 2 the program will parse the input opts and related args
in case 3 the program will read a set of commmand inside a file and in that case will performs a task.

They might be other way to start the program but whatever is the way the goal is to build a particular object of a particular class (i think it doesn't matter know too many details of which task and how the task is performed).

My doubt is the following... i was wondering how to handle this step in a non "monolitic way". I mean... the simplest way could be

if( case 1...) {...}
else if(case 2...) {...}
else if(case 3...) {...}

etc

of course in this way i have to concentrate too much stuff in the same class... instead i'm implementing the whole by the design pattern "chain of responsability".

so... if i passed nothing/file/command line the proper handler would perform the correct task and moreover i suppose it would be easier to be extended with other class, decoupled from the old one.

Do you think that this kind of design can be good? i highlight the task is not trivial... so i'm struggling in find a good design.

Cheers
This:
1
2
3
if( case 1...) {...}
else if(case 2...) {...}
else if(case 3...) {...}

could be the basis of some kinda factory method, returning you one of 3 objects, all which implement some kind of interface with a "Run()" method, or something like that.e.g.

1
2
IInterface pInterface = myFactoryMethod(/* relevant parameters */);
pInterface->Run()


edit: I'm not sure why you've decided to post this in the linux sub-forum?
Last edited on
what about the command pattern? is it too much for this stuff?

(it's here because i'm working in a linux environment, i think i can post some code which contains linux system call etc...).
1
2
3
4
5
switch (argc) {
case 1: promptForArgs(); break;
case 2: readArgs(argv[2]); break;
default: parseCmdLine(argc, argv); break;
}

This is simple and easy to understand. Why make it more complicated?
Because what i need is to initialize an object in one of the way...
I'm understanding that probably the best way is (simply) use the callback right?

PS. Further you've to consider that i could interact with such tool with a GUI in the future, so i want something of flexible to this. Probably thinking now at this things it's a bit tricky, but i think i can benefit from this kind of design.
Last edited on
Topic archived. No new replies allowed.