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

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

  利用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

执行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

  

posted @   兜兜有糖的博客  阅读(259)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示