We are using PyCharm for this tutorial as we have been using in every other Tutorial of this series.
If you haven't downloaded it yet, then download it by clicking on the Download PyCharm. This will take you to the PyCharm official site. For installation guidelines, check Downloading Python and PyCharm Installation tutorial.
Today we are going to learn how we can make command-line utility in python and the benefits and uses of making a command-line utility. Let us get a brief overview of the command-line utility.
A command-line utility is a way of giving operating system instructions using lines of texts. Command-line programs operate via command line or PowerShell. It will interact with a command-line script.
Now let us come to the why part that why we should use the command-line utility in our program. We can call a command line program in python or any other language into a different language program easily as each program has calling support in it for calling the command lines program. So in cases, were are writing a program in some other language, but we want to perform a task in Python and call it in our program, then the command line can help us to do that.
Now we are going to discuss how part of this tutorial. For creating a Command Line Utility In Python, first import two modules i.e., argsparse and sys. argsparse helps us to get command-line arguments in our program, and the sys module helps us to import the code we wrote using argparse onto the console. For more details and descriptions about these modules, you can read the python documentation for these modules.
import argparse import sys
Python comes with several different libraries that allow us to write a command-line interface for our scripts, but the standard way for creating a CLI in Python is by using the Python argparse module. The argparse module helps us to parse the arguments passed with the script and process them more conveniently. One of the advantages of using the argparse module is that it makes it easy to write user-friendly command-line interfaces.
We can use the Python argparse module to create a command-line interface by following these steps:
When we execute .parse_args(), we will get the Namespace object that contains a simple property for each input argument received from the command line. In this tutorial, we are going to use the Argumentparser class available in the argparse module. We fill ArgumentParser with information about program arguments by making calls to the add_argument() method.
Python provides the sys module that gives us independence from the host machine Operating System and allows us to operate on an underlying interpreter, irrespective of its being a Linux or Windows Platform. With the help of the sys module, we can access system-specific parameters and functions. It provides different functions used to manipulate different parts of the Python Runtime Environment. To use the sys module, we have to import it so that it brings required sys module dependencies into our scope.
import argparse import sys def calc(args): if args.o == 'add': return args.x + args.y elif args.o == 'mul': return args.x * args.y elif args.o == 'sub': return args.x - args.y elif args.o == 'div': return args.x / args.y else: return "Something went wrong" if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--x', type=float, default=1.0, help="Enter first number. This is a utility for calculation. Please contact harry bhai") parser.add_argument('--y', type=float, default=3.0, help="Enter second number. This is a utility for calculation. Please contact harry bhai") parser.add_argument('--o', type=str, default="add", help="This is a utility for calculation. Please contact harry bhai for more") args = parser.parse_args() sys.stdout.write(str(calc(args)))
I accepting your challenge I made a solution below import argparse import sys def calc(args): """This is faulty a calculator which will correctly solve all the problems except the following ones: 45 * 3 = 555, 56+9 = 77, 56/6 = 4""" if args.x ==45 and args.y == 3 and args.o == 'mul': return 555 elif args.x ==56 and args.y == 9 and args.o == 'add': return 77 elif args.x ==56 and args.y == 6 and args.o == 'div': return 4 elif args.o == "add": return args.x + args.y elif args.o == "sub": return args.x - args.y elif args.o == "mul": return args.x * args.y elif args.o == "div": return args.x / args.y else: return "Something went wrong......" if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--x', type=float, default=1.0, help='Enter the first number.') parser.add_argument('--y', type=float, default=3.0, help='Enter the second number.') parser.add_argument('--o', type=str, default='add', help='Enter the Operator.') args = parser.parse_args() sys.stdout.write(str(calc(args))) Hope my answer is correct.........
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here