Why the Debt Ceiling Raise does not mean America is Out of the Woods

After the latest raising of the US’s debt ceiling (Now standing at over $31 trillion according to the official, published figure though some believe it to be much higher.), it seems that the federal…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Making a command line tool using Python

I was going through few references from feedback and that is when I found this awesome module called argparse. It's a parser for command line options, arguments and sub-commands which makes it easy to write user-friendly command line tool(s). So this is going to be a quick intro to argparse module and at the end of this article I will link to the final upgraded code for counting files.

Enough chitchat, lets create a file called calc.py and start writing some code.

Above code block is self-explanatory, we first created a parser object from ArgumentParser class and while at it, we provided a description as well. We will be adding some options to our script where commented, but before we can use them we need to parse it first. Which we did next. Finally we can use the args variable to access our option values. But wait, even though we haven't added any options to our script yet, we get a default flag option -h or --help. This flag prints help screen with description and all the available command parameter options. So if we run this:

We will get output like this:

Now let’s add some arguments (aka options) to out calculator script. There are two kind of arguments that the parser can parse.

We will have two operations in our calculator — add and sub and we will take this input as first parameter to our script. And we will take two more parameter value to operate on, lets just call them - a and b. All of these will be positional arguments. So after # add command line options here line we add following code:

Now if we run the script with -h flag again we should see something like this:

Awesome now we can take command and parameter values like this:

And if we miss any of these three parameter script will raise error as they are positional arguments.

Our calculator script is almost ready we just need to add some logic there. Lets do that:

All done! Let’s run our script:

Wait, what the heck just happened!!! Well the answer is simple! When we added a and b argument we didn't specified any expected type for them. So by default parser will parse them as string. As a result even though we entered numbers as input, they were parsed as string. And substraction between two string is unsupported in python, hence this error. Let's add type to our parameters to fix this issue. Update the following lines in code

Now if we run the command again, we get the result!

Sweet!

Here is the final code:

However if you want you can start playing with above script too! Try to add more commands to our calculator like multiply and divide etc! Let me know whether you were successful or got stuck on any point, would love to hear from you. :)

Add a comment

Related posts:

Sobre o Projeto

Em uma oportunidade anterior, expliquei detalhadamente como criar um bot do Telegram que controlasse os LEDs conectados a placa. Hoje meu objetivo é apresentar a vocês um projeto de monitoramento…

THERMACOIN ICO

Thermacoin powers a revolutionary Peer-To-Peer creative works sharing and registry Platform for the Mechanical Construction industry. The Mechanical ecosystem will work on a decentralized ledger…

Our home

Your lawn overrun by coarse wild weed from months of neglect and indifference but you are more worried that I did not fertilise my flowerbed. Your living room, fine leather sofas embellished by…