True command-line arguments
-c/--chars: command-line Options
Dest: Defines the variable name for the option, and the value characters is the name of the '-C ' option
The value of Default=false:characters is False, meaning that the command does not have the-C option by default
Help: Explanation of Options section
[[Email protected] wc]# vim 9_optparse.py #!/usr/bin/env pythonimport sys, osfrom optparse import optionparserparser = 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") options, Args = parser.parse_args () Print options, argsdata = sys.stdin.read () chars = len (data) words = len (Data.split ()) lines = data.count (' \ n ') if options.chars: print chars,if options.words: print words,if options.lines: print lines [[email protected] wc]# cat /etc/hosts | python 9_optparse.py -c{' chars ': true, ' lines ': false, ' words ': false} []1534[[email protected] wc]# cat /etc/hosts | python 9_optparse.py -c -w{' chars ': true, ' lines ': false, ' words ': true} []1534 74[[email protected] wc]# cat /etc/hosts | python 9 _optparse.py -c -w -l{' chars ': true, ' lines ': true, ' words ': true} []1534 74 19[[email protected] wc]# cat /etc/hosts | python 9_optparse.py {' chars ': false, ' lines ': false, ' words ': false} []
Options and args understand?
Parse_args () This method returns two values, options and args, which are objects and lists, which include all the options defined using the Parser.add_option () method, such as '-w '.
options.words is the option to store '-W ', whose value is true or false, such as when the script is followed by the-w option, then the Options.words value is true.
below this output under Ipython, since no option is defined with add_option (), there is no value for the option in the output of options.
This is the Python's own module, want to know exactly how it is implemented inside, source files in this position,/usr/lib64/python2.6/optparse.py.
This is defined in the script: Dest = "Characters",
The following should be judged: if options.characters, not if options.chars
What is the use of dest and action? See help doesn't seem to mention it?
The reference to the option in the code requires dest the name defined later, such as the-C option, Using options.characters, each option requires dest to define a name, which is the name of the option, which is used to refer to this option in the program, for example: if not (Options.characters or options.words or options.lines): These options are referenced in parentheses.
Some of the options behind the command is a letter, some not only have letters, but also the value behind, compare the following two commands:
wc-l/etc/passwd
tail-n 20/etc/passwd
The- L and-n are options, but the behavior is not the same, there is no value after-L,-n is followed by a value, then the option with no value is the action, if action= "Store_true", then there is no value after the description option, if action= ' store ', A value is required after the description option.
when the script is followed by-H, you can see the contents of the help definition.
What does default do, and what does it say for false and true?
take the-C option to give an example,
default If True, indicates that if you do not add the-C option after the script, it also has the-C behavior.
when default is false, it means that the script is not followed by the-C option, there is no behavior of-C, such as Wc-l/etc/hosts, without the-c option, it does not count the characters, only the number of rows statistics
This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1862613
Python Learning Notes 14-optparse real command-line arguments