Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to my applicattion accept parameters..?

Posted on 2004-08-16
11
Medium Priority
?
300 Views
Last Modified: 2010-04-05
i need eexecute my app like this
   MyApplication.exe [servername][databasename] [username][password] how..?

hot to recognize this paremters into MYApplication.exe? and how to accept this parameters?...
0
Comment
Question by:ThoseBug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Accepted Solution

by:
Chadak earned 400 total points
ID: 11811485
Hi

   just use paramcount which return the number of param that was used
and use paramstr(0) to get the first parameter etc...
There are the 2 method you need to work with parameters

Chad
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 11811843
example code:
----------------

{ TODO -oUser -cConsole Main : Insert code here }

    sc:=['/','-','|'];
    sCmdLn := lowercase(CmdLine);
    { If help is needed display help & example, and then exit }
    if (pos('help',sCmdLn) > 0) or (pos('?',sCmdLn) > 0) then
    begin
        WriteLineOutPut('Application called with HELP parameter!');
        WriteLineOutPut('EXAMPLE:');
        WriteLineOutPut('   command format: appname [options]');
        WriteLineOutPut('   Explanations of usage are as follow:');
        WriteLineOutPut('   -o       : < Output Data to text file >');
        WriteLineOutPut('Example:');
        WriteLineOutPut('COPYRIGHT ' + #169 + ' 2004 Coder');
        exit;
    end;
    sCurDir :=  ExtractFilePath(ParamStr(0));  {Store the dir where your app was executed from}
    if ParamCount > 0 then  { Command line arguments were passed, parse them }
    begin  
    { [servername][databasename] [username][password] }          
    { -s <servername> -db <databasename> -u <username> -p <password> }
        for i:=1 to ParamCount do
        begin
            if ParamStr(i) = '-s' then
              servername := ParamStr(i + 1);
            if ParamStr(i) = '-db' then
              databasename  :=  ParamStr(i + 1);
            if ParamStr(i) = '-u' then
              username  :=  ParamStr(i + 1);
            if ParamStr(i) = '-p' then
              password  :=  ParamStr(i + 1);
        end;
    end;
    try
      Execute;
    except
      WriteLineOutPut('ERROR!');
      exit;
    end;

----------------
hope this helps.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11811936
Hello

Just quick sample to show the no of parameters passed to your application and each one with it's value

procedure TForm1.FormCreate(Sender: TObject);
var
 I : Integer;
begin
  ShowMessage('Total Parameters passed = ' + IntToStr(ParamCount));
  for I := 1 to ParamCount do
    ShowMessage('Param No ' + IntToStr(I) + ' Value = ' + ParamStr(I));
end;


HTH

Regards,
Mohammed
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:ThoseBug
ID: 11812778
sorry for the ignorance..but where i can do this code and how to call from antoher delphi program...when i try to execute from the run project1.exe someparameter i have a error...
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 11815100
My example is for a console application, you didnt state if your program used a gui or not.
but my example can easily be adjusted to fit.

if you want to run your project from the ide you need to pass the parameters.
goto menu "Run" "Parameters"
insert some test parameters into the parmaeters field:
{ -s <servername> -db <databasename> -u <username> -p <password> }

-s 127.0.0.1 -db dbname -u myname -p pass

only other way to test is from the cmd prompt manually.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11815102
If you program is called MyProgram.exe
you can run it with parameters typing:
"MyProgram db=testdb user=john pass=abcd"

from Delphi you set the parameters in Run / Parameters

from inside your application you can access the WHOLE command line with CmdLine function - it returns you a string.
you can also access parameters like this

  var
    param1  : String;
  begin
    param1 := ParamStr(1);

  in this case param1 = 'db=testdb'

  ParamCount returns you the number of parameters.
  ParamStr(0) - this is the file full name. The same as Application.ExeName.

  You can handle the command line parameters in OnCreate of the main form.

  Hope this helps
0
 
LVL 3

Expert Comment

by:MikProg
ID: 11817820
The best way is to use TStringList to hold program parameters

        ProgramParams:=TStringList.Create;
        for i:=1 to ParamCount do
        begin
            ProgramParams.Add(ParamStr(i);
        end;
// here you can assign defult values to parameters not assigned from command line
       if ProgramParams.Values['-db']='' then
            ProgramParams.Values['-db']:=DefaultDatabaseName;
// use these parameters
        servername := ProgramParams.Values['-s'];
        databasename  :=  ProgramParams.Values['-db'];
// so on so forth ...

// now you can save last run parameters
       ProgramParams.SaveToFile('ProgramName.cfg');
       
0
 

Expert Comment

by:krypto2000
ID: 11818753
Just make a new applicacation with a listView with two columns and viewStyle=report

The most important think is to know the ParamStr and the ParamCount variables...

Here is the procedure :

procedure TForm1.FormCreate(Sender: TObject);
var i: integer; AddedItem: TListItem;
begin
   for i := 1 to ParamCount do
      begin
      AddedItem := ListView1.Items.Add;
      AddedItem.Caption := 'Param[' + IntToStr(i) + ']';
      AddedItem.SubItems.Add(ParamStr(i));
      end;
end;
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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