Python---optparse module

Source: Internet
Author: User

Then a log,

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[' ', '-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.