如何在此代码中添加更多的参数功能?
问题描述:
我发现了一个不错的小程序,可以在终端中快速记录。有一点缺乏功能 - 我无法读取我用它制作的笔记,也无法从存储它们的文件中清除笔记。我想修改这个程序,所以我可以运行它的参数来阅读我写在那里,也是干净的。我有一些想法如何做到这一点,但无法在代码中找到写入位置来粘贴我的行。我想带参数,如运行:-r -c如何在此代码中添加更多的参数功能?
原来的程序是这样的:
#!/usr/bin/env python3
import time
import os
import sys
# add the current local time to the entry header
lines = [ time.asctime() + '\n' + '--------------------\n' ]
if len(sys.argv) > 1:
lines.append(' '.join(sys.argv[ 1: ]))
lines[-1] += '\n'
else:
while 1:
try:
line = input()
except EOFError:
break
# get more user input until an empty line
if len(line) == 0:
break
else:
lines.append(line + '\n')
# only write the entry if the user entered something
if len(lines) > 1:
memoir_path = os.path.expanduser('~/.memoir')
# prepend a seperator only if the file exists (there are entries already in there)
if os.path.exists(memoir_path):
lines.insert(0, '--------------------\n')
with open(memoir_path, 'a') as f:
f.writelines(lines)
我的代码,我不知道在哪里粘贴(如果它是正确的):
# read memoir file
if str(sys.argv) == ("r"):
os.system('cat ~/.memoir')
# clear memoir file
if str(sys.argv) == ("c"):
os.system('> ~/.memoir')
编辑:
我已经作出一些改变,由于回答,一切工作正常,但我想使这个代码有点simplier。这段代码的作者为我添加了一些usles功能,用随机参数数量来运行这个程序,这些随机参数将在笔记中被“转换”为空行。我的更新似乎无法工作,所以我想摆脱这个功能。我认为它从第37行开始寻找#here!评论
新的代码看起来是这样的:
#!/usr/bin/env python3
import time
import os
import sys
def help():
print ("memoir is a minimal cli diary")
print ("run script with -r argument to read notes")
print ("run script with -c argument to clear notes file")
print ("run script with -h argument for help")
# add the current local time to the entry header
lines = [ time.asctime() + '\n' + '------------------------\n' + '\n' ]
if len(sys.argv) >= 2:
if sys.argv[1] == '-r':
# read .memoir file
os.system('cat ~/.memoir')
print('\n')
exit(0)
if sys.argv[1] == '-h':
# print help
help()
exit(0)
if sys.argv[1] == '-c':
# clear .memoir file
os.system('> ~/.memoir')
exit(0)
else:
print("invalid argument, type m -h for help")
exit(0)
if len(sys.argv) > 1 and len(sys.argv) != 2: #here!!!
lines.append(' '.join(sys.argv[ 1: ]))
lines[-1] += '\n'
else:
while 1:
try:
line = input()
except EOFError:
break
# get more user input until an empty line
if len(line) == 0:
break
else:
lines.append(line + '\n')
# only write the entry if the user entered something
if len(lines) > 1:
memoir_path = os.path.expanduser('~/.memoir')
# prepend a seperator only if the file exists (there are entries already in there)
if os.path.exists(memoir_path):
lines.insert(0, '\n------------------------\n')
with open(memoir_path, 'a') as f:
f.writelines(lines)
if len(sys.argv) >= 2:
# clear .memoir file
if sys.argv[1] == '-c':
os.system('> ~/.memoir')
答
首先,sys.argv
是在它的命令行参数数组。所以,你需要测试它的长度:
if len(sys.argv) >= 2 :
#sys.argv[0] is the name of your program
if sys.argv[1] == '-c' :
然后你就可以看到文件被写入最新的线路:
if len(lines) > 1:
memoir_path = os.path.expanduser('~/.memoir')
# prepend a seperator only if the file exists (there are entries already in there)
if os.path.exists(memoir_path):
lines.insert(0, '--------------------\n')
#here the file is opened with 'a' = append mode.
#If you want to override the content of the file (for your command 'clear'), use 'w' = write mode.
with open(memoir_path, 'a') as f:
f.writelines(lines)
所以,你可以在末尾的“清除”命令。 'read'命令将在程序开始时找到它的位置。
使用命令行参数作为'-arg1 -arg2'我建议看看'argparse'或'docopt'模块。 – albert