Skip to content

Instantly share code, notes, and snippets.

@iamn3
Last active June 19, 2018 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iamn3/cc708d7b3521b5fc9cd3112537c09884 to your computer and use it in GitHub Desktop.
Save iamn3/cc708d7b3521b5fc9cd3112537c09884 to your computer and use it in GitHub Desktop.
CLI todo list. Python3 tutorial
# ToDoリストを登録する変数
todo_list = []
# エントリーポイント
def start_repl():
print("簡単なToDoプログラム")
while True:
print("todo>> ",end='') # endオプションにから文字を入れると改行せずに出力
commands = input().split(' ')
if commands[0] == "bye":
print("バイバイ!")
break
elif commands[0] == "add" and len(commands) >= 2:
add_todo(commands[1])
elif commands[0] == "delete" and len(commands) >= 2:
delete_todo(commands[1])
elif commands[0] == "done" and len(commands) >= 2:
done_todo(commands[1])
elif commands[0] == "list":
if len(commands) >= 2 and commands[1] == "all":
show_todo(True)
else:
show_todo()
elif commands[0] == "help":
show_help()
elif len(commands) > 0:
print("コマンドが見つかりません。({})".format(commands[0]))
return
# データの登録処理
def add_todo(text):
todo_list.append(todo_data(text=text, done=False))
# 登録データをディクショナリで作成する
def todo_data(text='', done=False):
return {"text":text,"done":done}
# noで指定されたToDoを削除する
def delete_todo(no):
todo_list.pop(int(no))
# noで指定されたToDoをdoneにする
def done_todo(no):
todo_list[int(no)]["done"] = True
# 一覧表示する
# 基本的にdoneな項目は表示しない
# "list all"コマンドでdoneな項目も表示
def show_todo(show_all=False):
idx = 0
for todo in todo_list:
if show_all or not show_all and not todo["done"]:
show_todo_format(idx,todo)
idx += 1
# ToDoの表示フォーマットを決める
def show_todo_format(no, todo):
print("No:{0} Text:{1} Done:{2}".format(no, todo["text"], str(todo["done"])))
# ヘルプの表示
def show_help():
message = """
使い方
1. 登録する
add [ToDoの内容]
2. 削除する
delete [list表示時のNo]
3. ToDoに終了フラグを設定する
done [list表示時のNo]
4. 一覧表示
list (省略可: [all])
キーワード「all」をつけて終了したToDoも表示
5. ヘルプの表示
help
6. 終了
bye
"""
print(message)
if __name__ == "__main__":
# execute only if run as ascript
start_repl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment