Python optparse模块

#!/usr/bin/python3.8
# -*- coding:utf-8 -*-
# __author__ = 'Qiushi Huang'

import optparse  # sys.argv的功能类似


# python ftp_client.py -h 192.168.22.33 -p 8091
# user:
# password
# https://www.cnblogs.com/xiugeng/p/8821302.html
# optparse 不再更新,使用 argparse 代替
# 其它解析模块:getopt


class FTPClient(object):
    """ftp客户端"""

    def __init__(self):
        usage = "usage: %prog [options] arg1 arg2"
        parser = optparse.OptionParser(usage)
        parser.add_option("-s", "--server", dest="server", help="ftp server ip_addr", metavar="SERVER")
        parser.add_option("-P", "--port", type="int", dest="port", help="ftp server port", metavar="PORT")
        parser.add_option("-u", "--username", dest="username", help="username info", metavar="USERNAME")
        parser.add_option("-p", "--password", dest="password", help="password info", metavar="PASSWORD")
        self.options, self.args = parser.parse_args()
        if self.args:
            print('参数不存在,使用testpy.py -h查看帮助')
        print(self.options, self.args)


if __name__ == '__main__':
    client = FTPClient()  # 实例化

 

posted @ 2023-08-18 09:12  風£飛  阅读(16)  评论(0编辑  收藏  举报