Then a log, http://zidingyi.blog.51cto.com/10735263/1873566
First, let's see what Parse_args () is returning. He returned two values. One is options, one is args!
#!/usr/bin/env python#coding:utf-8# statistical import sys, osfrom optparse import of standard inputs Optionparserdef opt (): usage = "usage: %prog [options] Arg1 arg2 " parser = optionparser () parser.add_ Option ("-C", "--char", dest= "Chars", action= "Store_true", default=false, help= "Only count chars") parser.add_option ("-W", "--word", dest= "Words", action= "Store_true", default=False, help= "Only count words") parser.add_option ("-L", "--line", dest= "Lines", action= "Store_true", default=false, help= "Only count lines") parser.add_option ("-N", "-- Nototal ", dest= "Nototal", action= "Store_true", default=False, help= "Nototal") options, args = parser.parse_args () return options, args opt () print sys.argv[:] //Print out the script runtime parameters, note and parse_aRGS returns the parameters to do contrast options, args = opt () print options, args //Print out parse The value of the args returned by _args.
Run to perform a bit:
[[email protected] socket]# python 1.py-l/etc/passwd[' 1.py ', '-l ', '/etc/passwd ']//sys.argv return the parameter {' chars ': False, ' Lines ': True, ' nototal ': false, ' words ': false} ['/etc/passwd ']//the previous dictionary returns the value of the options, followed by parentheses for the returned Parse_args parameter value, Note that this value is not the same as the parameter value returned by the SYS module.
This article is from "Custom" blog, declined reprint!
Python---optparse module