RosettaCodeData/Task/Simple-database/11l/simple-database.11l

80 lines
1.7 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T Item
String name, date, category
F (name, date, category)
.name = name
.date = date
.category = category
F String()
R .name, (.date), (.category)
V db_filename = simdb.csv
F load()
[Item] db
L(line) File(:db_filename).read().rtrim("\n").split("\n")
V item = line.split(, )
db.append(Item(item[0], item[1], item[2]))
R db
F store(item)
File(:db_filename, APPEND).write(String(item)"\n")
F printUsage()
print(|
Usage:
simdb cmd [categoryName]
add add item, followed by optional category
latest print last added item(s), followed by optional category
all print all
For instance: add "some item name" "some category name)
F addItem(args)
I args.len < 2
printUsage()
R
V date = Time().strftime(%Y-%m-%d %H:%M:%S)
V cat = I args.len == 3 {args[2]} E none
store(Item(args[1], date, cat))
F printLatest(a)
V db = load()
I db.empty
print(No entries in database.)
R
I a.len == 2
L(item) reversed(db)
I item.category == a[1]
print(item)
L.break
L.was_no_break
print(There are no items for category 'a[1]')
E
print(db.last)
F printAll()
V db = load()
I db.empty
print(No entries in database.)
R
L(item) db
print(item)
:start:
I :argv.len C 2..4
S :argv[1].lowercase()
add
addItem(:argv[1..])
latest
printLatest(:argv[1..])
all
printAll()
E
printUsage()
E
printUsage()