python 写命令行程序

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

python 写命令行程序

tairan wang
打算用一个python写一个命令行的程序,不知道处理的参数的逻辑如何设计。请给些提示!:)

Thanks


Express yourself instantly with MSN Messenger! MSN Messenger
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python 写命令行程序

Jiahua Huang
python 标准库里有 getopt 模块,
去看 pydoc getopt  吧

2008/1/16 tairan wang <[hidden email]>:
>
> 打算用一个python写一个命令行的程序,不知道处理的参数的逻辑如何设计。请给些提示!:)
>
> Thanks
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python 写命令行程序

Lich_Ray
In reply to this post by tairan wang
参考Unix经典程序,比如 ls 。

在08-1-16,tairan wang <[hidden email]> 写道:
打算用一个python写一个命令行的程序,不知道处理的参数的逻辑如何设计。请给些提示!:)

Thanks


Express yourself instantly with MSN Messenger! <a href="http://clk.atdmt.com/AVE/go/onm00200471ave/direct/01/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> MSN Messenger

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
Ray Stinger, nickname Lich_Ray
God is in his heaven, all's right with the world.
-------------------------------------------------
let focus = 'computing' in where:
http://lichray.javaeye.com
let focus = 'computing' in here:
http://lichray.bokeland.com
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python 写命令行程序

Jiahua Huang
In reply to this post by tairan wang
你是说设计参数?

不如首先看 python 怎么处理参数的

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
[ 多个短参数/--长参数]  [文件/命令]  [其他选项]

两杠开头的为长参数,类似
--encode utf8 或 --encode=utf8

单杠开头的为短参数,或长参数缩写,类似
-d utf8 或 -d=utf8

第一个非 - 开头的为文件/命令

文件/命令后的 是其他参数



贴个实例:
$ zshelve
commandline tool for zshelve databases

Usage: zshelve  FILE  dump                    Dump the data tree
       zshelve  FILE  keys                    List of keys
       zshelve  FILE  get          KEY        Dump value for key
       zshelve  FILE  set          KEY VALUE  Set db[key] = value
       zshelve  FILE  has_key      KEY        True if database has the key
       zshelve  FILE  search_key   KEY        Search key
       zshelve  FILE  search_value VALUE      Search value

2008/1/16 tairan wang <[hidden email]>:
>
> 打算用一个python写一个命令行的程序,不知道处理的参数的逻辑如何设计。请给些提示!:)
>
> Thanks
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python 写命令行程序

HG-11
In reply to this post by tairan wang
copy一段我的,
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hs:t:b:", ["help",
"site=","build=","type="])
        #支持长选项和短选项两个方式,之所短的: "hs:t:b:"表示-h是开关类型的选项,-s,-t,-t是需要提供额外的值。
       #比如: cmd.py -s www.google.com -t homepage -b other_args 或者
       #cmd.py -h 或者 cmd.py --help
       #opts包含(opt,value)这样的选项和值
      #args包含参数列表
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--output"):
            output = a
        elif o in ("-s","--site") :
            site_alias = a
        elif o in ("-t","--type"):
            build_type = a
        elif o in ("-b","--build"):
            build_number = a
        else:
            assert False, "unhandled option"

        # ...
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

RE: python 写命令行程序

tairan wang
收到!

Thanks




> Date: Thu, 17 Jan 2008 12:27:34 +0800

> From: [hidden email]
> To: [hidden email]
> Subject: Re: [python-chinese] python 写命令行程序
>
> copy一段我的,
> try:
> opts, args = getopt.getopt(sys.argv[1:], "hs:t:b:", ["help",
> "site=","build=","type="])
> #支持长选项和短选项两个方式,之所短的: "hs:t:b:"表示-h是开关类型的选项,-s,-t,-t是需要提供额外的值。
> #比如: cmd.py -s www.google.com -t homepage -b other_args 或者
> #cmd.py -h 或者 cmd.py --help
> #opts包含(opt,value)这样的选项和值
> #args包含参数列表
> except getopt.GetoptError, err:
> # print help information and exit:
> print str(err) # will print something like "option -a not recognized"
> usage()
> sys.exit(2)
> for o, a in opts:
> if o in ("-h", "--help"):
> usage()
> sys.exit()
> elif o in ("-o", "--output"):
> output = a
> elif o in ("-s","--site") :
> site_alias = a
> elif o in ("-t","--type"):
> build_type = a
> elif o in ("-b","--build"):
> build_number = a
> else:
> assert False, "unhandled option"
>
> # ...
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


Express yourself instantly with MSN Messenger! MSN Messenger
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python 写命令行程序

shhgs
难道没人知道optparse吗?



2008/1/17 tairan wang <[hidden email]>:

>
> 收到!
>
> Thanks
>
>
>
> ________________________________
> > Date: Thu, 17 Jan 2008 12:27:34 +0800
> > From: [hidden email]
> > To: [hidden email]
> > Subject: Re: [python-chinese] python 写命令行程序
>
>
> >
> > copy一段我的,
> > try:
> > opts, args = getopt.getopt(sys.argv[1:], "hs:t:b:", ["help",
> > "site=","build=","type="])
> > #支持长选项和短选项两个方式,之所短的: "hs:t:b:"表示-h是开关类型的选项,-s,-t,-t是需要提供额外的值。
> > #比如: cmd.py -s www.google.com -t homepage -b other_args 或者
> > #cmd.py -h 或者 cmd.py --help
> > #opts包含(opt,value)这样的选项和值
> > #args包含参数列表
> > except getopt.GetoptError, err:
> > # print help information and exit:
> > print str(err) # will print something like "option -a not recognized"
> > usage()
> > sys.exit(2)
> > for o, a in opts:
> > if o in ("-h", "--help"):
> > usage()
> > sys.exit()
> > elif o in ("-o", "--output"):
> > output = a
> > elif o in ("-s","--site") :
> > site_alias = a
> > elif o in ("-t","--type"):
> > build_type = a
> > elif o in ("-b","--build"):
> > build_number = a
> > else:
> > assert False, "unhandled option"
> >
> > # ...
> > _______________________________________________
> > python-chinese
> > Post: send [hidden email]
> > Subscribe: send subscribe to [hidden email]
> > Unsubscribe: send unsubscribe to [hidden email]
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
> ________________________________
> Express yourself instantly with MSN Messenger! MSN Messenger
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese