optparse library and its importance in python - ProgrammerTech
Language:

Search

optparse library and its importance in python

  • Share this:
optparse library and its importance in python

optparse Library

The optparse library is a very important library that helps us design the user's own inputs, for example, if we write a special script for penetration testing, surely here we will need to add inputs in order to explain to the user how to use this script that we programmed, and this explanation is via a library optparse.

The optparse library is more convenient, flexible and powerful for parsing command line options than the getopt module, and optparse uses a more elaborate method for command line parsing.

OptionParser can be instantiated and populated with options and command line parsing, optparse allows users to specify options in the traditional GNU/POSIX syntax as well as generate use and help messages for you eg -h for help.

Options are used to provide additional information for fine-tuning or customizing a program's execution. If not, the options are usually optional, and the program must be able to run well without any options at all.

As an example of good CLI design, consider the humble cp tool for copying files and that it doesn't make sense to try to copy files without providing at least one source and destination and thus cp fails if you run it without arguments. Require no options at all.

Example 1

We will create a file on the desktop called script.py.

from optparse import OptionParser
parser = OptionParser ("""
#Here we write a definition for our script, for example
My Tool is designed for penetration testing
we need the website url and data
      """) 
parser.add_option("-u","--url",dest="target_url",help="target url",deault="https://www.programmer-tech.com")
parser.add_option("-d","--data",dest="target_data",help="target data",deault="my data")
(options,args) = parser.parse_args()
if options.target_url == None and options.target_data == None:
       print (parser.usage)
       exit(0)
else:
      print("{0}:{1}".format(options.target_url,options.target_data))

In our example, we added the options, which is the parser that we defined earlier, and with it we used the required argument, which is:

  1. The -u is an abbreviation for the url requested by the user.
  2. The u-- if we want the user to put the full url.
  3. The dest is treated as a variable.
  4. The help button is to log in for help.
  5. The default is the default.
  6. The -d is an abbreviation for data.
  7. The data-- the complete data mode.

We can add more than two options according to our request and it has the same content as the previous one, and then we specify the required args entries via options.args and set the special entries for it in parser and store all of them in options.args and put a condition through if and set empty values ​​for target data And the target url and we told him to print us using parser.usage and exit the script and put else if the user enters the options and the url together and we told him to print the full values ​​for us and we used the format here, which we explained previously.

Run the script we wrote earlier. When this script is currently running, the result will be.

https://www.programmer-tech.com :my data

We notice that it has printed the default values ​​for us.

 

option group

If we have a group of options specific to a specific thing, we need to put an explanation for it alone, such as large tools, it is necessary to put more than options for it, and we will use the option group in this, we will give the import include for all first by putting an asterisk *.

Example 2

from optparse import *
parser = OptionParser ("""
#Here we write a definition for our script, for example
My Tool is designed for penetration testing
we need the website url and data
      """) 
parser.add_option("-u","--url",dest="target_url",help="target url",deault="https://www.programmer-tech.com")
parser.add_option("-d","--data",dest="target_data",help="target data",deault="my data")
 #---------Advanced Group
new_group = OptionGroup(parser,"advanced options","some options for advanced usage")
new_group.add_option("-p","--payload",dest="target_payload",help="target payload",default="kaissar")
new_group.add_option("-D","--down",dest="target_download",help="target download",default="nidalDZ")
#-------END ADVANCED GROUP
(options,args) = parser.parse_args()
if options.target_url == None and options.target_data == None:
       print (parser.usage)
       exit(0)
else:
      print("{0}:{1}".format(options.target_url,options.target_data))
      print("{0}:{1}".format(options.target_payload,options.target_download))

The result will be displayed as follows

kaissar:nidalDZ

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com