Skip to content

Instantly share code, notes, and snippets.

@bxb100
Last active March 24, 2017 06:54
Show Gist options
  • Save bxb100/d3e746fc8cc4d05939aac00c4dcef39c to your computer and use it in GitHub Desktop.
Save bxb100/d3e746fc8cc4d05939aac00c4dcef39c to your computer and use it in GitHub Desktop.
我们需要实现一个简单的服务器管理工具(命令行工具),该工具只需要支持以下功能:添加服务器(add 命令):支持以下参数 -h hostip,-u user,-p password列出服务器(list 命令):没有参数,列出当前存储的所有服务器信息 删除服务器(delete):支持参数 -h hostip。成功的话无输出。
import click
import os
@click.group()
def cli():
pass
@cli.command()
@click.option('-h', help='host ip')
@click.option('-u', help='user')
@click.option('-p', help='password')
def add(h, u, p):
if not h or not u or not p:
click.echo('ERROR')
return False
string = '%s %s %s \n' % (h, u, p)
if is_in_file(string):
with open('store.txt', 'a') as f:
f.write(string)
f.close()
@cli.command()
@click.option('-h', help='host ip')
def delete(h):
with open('store.txt', 'w+') as f:
a_t = f.readlines()
for i in a_t:
if h in i:
a_t.remove(i)
f.writelines(a_t)
f.close()
@cli.command()
def list():
with open('store.txt', 'r') as f:
for i in f.readlines():
click.echo(i)
def is_in_file(hup):
with open('store.txt', 'r') as f:
if hup in f.read():
click.echo('ERROR')
return False
f.close()
return True
if __name__ == '__main__':
if not os.path.isfile('store.txt'):
open('store.txt', 'w').close()
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment