Skip to content

Instantly share code, notes, and snippets.

@rberaldo
Created September 11, 2012 03:38
Show Gist options
  • Save rberaldo/3695786 to your computer and use it in GitHub Desktop.
Save rberaldo/3695786 to your computer and use it in GitHub Desktop.
Ensaio para um gerenciador de coleções
#!/usr/bin/python
import os # To check if database.txt exists
import argparse # Happily handles arguments and options
# My lovely arguments (working on them)
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--author", help="the author of the book", action="store", required=True)
parser.add_argument("-t", "--title", help="the title of the book", action="store", required=True)
parser.add_argument("-y", "--year", help="the year the book was published", action="store", required=True)
parser.add_argument("-p", "--publisher", help="the company that published the book", action="store", required=True)
parser.add_argument("-e", "--edition", help="the edition of the book", action="store")
parser.add_argument("-i", "--isbn", help="the ISBN number of the book", action="store")
parser.add_argument("--translator", help="the translator of the book", action="store")
args = parser.parse_args()
# This will create the database.
def firstBook():
outFile = open('database.txt', 'wt')
outFile.write("[book]\n")
outFile.write("title: " + args.title + "\n")
outFile.write("author: " + args.author + "\n")
outFile.write("publisher: " + args.publisher + "\n")
outFile.write("year: " + args.year + "\n")
outFile.write("\n")
print("Creating database... Adding book... Done!")
outFile.close()
# This will append to the database.
def addBook():
outFile = open('database.txt', 'at')
outFile.write("[book]\n")
outFile.write("title: " + args.title + "\n")
outFile.write("author: " + args.author + "\n")
outFile.write("publisher: " + args.publisher + "\n")
outFile.write("year: " + args.year + "\n")
outFile.write("\n")
print("Adding book... Done!")
outFile.close()
print("Welcome to Shellf!")
# Let's check whether database.txt exists:
if os.path.exists('database.txt'):
addBook()
else:
firstBook()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment