This article mainly introduces the Python getopt detailed and simple examples of relevant information, the need for friends can refer to the following
Python getopt detailed
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 = Get Opt.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)) Except Getopt. Getopterror, Err:usage (docstring) print "* *", str (ERR), "* *" Sys.exit (2) path_specified = False for O, a in op Ts:if o in ("-H", "--help"): Usage (docstring) sys.exit () Elif O in ("-V", "--verbose"): Options.verb OSE = True elif o in ("-P", "--path"): Options.search_path = a elif o in ("--signapk_path",): Options.sign Apk_path = a elif o in ("--extra_singapk_args",): Options.extra_signapk_args = Shlex.split (a) elif O in ("--jav A_path ",): Options.java_path = a else:if Extra_option_handler is None or not Extra_option_handler (O, a): Assert False, "uNknown option \ "%s\" "% (o,) os.environ[" PATH "] = (Os.path.join (Options.search_path," bin ") + os.pathsep + os.environ[" PA TH "]) return args
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): if O in ("-B", "--board_config"): pass # deprecated elif o in ("-K", "--package _key "): Options.package_key = a elif o in ("-I ","--incremental_from "): Options.incremental_source = a elif O In ("-w", "--wipe_user_data"): Options.wipe_user_data = True elif o in ("-N", "--no_prereq"): Options.omit_prereq = True elif o in ("-E", " --extra_script "): options.extra_script = a elif o in ("-a ","--aslr_mode "): If A in (' On '," on "," true "," T Rue "," Yes "," yes "): Options.aslr_mode = True else: options.aslr_mode = False elif o in ("--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 ']
Thank you for reading, hope to help everyone, thank you for the support of this site!