Delphi tutorial: Command Line Parameters, an alternative to .ini, registry.

HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

IGNORE ANY PERIODS (.) AT THE START OF ANY LINE

Using Command Line Parameters

I was writing a program to display datafiles of stock prices.

I had many files, all of the same format, all in the same folder. For this, you can imagine just a sample: IBM.HIS, HPCO.HIS and DIS.HIS.

The program starts by asking which data you want to see. If you type 'DIS', you get the stock prices for Disney, from the file DIS.HIS

I did not want my program to need to use an .ini file or the registry, but I did want a way to tell it where (in what folder, aka directory) to look for the data, without having to put that information each time I ran the program, and without having to have the path hard coded into the program.

I was prepared to hard code a default location for the data. However, in addition to that, I wanted it to be possible for the user to tell the program to look in another place by using a 'command line parameter'. (Hereinafter called a clp.)

Before I go into the programming of this tutorial, let me just expand on the idea of using clps. For simplicity, I'll work with a more simple case. Suppose you had a program for displaying the contents of a text file. It would be possible to write it so that the text would be displayed in a 10 point font if you used a clp of 10, or 18 point font if you used a clp of 18, (and so forth).
__________
In Win3.x you would either modify an existing program manager icon, or set up a new one in the usual way: Program manager |File |New |Program Item. If you are modifying an existing icon, within Program Manager you select it, then use File| Properties to access the Program Item Properties dialog. You add command line parameters (clps) to the end of the file's command line property. We'll see in a moment how the program can look at what you've put there. Don't try it yet.
__________
In Win 95 and 98 you can only (as far as I know!) use clps with shortcuts to programs, but this doesn't seem like a particular nuisance. In both, first create the shortcut. Then right click on it, then click properties. In 95, you select the 'program' tab and add the clp to the designated 'cmd line'. In 98, you add the clp to the 'target' line of the shorcut. In some circumstances, it seems that you need to put the program designation in quotes, with the clp coming after, e.g.

"C:\Windows\notepad.exe" C:\MyText.txt

(The example given would create a shortcut which would launch Notepad loaded with MyText. Does anyone know how, in Win3.x, to cause Notepad to launch with wordwrap turned on?)
__________
Anyway... back to our story...

Write a small text file in C:\. Call it TMPDD08.txt

Start a Delphi project. Put a memo on the form.

Set up....

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.lines.loadfromfile('C:\tmpdd08.txt');
end;

(You might also want a 'quit' button which executes application.terminate)

When you run this, you should see your text appear in the memo.

Once that's working...
Just after the loadfromfile line, add...

memo1.font.size:=strtoint(InputBox('Font Size Selection', 'Font size?', '12'));

(When you run the program, it will complain if you enter something silly, but this tutorial isn't about error trapping.)

The program should now allow you to specify the size you want used to display the text. You still have to type in your requirement each time the program runs, though.

Now replace the memo1.font.size... line with the following two lines...

if paramcount>0 then
memo1.font.size:=StrToInt(paramstr(1));

AND (before you try to run it) put 18 in as a parameter using Delphi's Run| Parameters menu item. (This is just a quick, easy, way to see how the program would run from an icon or shortcut with a clp specified. Once you get the program running under the IDE (i.e. with Delphi's Run command), you can try running the program from outside Delphi if you want to. You don't even have to close Delphi down.)

Now when you run the program, the memo should appear in an 18pt font. Change the size specified, just to be sure you're seeing the result of the parameter you have set.

Now, backtracking a little, what was all that code about?

paramcount is a function. It returns the count of clps which were present when the program was run.

paramstr(1) returns the first clp. You can use paramstr(2), (3), etc if you have more than one clp.

So far so good, I hope?
_______
We're now going to return to the original objective: setting up a program to fetch files from a default location if there is no clp, or if the clp points to a place without the required type of file, or from a clp specified location otherwise.

You can use different locations and filenames if you wish, but for this tutorial, I'm going to make the extension of the target files DD8. The clp-specified location will be C:\TEMP\ and the default location will be C:\ Note that for the tutorial you should put the data files in a different directory than the .exe file just in case something works, but not for the reasons you think! (In use, the system doesn't mind if the data shares the .exe's folder.)

Create a two test data files...
C:\tmp1.DD8 containing 'This came from C:\'
C:\TEMP\tmp1.DD8 containing 'This came from C:\TEMP'
(You can delete TMPDD08.txt; we won't need it again)

Make the following changes to your Delphi program:

Just after the {$R *.DFM} after 'implementation', add

const DefaultPath='C:\';(*must end with \*)
var sDataPath:string;

Make FormCreate (in it's entirity, i.e. remove earlier code in it)...

sDataPath:=DefaultPath;
memo1.lines.loadfromfile(sDataPath+'tmp1.DD8');

... and get that much running.

Use Delphi's Run| Paramters menu item to set up C:\TEMP\ as the clp. (Don't miss the second \)

Add....

if paramcount>0 then sDataPath:=paramstr(1);

..... to the program, just after the sDataPath:=... line.

Now when you run the program, your other tmp1.DD8 should appear in the memo box.

______
That gives us most of what was required. However, the program needs to be made more robust, more user-friendly.

The program will be changed so that it only uses the clp designated folder if there is suitable data there. For the purposes of this, we will define 'suitable' as having a .DD8 file name extension. (This was the reason I chose that 'odd' extension.)

Just after the....

if paramcount>0 then sDataPath:=paramstr(1);... add....

if sDataPath[length(sDataPath)]<>'\' then sDataPath:=sDataPath+'\';
    (*preceeding saves user who forgets terminal \ in path designation*)
if not fileexists(sDataPath+'*.dd8') then begin
    showmessage('No .DD8 files found in folder '+sDataPath+
       '. Trying default.');
    sDataPath:=DefaultPath;
if not fileexists(sDataPath+'*.dd8') then
    showmessage('No .DD8 files found in folder '+sDataPath+'.');
end;

(If your program cannot run without a data file, you need to add code to handle the error that occurs if neither the default folder nor the clp-designated folder hold suitable files.)

Clps are a great alternative to .ini files and the Win95 registry. I don't like receiving programs that 'do things' to my system, so I try to avoid putting such 'features' in programs I sell. With clps, I can write a program so that it will run on a customer's machine without 'doing things' to it until he/she has decided that he wants to allow the changes. The 'you may do things' instruction can be put into a clp to save the customer having to answer 'May I...?' repeatedly. Clps can also be used to configure software in a relatively permanent and convenient manner. For example, I have some arithmetic games. A parent or teacher can have one copy of the program, but two shortcuts. Using the first shortcut entails a clp which causes the program to give easy questions; using the other causes the program to give hard questions.

Floreat clps!


   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search


Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.


Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....