[Tutorial] How to create a Commando Prompt

Discussion in 'Programming' started by ProgMania, Mar 15, 2007.

  1. #1
    This is a tutorial how to make your own very simple custom command prompt in Delphi.

    There is not support for executing files, listing directories or things like that, just run your own commands. Maybe I'll extend the totorial later and explain how to do that.

    This tutorial is aimed at Delphi beginners.

    // First we need to define the name of the program.
    // In this case it's "MyCommandPrompt".
    program MyCommandPrompt;
    
    // Here we have to specify what type of application we
    // are going to make. This app is a console application.
    {$APPTYPE CONSOLE}
    
    // In this place we must tell Delphi what libraries to
    // use and include with our program.
    uses
      SysUtils, dialogs, windows;
    
    // Here we gonna define our variables and constants.
    var
      running: boolean;
      command: string;
      cLocn: coord;
      nWritten: dword;
    const
      version: string = '1.0';
      author: string = 'MyName';
    
    // This is here where all the fun begins :-)
    // This is the main code for our console application.
    begin
    // First we have to tell our program that it's running.
    // We do that by setting the "running" boolean variable to true.
      running:=true;
    
    // Prints the application-name and the current version to the screen.
      writeln('MyCommandPrompt v'+version);
    
    // This is our main loop. It's here we gonna make all our code.
    // I'll try to explain a bit: While "running" is = true our
    // program is running. If it's false then our program
    // will terminate.
      while running=true do
        begin
          write('$'); // Print "$" to the screen.
          readln(command); // Save the "command" variable.
    
    // Here we gonna run our commands.
    
          // The about command that prints the application-name,
          //version and author to the screen.
          if command='about' then
            begin
              writeln('MyCommandPrompt v'+version);
              writeln('Written by: '+author)
            end
    
          // The exit command.
          else if command='exit' then
            running:=false
    
          // Clear the screen.
          // Basicly it just fills the screen with spaces.
          else if command='clear' then
            begin
              cLocn.X := 0;
              cLocn.Y := 0;
              FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE ), ' ', 500 * 120, cLocn, nWritten);
              SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE ), cLocn);
            end
    
          // Your own command.
          // This is just an example, how to make your own commands.
          else if command='mycommand' then
            writeln('This mycommand.')
    
          // If the command doesn't exist then print an error message.
          else
            writeln('Unknown command !');
    
        end;
    end.
    
    // Created for http://www.progmania.dk
    Code (markup):

    Feel free to post any comments or questions.
     
    ProgMania, Mar 15, 2007 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Just a fast question , i know that delphi is old .

    anyone using it these days ? and in what kind of programming ?
     
    commandos, Mar 15, 2007 IP
  3. Xangis

    Xangis Active Member

    Messages:
    182
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    78
    #3
    Wow. I have never in my life seen Delphi code before today, and I have an ex-g/f that's w Delphi programmer... it looks vaguely similar to Python.
     
    Xangis, Mar 15, 2007 IP
  4. ProgMania

    ProgMania Active Member

    Messages:
    632
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    70
    #4
    You can create good and powerful applications in Delphi though..
     
    ProgMania, Mar 16, 2007 IP
  5. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Like ?

    What Delphi is specialized in ?
     
    commandos, Mar 16, 2007 IP
  6. ProgMania

    ProgMania Active Member

    Messages:
    632
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Here you go :)
     
    ProgMania, Mar 18, 2007 IP
  7. erkanbs

    erkanbs Active Member

    Messages:
    207
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #7
    its still popular. i know too many people using it.
     
    erkanbs, Mar 18, 2007 IP
  8. ProgMania

    ProgMania Active Member

    Messages:
    632
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    70
    #8
    Yeah me too Erkan. Its good and easy if you want to create windows apps.
     
    ProgMania, Mar 18, 2007 IP
  9. Xangis

    Xangis Active Member

    Messages:
    182
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    78
    #9
    For Windows apps I'm hopelessly addicted to wxWidgets. Not only does it make porting to Linux and MacOS trivial, the interface is FAR FAR easier to comprehend than Win32 or MFC due to the consistency. That's the biggest thing that always bugged me about using Win32 - the fact that doing the same thing with different controls (i.e. a slider vs. spin control) used wildly different settings and arguments.

    Most people I've heard mention using Delphi say that they like it though.
     
    Xangis, Mar 18, 2007 IP
  10. bigspaces

    bigspaces Guest

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    As am I. Although lately I have just made *every* interface into a web app..
     
    bigspaces, Mar 19, 2007 IP