常用连链接命令行存储小工具

一、工作中经常使用到一些链接,写了个脚本,可以在控制台进行命令行添加、删除、和打印

  利用python的optionparser模块进行解析命令:

  

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-7-4 15:40:03
# @Author  : 兜兜有糖
# @QQ      : 271353531
# @github  : https://github.com/liqiushui

import copy
import json
from optparse import OptionParser

class UrlList:
    def __init__(self):
        self.list = []
        self.reload()

    def reload(self):
        try:
            with open('data.json', 'r') as f:
                data = json.load(f)
                if data and len(data) > 0:
                    self.list = data
        except Exception:
            print('not found data.json')


    def resave(self):
        try:
            with open('data.json', 'w') as f:
                json.dump(self.list, f)
        except Exception:
            print('can not save data.json')


    def add(self, url):
        if not url in self.list:
            self.list.append(url)
            self.resave()

    def remove(self, url):
        if url in self.list:
            self.list.remove(url)
            self.resave()
    
    def list_all(self):
        return copy.copy(self.list)

    


def main():
    parser = OptionParser()  
    parser.add_option("-a","--add", action="store", dest="url", default=None, help="add url to common url list")
    parser.add_option("-r","--remove", action="store", dest="remove_url", default=None, help="remove url in common url list")
    parser.add_option("-l","--list", action="store_true", dest="list_url", default=False, help="list all urls")
    (options, args) = parser.parse_args()
    url_list = UrlList()

    if options.url:
        url_list.add(options.url)
    if options.remove_url:
        url_list.remove(options.remove_url)
    if options.list_url:
        for url_item in url_list.list_all():
            print(url_item)


if __name__ == "__main__":  
    main()  

然后使用alias 软链接指令,放入启动执行脚本中

LUNLI-MC1:workscript lunli$ vim ~/.bash_profile
LUNLI-MC1:workscript lunli$ source  ~/.bash_profile

执行效果:

LUNLI-MC1:workscript lunli$
LUNLI-MC1:workscript lunli$ ww -l
LUNLI-MC1:workscript lunli$ ww -a http://www.qq.com
LUNLI-MC1:workscript lunli$ ww -a http://www.baidu.com
LUNLI-MC1:workscript lunli$ ww -a http://www.google.com
LUNLI-MC1:workscript lunli$ ww -l
http://www.qq.com
http://www.baidu.com
http://www.google.com
LUNLI-MC1:workscript lunli$ ww -r http://www.qq.com
LUNLI-MC1:workscript lunli$ ww -l
http://www.baidu.com
http://www.google.com

  

猜你喜欢

转载自www.cnblogs.com/doudouyoutang/p/9263985.html