Using Command Line Arguments

Command line arguments enable the user to customise the program. You can choose one of these methods for supplying your program with command line arguments in Windows. These examples use three arguments but you can, of course, have just one or many arguments.
  1. Change directory of the Command Prompt to that of your executable and type a command of the form ProgramName argument1 argument2 argument3.
  2. Include the arguments in a shortcut to the executable. To create the shortcut, right click on the program name in File Explorer then select the menu item Create shortcut. Right click on the shortcut and select menu item Properties. Select the Shortcut tab and append to Target a space followed by argument1 argument2 argument3.
  3. With your program in the Lazarus editor, select menu item Run > Run Parameters... and into the edit box with caption Command line parameters (without application name) type argument1 argument2 argument3.

Using Command Line Arguments in Pascal

The first example demonstrates the use of the inbuilt variables ParamCount and ParamStr. The pathname of the executable is held in ParamStr(0). A copy of the command, input and output follows the code.

program CommandLineDemo1;
var
  ParamNum: integer;
begin
  write('Please enter the number of the parameter required: ');
  readln(ParamNum);
  if ParamNum <= ParamCount then
    writeln('Your chosen param: ', ParamStr(ParamNum))
  else
    writeln('Not enough arguments supplied');
  readln;
end.    

Command, input and output:

C:\Working\CommandLineDemo>CommandLineDemo1 one two three four five
Please enter the number of the parameter required: 4
Your chosen param: four

The second demonstration shows how you can make use of an argument if supplied. A copy of the commands and output follows the code.

program CommandLineDemo2;
uses
  SysUtils;
var
  i, InputNum : Integer;
  Fact: int64 = 1;
begin
  if ParamCount < 1 then
    begin
      write('Please enter an integer in the range 1 to 20 to find its factorial. ');
      readln(InputNum);
    end
  else
    InputNum := StrToInt(ParamStr(1));
  i := InputNum;
  while i > 1 do
    begin
      Fact := Fact * i;
      dec(i);
    end;
  writeln(InputNum, '! = ', Fact);
  readln;
end.
    

Commands and output:

C:\Working\CommandLineDemo>CommandLineDemo2 5
5! = 120


C:\Working\CommandLineDemo>CommandLineDemo2
Please enter an integer in the range 1 to 20 to find its factorial. 5
5! = 120

Using Command Line Arguments in C++

The C++ demonstrations in this section are equivalents of the Pascal ones above so we do not repeat the copies of the output here.

In order to supply arguments to a program in the Code::Blocks editor, select menu item Project > Set projects' arguments then enter the arguments separated by spaces in the box. Alternatively, use either of the first two methods at the top of the page, which apply to Windows executables created with any language.

The first example demonstrates the use of argc and argv. The pathname of the executable is held in argv[0]. (You can change these identifiers but you must not change their types).

#include <iostream>
#include <conio.h>

using namespace std;

int main(int argc, char **argv)
{
  int paramNum;
  cout << "Please enter the number of the parameter required: ";
  cin >> paramNum;
  if (paramNum <= argc)
    cout << "Your chosen param: " << argv[paramNum] << endl;
  else
    cout << "Not enough arguments supplied" << endl;
  _getch();

  return 0;
}
    

The second C++ demonstration shows how you can make use of an argument if supplied.

#include <iostream>
#include <stdint.h>
#include <cstdlib>
#include <conio.h>

using namespace std;

int main(int argc, char **argv)
{
  int i, inputNum;
  int64_t fact = 1LL;
  if (argc < 2)
  {
    cout << "Please enter an integer in the range 1 to 20 to find its factorial. ";
    cin >> inputNum;
  }
  else
    inputNum = strtol(argv[1], NULL, 10);
  i = inputNum;
  while (i > 1)
  {
    fact *= i;
    i--;
  }
  cout << inputNum << "! = " << fact << endl;
  _getch();

  return 0;
}
    

Using Command Line Arguments in C#

In C# the command line arguments in the args array are zero based and the number of arguments supplied is args.Length. (You can change this identifier but its type must be array of string). In order to supply arguments with your code in the SharpDevelop editor, select menu item Project > Project Options... then the Debug tab. Type the command line options, separated by spaces, in the appropriate edit box.

The first example demonstrates the use of the array of argument strings. The C# demonstrations in this section are equivalents of the Pascal examples shown first so we do not repeat the copies of the output here.

using System;

namespace CommandLineDemoCS
{
  class Program
  {
    public static void Main(string[] args)
    {      
      int paramNum;
      string strParamNum;    
      Console.Write("Please enter the number of the parameter required: ");
      strParamNum = Console.ReadLine();
      paramNum = int.Parse(strParamNum);
      if  (paramNum <= args.Length)
        Console.WriteLine("Your chosen param: " + args[paramNum - 1]); // zero based args
      else
        Console.WriteLine("Not enough arguments supplied");
      Console.ReadKey(true);
    }
  }
}

The second C# demonstration shows how you can make use of an argument if supplied.

using System;

namespace CommandLineDemo2CS
{
  class Program
  {
    public static void Main(string[] args)
    {      
      int inputNum, i;      
      string strInputNum;
      long fact = 1;    
      if (args.Length < 1)
      {
        Console.Write("Please enter an integer in the range 1 to 20 to find its factorial. ");
        strInputNum = Console.ReadLine();
        inputNum = int.Parse(strInputNum);
      }      
      else
        inputNum = int.Parse(args[0]);
      
      i = inputNum;
      while (i > 1)
      {
        fact *= i;
        i--;
      }
      Console.WriteLine(inputNum.ToString() + "! = " + fact.ToString());
      Console.ReadKey(true);
    }
  }
}


Programming - a skill for life!

Overviews of site content and general information, tips and tricks for student programmers