Write the program without the interface, the most annoying is the parameter resolution problem, especially a lot of parameters, the following is a small demo, come out to share with you:
1 #-*-Coding:utf8-*-2 ImportOS3 Importdatetime4 ImportSYS5 fromOptparseImportOptionparser6 7 8 defGet_user_paras ():9 Try:Tenopt =Optionparser () OneOpt.add_option ('--host_ip', Adest='host_ip', -Type=str, -help='The IP of the check host') theOpt.add_option ('--run', -action="store_true", -dest="Is_run", -default=False, +help="Run the Scripts") -Opt.add_option ('--view', +action="Store_false", Adest="Is_run", atdefault=False, -help="Only view and not run the scripts") -Opt.add_option ('--show_type', -dest="Show_type", -Type=int, -default=0, inhelp="0 or 1, 0 only show the simple data, and 1 show the full data") -(options, args) =Opt.parse_args () toIs_valid_paras =True +Error_messages = [] -HOST_IP =options.host_ip theIs_run =Options.is_run *Show_type =Options.show_type $ if nothost_ip:Panax NotoginsengError_messages.append ("host_ip must be set;") -Is_valid_paras =False the ifShow_type not inch[0, 1]: +Error_messages.append ("Show_type only can is 0 or 1;") AIs_valid_paras =False the + ifIs_valid_paras: -User_paras = {"host_ip": Host_ip,"Is_run": Is_run,"Show_type": Show_type} $ returnUser_paras $ Else: - forError_messageincherror_messages: - Print(error_message) the returnNone - exceptException as ex:Wuyi Print("exception: {0}". Format (str (ex))) the returnNone - Wu - defMain (): AboutUser_paras =Get_user_paras () $ ifUser_paras isNone: - sys.exit (0) -info ="host_ip:{0}, Is_run:{1}, show_type:{2}" -info = Info.format (user_paras["host_ip"], Auser_paras["Is_run"], +user_paras["Show_type"]) the Print(Info) - $ the if __name__=='__main__': theMain ()
When using Optionparser, the--help and-H parameters are automatically added, and parameter help is automatically generated, such as:
For code:
Opt.add_option ('--run', action="store_true" , dest="is_run", default=False, Help ="run the scripts")
--run indicates the name of the parameter
The action indicates how the value of the parameter is handled, often with Store/store_true/store_false,store, which is literal, and store_true is passed to the parameter as a parameter value, store_ False to pass false as the parameter value to the parameter
Dest represents the parameter name after the command-line argument is parsed.
In the code above,--run is passed in as a command-line argument because the action is store_true, so the parameter value is true, the parsed parameter is named Is_run, and after (options, args) = Opt.parse_args () is assigned, You can use Options.is_run to ask for parameter values.
More help: Https://docs.python.org/2/library/optparse.html
##========================================================##
For more parameters or large parameter values, individuals prefer to use parameter profiles for simple and easy editing, such as creating a run_config.py file:
# -*-Coding:utf8-*- # Run config class Runconfig (object): = True = 1 "192.167.1.1" " " " Select *from tb001where ID >1000and c1<300 "" "
Then, in other files, access:
#-*-Coding:utf8-*- fromRun_configImportRunconfigdefMain ():Print("is_run:{0}, Host_ip:{1}". Format (RUNCONFIG.IS_RUN,RUNCONFIG.HOST_IP))Print("run_scripts:{0}". Format (runconfig.run_scripts))if __name__=='__main__': Main ()
Simple rough, not so troublesome, the practice of the soil leopard ha!
##===================================================##
python--command-line parameter parsing demo