Command line arguments

Python Programming Training Certification

Flexible Hours

100 Assignments

Instructor Led online Training

50 LMS Access

24X7 Support

100% Skill Level

Enquire Now

4.9 out of 1000+ Ratings
Best Python Institute for Learning Python Course & Training, Live Project Training in Python with Django, Data Science and AI, Interview Assistance, Expert Coaching Trainers. Python Certification & Interview Assistance! Get free demo now!

Course Overview

Python is one of the world’s top programming languages used today and Python training has become the most popular training across individuals. Training Basket’s Python Training & Certification course covers basic and advanced Python concepts and how to apply them in real-world applications.Python is a flexible and powerful open-source language that is easy to learn and consists of powerful libraries for data analysis and manipulation. Our Python training course content is curated by experts as per the standard Industry curriculum. The curriculum, coding challenges and real-life problems cover data operations in Python, strings, conditional statements, error handling, shell scripting, web scraping and the commonly used Python web framework Django. Take this Python training and certification course and become job-ready now.

Command line arguments

The Python supports the programs that can be run on the command line, complete with command line arguments. It is the input parameter that needs to be passed to the script when executing them.

It means to interact with a command-line interface for the scripts.

It provides a getopt module, in which command line arguments and options can be parsed.

What is argument passing?

The command ls is often used to get a summary of files and folders present in a particular directory.

Why to use argparse?

It means to communicate between the writer of a program and user which does not require going into the code and making changes to the script. It provides the ability to a user to enter into the command-line arguments.

Access command line arguments

The Python sys module provides access to command-line arguments via sys.argv. It solves the two purposes:

Python sys module

It is a basic module that was shipped with Python distribution from the early days on. It is a similar approach as C library using argc/argv to access the arguments. The sys module implements command-line arguments in a simple list structure named sys.argv.

Each list element represents a single argument. The first one — sys.argv[0] — is the name of Python script. The other list elements are sys.argv[1] to sys.argv[n]- are the command line arguments 2 to n. As a delimiter between arguments, space is used. Argument values that contain space in it have to be quoted, accordingly.

It stores command-line arguments into a list; we can access it using sys.argv. This is very useful and a simple way to read command-line arguments as String.

  • import sys
    print(type(sys.argv))
    print(‘The command line arguments are:’)
    for i in sys.argv:
    print(i)

Python getopt module

The Python getopt module extends the separation of the input string by parameter validation. Based on getopt C function, it allows both short and long options, including a value assignment.

It is very similar to C getopt() function for parsing command line parameters.

It is useful in parsing command line arguments where we want the user to enter some options.

Code

  • import getopt
    import sys
    argv = sys.argv[1:]
    try:
    opts, args = getopt.getopt(argv, ‘hm:d’, [‘help’, ‘my_file=’])
    print(opts)
    print(args)
    except getopt.GetoptError:
    # Print a message or do something useful
    print(‘Something went wrong!’)
    sys.exit(2)

Python argparse module

It offers a command-line interface with standardized output, whereas the former two solutions leave most of the work in your hands. argparse allows verification of fixed and optional arguments with a name checking as either UNIX or GNU style. It is the preferred way to parse command-line arguments. It provides a lot of option such as positional arguments, the default value for arguments, helps message, specifying the data type of argument etc.

It makes it easy to write the user-friendly command-line interfaces. It automatically generates help and usage messages and issues errors when a user gives invalid arguments to the program.

getopt.getopt method

This method is used for parsing the command line options and parameter list.

Syntax:

  • getopt.getopt(args, options, [long_options])

args– It is an argument list that needs to be parsed.

options– A string of option letters that the script wants to recognize, with options that require an argument which should be followed by a colon(:).

long_options(optional)– It must be a string with names of the long options, which should be supported.

  • This method returns a value consisting of two elements, i.e. list of (option, value) pairs, list of program arguments left after option list was stripped.
  • Each option-and-value pair are returned as an option as its first element, prefixed with a hyphen for short options (e.g.,’-x’) or two hyphens for long options (e.g., ‘–long-option’).

Exception getopt.GetoptError

This exception arises when an unrecognized option is found in the argument list or when any option requiring an argument is given none.

The argument to the exception is a string that indicates the cause of the error. The attributes msg and opt to give the error message and related option.

Code

  • #!/usr/bin/python
    import sys, getopt
    def main(argv):
    inputfile = ”
    outputfile = ”
    try:
    opts, args = getopt.getopt(argv,”hi:o:”,[“ifile=”,”ofile=”])
    except getopt.GetoptError:
    print ‘test.py -i <inputfile> -o <outputfile>’
    sys.exit(2)
    for opt, arg in opts:
    if opt == ‘-h’:
    print ‘test.py -i <inputfile> -o <outputfile>’
    sys.exit()
    elif opt in (“-i”, “–ifile”):
    inputfile = arg
    elif opt in (“-o”, “–ofile”):
    outputfile = arg
    print ‘Input file is “‘, inputfile
    print ‘Output file is “‘, outputfile

    if __name__ == “__main__”:
    main(sys.argv[1:])

Output:

  • $ test.py -h
    usage: test.py -i <inputfile> -o <outputfile>

    $ test.py -i BMP -o
    usage: test.py -i <inputfile> -o <outputfile>

    $ test.py -i inputfile
    Input file is ” inputfile
    Output file is “

How to use command line arguments in python?

Module Use Python version
sys All arguments in sys.argv (basic) All
argparse Build a command line interface >= 2.3
docopt Created command line interfaces >= 2.5
fire Automatically generate command line interfaces (CLIs) All
optparse Deprecated < 2.7

Docopt

Docopt is used to create command line interfaces.

  • from docopt import docopt
    if __name__ == ‘__main__’:
    arguments = docopt(__doc__, version=’Example 1′)
    print(arguments)

Fire

Python Fire automatically generates a command line interface; you only need one line of code. Unlike the other modules, it works instantly.

You don’t need to define any arguments; all the methods are linked by default.

To install it type:

  • pip install fire

Define or use a class:

  • import fire
    class Python(object):
    def hello(self):
    print(“Hello”)
    def openfile(self, filename):
    print(“Open file ‘” + filename + “‘”)

    if __name__ == ‘__main__’:
    fire.Fire(Python)

You have the options matching to the class methods:

  • python example.py hello
    python example.py openfile filename.txt

s

Recently Trained Students

Jessica Biel

– Infosys

My instructor had sound Knowledge and used to puts a lot of effort that made the course as simple and easy as possible. I was aiming for with the help of the ZebLearn Online training imparted to me by this organization.

Richard Harris

– ITC

I got my training from Zeblearn in the Python Certification Training, I would like to say that say he is one of the best trainers. He has not even trained me but also motivated me to explore more and the way he executed the project, in the end, was mind-blowing.


Candidate’s Journey During Our Training Program

Card image cap

Expert’s Advice & Selection of Module

Choosing the right type of module for the training is half the battle & Our Team of experts will help & guide you.

Card image cap

Get Trained

Get Trained & Learn End to End Implementation from our Expert Trainer who are working on the same domain.

Card image cap

Work on Projects

We Do make our student’s work on multiple case studies , scenario based tasks & projects in order to provide real-time exposure to them.

Card image cap

Placements

We have a dedicated placement cell in order to provide placement assistance & relevant interviews to our candididates till selection

Placement Partner

×

Leave your details

×

Download Course Content



wop;[\]