Command-Line Options and the std.getopt Module

In this lesson, you will learn about command-line options and the std.getopt module.

We'll cover the following

In the previous lesson, you learned all there is to know about the parameters and the return value of main(). However, parsing the arguments is a repetitive task. The std.getopt module is designed to help with parsing the command line options of programs.

Command-line options #

Some parameters like “world” and “hello” are purely data for the program to use. Other kinds of parameters are called command-line options and are used to change the behaviors of programs. An example of a command-line option is the -l option.

Command-line options make programs more useful by removing the need for a human user to interact with the program to make it behave in a certain way. With command-line options, programs can be started from script programs and their behaviors can be specified through command-line options.
Although the syntax and meanings of command-line arguments of every program is specific to that program, their format is somewhat standard. For example, in POSIX, command-line options start with -- followed by the name of the option, and values come after = characters:

# ./deneme --an-option=17

Get hands-on with 1400+ tech skills courses.