Solved

adding commandline support

Posted on 2004-05-02
12
305 Views
Last Modified: 2010-04-05
I have done some reading about how to use command line params in your delphi app but they did not really tell a full story of how it's done so I am asking the experts for alil help.

Basically I have my program completely finished and I would like to give the end user the ability to go to the Start>Run box in windows and type something like this.

program.exe /param1 /param2 /param3 .......

and depending on what switches they put in the program would start in a different manner.

Lets take a cd burning program for example. In the program when a user presses ButtonA it starts the burning process and if they press ButtonB it checks how much free space is left on the cd.

So given the example above how would you add command line support so that if the user types in the Start>Run box

cdburn.exe /burn it will do the same as if he pressed buttonA

or

cdburn.exe /check it will do the same as buttonB

or finally

cdburn.exe /check /burn it would do both functions

Anyway thx for all replies in advance.
0
Comment
Question by:ezpete
  • 6
  • 5
12 Comments
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 50 total points
ID: 10971059

   you have global variable "CmdLine" which give you the command line

   also ParamStr(1) - first parameter, ParamStr(2) - second, etc
   ParamStr(0) is the application full name (with path)

   function ParamCount returns you the number of parameters supplied
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10971202
Have you looked at Delphi help for ParamCnt and ParamStr? E.g., for ParamStr:

Returns a specified parameter from the command-line.

Unit

System

Category

command line utilities

function ParamStr(Index: Integer): string;

Description

ParamStr returns the parameter from the command line that corresponds to Index, or an empty string if Index is greater than ParamCount. For example, an Index value of 2 returns the second command-line parameter.

ParamStr(0) returns the path and file name of the executing program (for example, C:\TEST\MYPROG.EXE).

Note:      Use double quotes to wrap multiple words as one parameter (such as long file names containing spaces).
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10971203
Aah. Ivanov_G - sorry I didn't see your earlier post.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:ezpete
ID: 10971591
Hello,

That much I already understood but what I don't understand is how to tie the switches to a function/procedure in my project.... ???
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10971607
You will have to parse them and set configuration switches accordingly.
0
 

Author Comment

by:ezpete
ID: 10971618
This is what I understand .... I know you can create params in Delphi with the Run>Parameters dialog.

So like i have said b4 lets say I create 2 params in the Run Parameters dialog called /Check and /Burn now what I DON'T understand is what do I need to do to my functions/procedures so they will be called when I type the switches in the Start>Run box.

BTW if there is an example in the delphi help for this I can't find it.
0
 
LVL 7

Accepted Solution

by:
sftweng earned 75 total points
ID: 10971633
When your program starts, in the OnCreate event (or elsewhere if you wish), you can parse and set the switches.

Something needs to trigger the execution - it could be from the OnCreate event or the OnShow event or perhaps on a button click.

So let's assume it's from a button click. In the OnClick event handler, test the switches found from the command line and act accordingly.

From what you have described, you might be better off with a "Console" application.
0
 

Author Comment

by:ezpete
ID: 10971658
The program I want to do this to is already finished "minus the commandline support stuff" so redoing it all again as a console app would be a waste of time unless you mean create an additional console app that controls the main app and that too would be something I would need help with.

Many popular programs today have commandline support and don't use additional console apps to accomplish this and so I thought this would be a snap but apparently not...

All I can say at this point is that I am going to need more help to get this thing going and so I will wait for more relpies and thx again for you time.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10971683
ezpete, it might help us to respond if you were to send us some code. Something needs to trigger your program's actions. Those actions need to test the switches set by parsing the commandline input.

If the switches are already used buy the program to control it's flow, then all you need to do is set them from the OnCreate event of the main form.
0
 

Author Comment

by:ezpete
ID: 10971769
I am no guru but this seemed to work... I added the /burn and /check params to the Run>Parameters stuff then I added this code on the forms OnCreate Event.

procedure TForm1.FormCreate(Sender: TObject);
begin
 if FindCmdLineSwitch('burn')
  then ShowMessage('you used the burn command line switch');

  if FindCmdLineSwitch('check')
  then ShowMessage('you used the check command line switch');
end;

Lastly I added this to the Start>Run box

"ProgramName.exe" /burn and it showed the burn message and the same for the /check switch

 so now my question is if this the way I should be doing it or not.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10971950
If it works then that's what you need. ;-)
0
 

Author Comment

by:ezpete
ID: 10974015
The reason I asked is because I did not want to do something that could be done in a better/safer/more stable manner but since nobody is saying it's a bad idea then I will use it and thx for the help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 103
Delphi XE10 Round Image 2 121
HTML text in the body of an email (delphi code) 12 112
Performance of SQL statement 37 111
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now