CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – Parsing command line

Most of the Applications support command line Arguments, to do some actions based on the given values. For example, when you type “explorer .” at Windows command prompt; it will open Windows Explorer showing the Current Folder content. In this article, I am going to explain the parsing command lines using a sample Python program.

In my previous Article “Python – Lists – A walk through with Examples“, we have discussed lists; the creation of lists, and access / modifying its elements. We are going to use lists in this Article; because, Python treats, command line as a list. That means Python stores the values entered at the command line in a list.

How to access the command line.?

Python stores the command line as a list variable, which is “argv” in the “sys” library.

The first argument is always, the calling Application itself. So, the actual arguments start from the 1st position (2nd element). The below statement displays the list of arguments passed to the calling Application:

print(sys.argv)

Observe the results; it displays command-line arguments as a list. Remember that, when you pass command line arguments, each argument should be separated by a space (” “). If there is a string you won’t pass, that should be enclosed in double-quotes (“) or in a single quote (‘). For example, when you pass the arguments: One ‘Hello World!’ 2 8.0 to the Application, and print the arguments; it will print like below:

[‘<<your-application-name>>.py’, ‘One’, ‘Hello, World’, ‘2’, ‘8.0’]

Please note that I got the below message when I use double quotes; so, I used a single quote to pass the string ‘Hello, World!’.

bash: !": event not found

How to parse the command line?

Before you write code to parse the command line; you need to know that; command line contains command-line argument(s) and its’ value(s). So you need to define, how to differentiate this. Usually, command-line arguments are prefixed with either “/” or “-” symbols. You can define whatever prefix you want to. In this example, I am considering the “-” symbol to differentiate the command line argument with its’ value.

Now, your code should go through each element of the command line, and do the below:

  • Verify whether the element is an argument or a value
  • If it is an argument, verify whether it has a value provided; then do the defined action
  • If any argument or value, which is not defined; display the “WARNING: INVALID ARGUMENT” message. That means, the argument should be one of the below; with a single value; otherwise, it should throw a WARNING.

Lets’ write our sample program to take below two arguments from the command line:

-PRINT <<message>> – this prints the given message.

-EVAL <<expression>> – evaluate the given mathematical expression.

Our program code looks like below:

When you run the code, the result is like below:

$ python cmdargs.py -PRINT 'Hello, World!' -EVAL 10/2
['cmdargs.py', '-PRINT', 'Hello, World!', '-EVAL', '10/2']
Hello, World!
5
$

We will discuss more Python in my upcoming Articles.

/Shijit/

Python – Parsing command line

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top