Skip to content

Commit

Permalink
Rewrite commands(no use of argv/argc now)
Browse files Browse the repository at this point in the history
Add help information
Michael1015198808 committed Dec 20, 2019

Verified

This commit was signed with the committer’s verified signature.
paescuj Pascal Jufer
1 parent 952b94c commit 612b4b9
Showing 4 changed files with 69 additions and 89 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Add help information to commands.
74 changes: 27 additions & 47 deletions commands.py
Original file line number Diff line number Diff line change
@@ -10,77 +10,55 @@ def display(cmd,effe,cmdlen=15,indent=2):
print(" ",end="")
print(effe)

def know():
if len(argv)>3:
print("Too many arguments!",file=stderr)
def know(namespace):
words=read()
if len(argv)==3:
find=-1
if namespace.eng:
idx=-1
for i in range(0,len(words)):
if argv[2]==words[i][0]:
find=i
if namespace.eng==words[i][0]:
idx=i
break
if find==-1:
print(argv[2]+" not found!",file=stderr)
else:
print(namespace.eng+" not found!",file=stderr)
return
i=find
else:
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
i=json.load(f)
idx=json.load(f)
except:
print("Can't fine the last word",file=stderr)
exit()
words[i][2]*=0.9
words[i][2]+=0.1
words[idx][2]*=0.9
words[idx][2]+=0.1
write(words)


def add():
def add(namespace):
words=read()
for word in words:
if argv[2]==word[0]:
print(argv[2]+" exists!")
if namespace.eng==word[0]:
print(namespace.eng+" exists!")
return
if len(argv)<4:
print("vocab add <en> <ch>!",file=stderr)
return
words.append([argv[2],argv[3],0,1])
words.append([namespace.eng,namespace.chi,0,1])
write(words)
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"w") as f:
json.dump((-1),f)
except FileNotFoundError:
pass

def version():
print("version: beta")

def help():
print("Usage: vocab <command>")
print("Commands:")
display("word", "Get one word(Default)")
display("show", "Print all words on the vocabulary")
display("add <en> <ch>","Add a new word")
display("init","Do initialization")
display("know","Decrease the probability of this word")
display("rm <en>|<ch>","Remove a word from list(Default: the last word)")
print("Options")
display("--version","Display version information")
display("--help","Display help information")
print(r"To load a existed word list, copy it to ~/.vocab/data.json")
print('''Format:
[["intern", "扣押,拘留", 0, 1], ["sore", "\u75bc\u75db\u7684", 0, 1]]
(English,Chinese,two numbers for further consideration)
(The order of English and Chinese can be swaped)''')
def version(namespace):
import version
print("version: alpha %d.%d.%d"% (version.major, version.minor, version.revision))
print("last update", version.date)

def show():
def show(namespace):
words=read()
for word in words:
display(word[0],word[1],indent=0)
print(str(len(words))+" words in total!")

def get_word():
def get_word(namespace):
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
i=json.load(f)
@@ -103,7 +81,7 @@ def get_word():
except FileNotFoundError:
pass

def init():
def init(namespace):
print("Are you sure you want to initialize? ALL files will be cleared unless you backup them manually")
print("""Input "Yes, I know exactly what I'm doing" to continue""")
if input()=="Yes, I know exactly what I'm doing":
@@ -112,15 +90,17 @@ def init():
else:
print("Initialization canceled")

def remove():
def remove(namespace):
print(namespace)
return
words=read()
if len(argv)==3:
if namespace.word:
for i in range(0,len(words)):
if words[i][0]==argv[2] or words[i][1]==argv[2]:
if words[i][0]==namespace.word or words[i][1]==namespace.word:
del words[i]
write(words)
return
print(argv[2]+" not found!")
print(namespace.word+" not found!")
else:
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
79 changes: 40 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
@@ -3,53 +3,54 @@
import argparse

def main():
epilog='''
To load a existed word list, copy it to ~/.vocab/data.json
Format:
[["intern", "扣押,拘留", 0, 1], ["sore", "\u75bc\u75db\u7684", 0, 1]]
(English,Chinese,two numbers for further consideration)
(The order of English and Chinese can be swaped)
bug/issue report to https://github.com/Michael1015198808/zsh-vocab
'''

parser = argparse.ArgumentParser(
description='a terminal word reciting program/plugin',
epilog="bug/issue report to https://github.com/Michael1015198808/zsh-vocab")
description='zsh-vocab : a terminal word reciting program/plugin',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=epilog)
parser.set_defaults(func=cm.get_word)
#parser.add_argument("--version", action="version", version="0")
subparsers = parser.add_subparsers(help='sub-command help')

subparsers.add_parser('--version', help='').set_defaults(func=cm.version)
subparsers.add_parser('show', help='').set_defaults(func=cm.show)
subparsers.add_parser('know', help='').set_defaults(func=cm.know)
subparsers.add_parser('add', help='').set_defaults(func=cm.add)
subparsers.add_parser('word', help='').set_defaults(func=cm.get_word)
subparsers.add_parser('remove', help='').set_defaults(func=cm.remove)
version_help = 'print current version'
init_help = 'do initialization'
show_help = 'print all words in wordlist'
know_help = 'mark last word as known'
add_help = 'add a new word'
word_help = 'randomly print a word'
remove_help = 'remove a word'

subparsers.add_parser('version', help=version_help).set_defaults(func=cm.version)
subparsers.add_parser('init', help=version_help).set_defaults(func=cm.init)
subparsers.add_parser('show', help=show_help).set_defaults(func=cm.show)
subparsers.add_parser('word', help=word_help).set_defaults(func=cm.get_word)

parser_know = subparsers.add_parser('know', help=know_help)
parser_know.add_argument('eng', nargs='?',
help='the word to mark as known')
parser_know.set_defaults(func=cm.know)

parser_add = subparsers.add_parser('add', help=add_help)
parser_add.add_argument('eng', help='English of the word')
parser_add.add_argument('chi', help='Chinese of the word')
parser_add.set_defaults(func=cm.add)

parser_remove = subparsers.add_parser('remove', help=remove_help)
parser_remove.add_argument('word', nargs='?',
help='the word to be removed. Both language is fine.\nempty to remove the last word')
parser_remove.set_defaults(func=cm.remove)

args = parser.parse_args()
if hasattr(args, "func"):
args.func()
exit()
try:
os.mkdir(os.path.expanduser("~/.vocab"))
except FileExistsError:
pass

try:
with open(os.path.expanduser("~/.vocab/data.json"),"r") as f:
try:
words=json.load(f)
except json.decoder.JSONDecodeError:
words=[]
except FileNotFoundError:
print(os.path.expanduser("~/.vocab/data.json"+" is not found."),file=stderr)
print("call "+argv[0]+" help for more information",file=stderr)
exit()

if len(argv)==1:
cm.get_word()
else:
try:
handler=cm.handler_dict[argv[1]]
handler()
except KeyError:
print("Unknown command "+argv[1],file=stderr)
print("Use --help to use more information",file=stderr)
pass
exit()

pass
args.func(args)

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
major = 0
minor = 1
revision = 1
date = " Thu 19 Dec 2019 11:21:17 AM PST "
revision = 3
date = " Fri 20 Dec 2019 04:06:58 AM PST "

0 comments on commit 612b4b9

Please sign in to comment.