Python getopt use

Source: Internet
Author: User

getopt

Function Prototypes:

getopt.getopt(args, shortopts, longopts=[])

Parameter explanation:

    • Args:args is a list of parameters that need to be parsed. General use Sys.argv[1:], this can filter out the first parameter (PS: The first parameter is the name of the script, it should not be parsed as a parameter)
    • Shortopts: Shorthand parameter list
    • Longopts: Long parameter list

return value:

    • OPTs: The parsed (option, value) list pair.
    • Args: The list of remaining command-line arguments that are not part of the format information.
Source Analysis

In the build system of the Android generation Ota, the Parseoptions function in the common.py file is used to parse the input parameters, so we can analyze the use of getopt by the implementation of the function.

The function source code is as follows:

 def parseoptions(argv, docstring, extra_opts="", extra_long_opts=(), Extra_option_handler=none):    Try: opts, args = Getopt.getopt (argv,"Hvp:s:x"+ Extra_opts, ["Help","Verbose","Path=","Signapk_path=","extra_signapk_args=","Java_path=","public_key_suffix=","private_key_suffix=","device_specific=","Extra="] + list (extra_long_opts))exceptGetopt. Getopterror, Err:usage (docstring)Print "**", str (ERR),"**"Sys.exit (2) path_specified =False     forO, ainchOPTsifOinch("-H","--help"): Usage (docstring) sys.exit ()elifOinch("-V","--verbose"): Options.verbose =True        elifOinch("-P","--path"): Options.search_path = aelifOinch("--signapk_path",): Options.signapk_path = aelifOinch("--extra_singapk_args",): Options.extra_signapk_args = Shlex.split (a)elifOinch("--java_path",): Options.java_path = aElse:ifExtra_option_handler is None or  notExtra_option_handler (O, a):assert False,"unknown option \"%s\ ""% (o,) os.environ["PATH"] = (Os.path.join (Options.search_path,"Bin") + Os.pathsep + os.environ["PATH"])returnArgs

Where extra_option_handler can be understood as a function pointer, its function is also to parse OPTs's key-value pairs.
Extra_option_handler source code is as follows:

   def option_handler(O, a):    ifOinch("-B","--board_config"):Pass   # deprecated    elifOinch("-K","--package_key"): Options.package_key = aelifOinch("-I.","--incremental_from"): Options.incremental_source = aelifOinch("-W","--wipe_user_data"): Options.wipe_user_data =True    elifOinch("-N","--no_prereq"): Options.omit_prereq =True    elifOinch("-E","--extra_script"): Options.extra_script = aelifOinch(" -a","--aslr_mode"):ifAinch("on","on","true","True","Yes","Yes"): Options.aslr_mode =True      Else: Options.aslr_mode =False    elifOinch("--worker_threads"): options.worker_threads = Int (a)Else:return False    return True

The parameters for the general generation of oat full-scale packages are argv as follows:

argv = [‘-v‘‘-p‘‘out/host/linux-xxx‘‘-k‘‘build/target/product/security/testkey‘‘out/target/product/xxx/obj/PACKAGING/target_files_intermediates/xxx-target_files.zip‘‘out/target/product/xxx/xxx_20150723.1340-ota.zip‘]

First, the parameters are analyzed, and the short parameters include:

-v,-p,-k,

After parsing, the resulting results are as follows:

opts = [(‘-v‘‘‘), (‘-p‘‘out/host/linux-x86‘), (‘-k‘‘build/target/product/security/testkey‘)]args =[‘out/target/product/xxx/obj/PACKAGING/target_files_intermediates/xxx-target_files.zip‘‘out/target/product/xxx/xxx_20150723.1340-ota.zip‘]

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python getopt use

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: info-contact@alibabacloud.com 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.