Python CMD | Python Command Line Arguments module - codebugfree

Python CMD and command line arguments

In this section you will come right place to learn the python CMD. Actually Python CMD is also known as Command Line Arguments. With the help of Argparse module make the command line Arguments.

Command line program is a very interesting things. Any programmer never ignore the command line arguments. There is a problem with the beginner programmer. They don't know the process of making the command line Arguments. 

Advance programmer has easy to make the command line argument. We teach the beginner. We will make the two project of command line arguments. first one is calculator and second one is mistake calculator.

First project gives the accurate answer of our mathematics problem. Interesting fact is that the second project does not give the accurate answer of our mathematics problem. So it is also called a faulty calculator.

In this project we use the python CMD module that is Argparse. In the first paragraph i am already give the short description of this python module. N0w we see the CMD program with describe the one by one line of code.

import argparse
import sys
def calcu(args):
    if args.c=='add':
        return args.a + args.b 
    elif args.c=='sub':
        return args.a - args.b
    elif args.c=='mul':
        return args.a * args.b
    elif args.c=='div':
        return args.a / args.b
    else:
        return "something wrong in value or number"
if __name__ == "__main__":
    cmd_make = argparse.ArgumentParser()
    cmd_make.add_argument('--a',type=float,default=1.0,help="Enter a 1st number" )
    cmd_make.add_argument('--b',type=float,default=5.0,help="Enter a 2nd number" )
    cmd_make.add_argument('--c',type=str,default='sub',help="enter add,sub,mul,div" )

    args = cmd_make.parse_args()
    sys.stdout.write(str(calcu(args)))

First we import the Argparse and sys. Sys is use to print the command line argument in computer screen. First if name == main run the program. Use that function to pas the number.. Set the first number and second number and input the +*. with the help of sys print the cmd in calcu function. Check their condition of user what input. Which condition match that print.

Now see the faulty calculator code it gives answer wrong. 

import argparse
import sys
def calcu(faulty):
    if faulty.o =="add":
        return faulty.a + faulty.b +4
    elif faulty.o =="sub":
        return faulty.a - faulty.b -4
    elif faulty.o =="mul":
        return faulty.a * faulty.b *4
    elif faulty.o =="div":
        return faulty.a / faulty.b /4
    else:
        return "some things went wrong"
if __name__ =="__main__":
    faulty_calculator = argparse.ArgumentParser()
    faulty_calculator.add_argument("--a",type=float,default=1.0,help= "-a enter a first number")
    faulty_calculator.add_argument("--b",type=float,default=5,help="-b  enter a second number")
    faulty_calculator.add_argument("--o",type=str,default="add",help="-o  enter add sub mul div for operation that number")
    faulty = faulty_calculator.parse_args()
    sys.stdout.write(str(calcu(faulty)))

It is same as a first project. Import the same module.. Here is many difference this calculator gives the wrong answer. 

What is use of if __name__ =="__name__":

If this is use than the program is run the first. It is a built in function. If name == main is use than you inside the code. That code is run first and other. 

Example in this our project first make the function and use to the that built in function. There is run the that built in function inside the code and other.

Post a Comment

0 Comments