Understanding Command Line Arguments and How to Use Them

  • September 8, 2017
  • Read 2,060 times
 

What are Command Line Arguments and why would you use them?

Command line arguments are extra commands you can use when launching a  program so that the program's functionality will change.  Depending on the program, these arguments can be used to add more features that includes specifying a file that output should be logged to, specifying a default document to launch, or to enable features that may be a bit buggy for normal use.

In order to understand what a command line argument is, we should show an example of how a program is normally launched. In Windows, when you start a program by clicking on it's icon, or shortcut, it simply runs an executable and the program runs with whatever default settings are programmed into it. For example, the C:\Windows\system32\notepad.exe program is the Windows Notepad. To launch it, you would simply type notepad into the search field and press enter or click on its icon.  All this does is start the Notepad.exe program as shown by the Target field in the shortcut properties below. Note, in the shortcut Target field below, %windir% means the folder Windows is installed into, which is usually C:\Windows on most PCs.

Notepad Shortcut

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

It is important to note that you must add a space between the program you want to run and the command line argument. For example, notepad.exe/s is not a valid argument because it does not contain a space. Instead you must add a space so it looks like notepad.exe /s.

How to use Command Line Arguments

Program's arguments are named command line arguments because they are typically used when you type them within a console, command prompt, or terminal. In a graphical user interface, when you want to move a file from one folder to another, you just drag it. On the other hand if you want to move a file from a console command, you need to specify the file and location using command line arguments, which are typed after the program name.

For example, in a Windows Command Prompt, to execute a program you must type its name and press Enter on the keyboard. These programs typically take arguments in order to work properly and these arguments are called command line arguments. Let's use the move program as an example.This command requires two command line arguments; the file to move and where you wish to move it. Therefore a valid use of the move program would be move test.txt c:\, which would move the test.txt file to the root of the C:\ drive as shown below.

Move Example

This same behavior works in Terminal on macOS and Linux as well as any other console environment. You type in the name of the program, followed by its arguments that tell the program how it should operate.

Now, just because they are called command line arguments does not mean they can only be used from a console.  For example, in Windows you can modify a shortcut so that it permanently uses a particular argument in order to perform a certain behavior every time you launch the program from the icon.

Let's use the above Notepad shortcut as an example. In the shortcut properties, the program that will be executed is listed in the Target field.  Now let's say that we wanted to have Notepad automatically open a particular file every time it launches. To do this, we would add the name of the file to be opened as an argument to Notepad. We do this by adding the document name, which in this case is C:\mynotes.txt, after notepad.exe in the Target field as shown below.

Now every time you launch Notepad, it will automatically open the mynotes.txt file.

Why would you use command line arguments?

Most people will use their computer and never use a command line argument at all. On the other hand, power users, programmers, and IT professionals may use them on a daily basis. For example, when I use the computer I always have a Windows Command Prompt open simply because I find it faster and easier to type in certain commands than to do it via the Windows graphical user interface.

Command Line Arguments are also extremely useful when used in batch files and login scripts as they allow you to create very complex tasks through command line programs.

WIth that said, command line arguments are not only used in a console. You can use them directly within Windows by just entering the command into the search field followed by the argument. I use notepad a lot, so I will use it for another example. I tend to have a lot of things going on at one time, so I keep notes in various text files in a particular folder on my computer. If I wanted to open a particular text file, I would just type notepad d:\notes\todo.txt in the Start Menu's search field, press Enter on the keyboard, and my text file opens. On the other hand, performing this task via the standard user interface of Windows would take longer for me.

Open a text file

As you can see there are many reasons why you would use command line arguments and each person will use them differently. As you start using them and getting familiar with them, you will start to use them more and more.

How to find a programs available Command Line Arguments

Now that you know how to use a command line argument, you may be wondering how you can get a list of arguments that are available in a program. Usually you can find out a programs arguments by typing either /?, -h, -?, or --help after a command, which will hopefully print out a programs available command line arguments. Unfortunately, there is no set method that tells a program to list its arguments and different developers will display them differently.

In my experience, though, I have found that the /? command works on all Microsoft programs that are shipped with Windows. Simply type the command followed by /? and the program should display its help file. This can be seen in the example below where we see the command line arguments for the Windows del command.

In Linux and macOS, the programs shipped with the OS will typically use a --help argument to show the help file as can be seen below.

Linux Command Line

For other programs that you download from the Internet, figuring out the command line arguments are going to be more difficult. I usually start with the /?, -h, -?, or --help arguments to see if it will cause the program to spit out a help file. If it does not, I then resort to either reading a manual or searching on Google.

Users who read this also read:

  • How to Restore a Missing Windows Desktop Image
    How to Restore a Missing Windows Desktop

    When using Windows, under certain circumstances the Windows Desktop will not be shown. When this happens, when you login to Windows instead of seeing the desktop, start menu, or your normal icons, you are shown a blank screen instead. This tutorial will provide steps on how to restore a missing Windows desktop.

  • How to see hidden files in Windows Image
    How to see hidden files in Windows

    By default Windows hides certain files from being seen with Windows Explorer or My Computer. This is done to protect these files, which are usually system files, from accidentally being modified or deleted by the user. Unfortunately viruses, spyware, and hijackers often hide there files in this way making it hard to find them and then delete them.

  • How to show hidden files in Windows 7 Image
    How to show hidden files in Windows 7

    Windows 7 hides certain files so that they are not able to be seen when you exploring the files on your computer. The files it hides are typically Windows 7 System files that if tampered with could cause problems with the proper operation of the computer. It is possible, though, for a user or piece of software to set make a file hidden by enabling the hidden attribute in a particular file or ...

  • How to fix crashes when you start Notepad++ Image
    How to fix crashes when you start Notepad++

    Notepad++ is a terrific Windows notepad replacement, but just like any application, it sometimes has a problem and no longer starts properly. If you find that Notepad++ is crashing when you start it, in my experience there are typically two things that could be wrong; either a problem with a previous session or a problem with an installed plugin.

  • How to remove a Trojan, Virus, Worm, or other Malware Image
    How to remove a Trojan, Virus, Worm, or other Malware

    If you use a computer, read the newspaper, or watch the news, you will know about computer viruses or other malware. These are those malicious programs that once they infect your machine will start causing havoc on your computer. What many people do not know is that there are many different types of infections that are categorized in the general category of Malware.

 

Comments:

blog comments powered by Disqus

Login

Remember Me
Sign in anonymously