Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

d

Command line arguments in D

Farrukh Alam Virk

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Command-line arguments are parameters given on the system's command line, and the values of these arguments are passed on to the program during execution. When a program starts execution without user interaction, command-line arguments are used to pass values or files to it. The arguments are given after the name of the file in the command-line interface. These parameters give direct control and flexibility over the application rather than hard coding the values inside the code.

Example

Press "Run" to see how the command line works. The commands being run are specified in the code widget.

import std.stdio;
void main(string[] args) {
//prinitng the arguments
writeln("The first argument is " ,args[0]);
writeln("The second argument is " ,args[1]);
writeln("The third argument is " ,args[2]);
writeln("The fourth argument is " ,args[3]);
}
Printing the arguments in D

Explanation

  • Line 3: We have a main() function that passes string[] args, an array of strings that carry the arguments from the command line.
  • Lines 5–8: We print the argument passed.

Note: The first index args[0] has the name of the file to be executed, which is by default automatically copied (in this case, hello.d), and the remaining indices consist of the parameters given by the user.

Each index of the array pointing to a string

Example

Let's try another example by copying the ./hello command from below and adding the arguments after that in the terminal given below.

./hello

Next, we can try by passing multiple arguments.

Note: In order to give multiple arguments space is needed in between to separate.

Let's try another command with multiple arguments:

./hello

We may try the above given commands in the below terminal by pressing the "Run" button.

import std.stdio;

void main(string[] args) 
{ 
//printing all the arguments using forloop
    foreach (i, arg; args) 
    {
        writeln("Argument " , i, " : " ,arg);
        
    }
}

Explanation

  • Line 6: We use foreach which calls a function for each element of an array.
  • Line 8: We use writeln function to print the arguments.

We can use the terminal for more practice by giving the arguments we want.

RELATED TAGS

d

CONTRIBUTOR

Farrukh Alam Virk
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring