Python handles example analysis of optpars usage of the command line parameter module

  • 2020-11-03 22:30:21
  • OfStack

This article illustrates Python's use of the command line parameter module optpars. To share for your reference, the details are as follows:

optpars is a module used in python to process command line parameters, which can automatically generate help information of the program. It is powerful, easy to use, and can conveniently generate standard, Unix/Posix specification command line description.

use add_option() To add options, use parse_args() to parse the command line.

add_option parameters ()

The first parameter represents the abbreviation of option and is guided by a single underscore, such as -ES21en and -ES22en, which can only be used with a single letter and can be used in uppercase;

The second parameter represents the full spelling of option and is guided by an underscore between the two, for example --file, --Opencv_version;

The first and second parameters can be used separately or simultaneously, but one of them must be guaranteed.

Starting from the third parameter are named parameters, which are optional parameters and commonly used:

type=: represents the type of value of the input command line parameter, default is string, can be specified as string, int, choice, float, complex one of them;
default=: represents the default value of the command parameter;
metavar=: displayed in the help document to prompt the user for the desired command parameters;
dest= : Specifies the name of the member in the options object. If the dest parameter is not specified, the value of the options object is accessed using the command line parameter name.
help=: Information displayed in the help document;

Parse command line


(options, args) = parse.parse_args()

Or in the main(argv) Function:


(options, args) = parser.parse_args(argv)

options, 1 object (optpars.Values), holds the value of the command-line arguments. Use the command line parameter name, such as file, to access its corresponding value: options.file;
args, is a list of positional arguments;

optparse use


import sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f','--file',type=str,default='./image',help='file path of images',dest='file_path')
parser.add_option('--weights','-w',type=str,default='./weights_saved',help="file location of the trained network weights")
parser.add_option('--iterations','-i',type=int,default=10000,help='iteration time of CRNN Net')
parser.add_option('--gpu','-g',type=int,default=0,help="gpu id")
def main(argv):
  (options, args) = parser.parse_args()
  (options, args) = parser.parse_args(argv)  # both OK
  print 'file path of images: ' + options.file_path
  print "file location of the trained network weights: " + options.weights
  print 'iteration time of CRNN Net: ' + str(options.iterations)
  print 'gpu id: ' + str(options.gpu)
if __name__ == '__main__':
 main(sys.argv)

View the help documentation:


python test.py -h

Display:

[

Usage: test.py [options]
Options:
-h, --help show this help message and exit
-f FILE_PATH, --file=FILE_PATH
file path of images
-w WEIGHTS, --weights=WEIGHTS
file location of the trained network weights
-i ITERATIONS, --iterations=ITERATIONS
iteration time of CRNN Net
-g GPU, --gpu=GPU gpu id

]

Enter the command line parameters:


python test.py -f ../tensorflow/train_image -w ../tensorflow/weights -i 5000 -g 2

Output:

[

file path of images: ../tensorflow/train_image
file location of the trained network weights: ../tensorflow/weights
iteration time of CRNN Net: 5000
gpu id: 2

]

More about Python related content interested readers to view this site project: "Python mathematical operation skills summary", "Python string skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using skills summary", "introduction to Python and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful for Python programming.


Related articles: