Implement a simple command-line interface in Python
Submit in your repo in the file individual/cli.py
This is an INDIVIDUAL assignment. (Work on it on your own.)
Goals
- Practice designing a usable command-line interface.
- Practice extracting information programmatically from your chosen dataset
(using, probably, python's
csvmodule) - Practice using python's
argparsemodule.
Rubric
2 - files are named and located as specified
1 - top-of-file comment contains the right stuff
2 - interface description (NAME/SYNOPSIS/DESCRIPTION) quality
2 - argparse used to correctly implement the proposed CLI interface
3 - implementation correctness and quality
What to do
- Choose a question you would like to ask of your dataset. Make sure the question requires at least one parameter. For example, don't make the question "show me all the books" or "show me all the authors". Rather, make it something like "show me all the books written by author X" or "show me all the authors born between years Y and Z".
- In your individual git repo, create a directory named
individual. You'll put all your individual coding and design assignments here for the rest of the term. - Create a python program called
cli.pyin theindividualdirectory. Design a command-line interface for your question, and express it like in a
manpage. This is likely to be pretty simple. My first example above, for example, could be supported by a design like this:NAME: cli.py - command-line interface exercise SYNOPSIS: python3 cli.py booksby authorname DESCRIPTION: Shows a list of the titles of all the books written by any author whose name contains the specified authorname string, case-insensitively..That is, try to describe what your interface causes to happen briefly, but pretty precisely.
- In a comment at the top of
cli.py, write the name of the program, your name, and the date. Follow that with your NAME/SYNOPSIS/DESCRIPTION. - Implement the design of your command-line interface using python's
argparsemodule. (Note: don't use third-party modules like pandas or numpy for this. Interact with your data file via either python's csv or json module, depending on the format of your raw data.) - Test
python3 cli.py -hto make sure you see a sensible usage statement provided byargparse. Make sure that copies of your team's chosen dataset are in a folder named
datain your repo.- Commit and push your changes to GitHub. That is, do a suitable sequence
of
git add,git status,git commit,git pull, andgit pushto ensure that your changes are saved and included in the central copy of your repo that's stored on github.com.
Advice
- Keep it simple. (If you definitely have the time and desire to mess around implementing something complicated, go wild. It won't earn you extra points, but you might learn something cool.)
You may hard-code the path of your data file in
cli.py. For example, you might include code that looks like this:import csv ... with open('../data/books.csv') as f: reader = csv.reader(f) for book_row in reader: ...- My code samples contain all the techniques you need, so make sure you read and understand them. Feel free to plunder chunks of code from the samples.
- Stuck? Try stuff and check the internet, of course. But don't stay stuck for long. Our Slack is there to provide you a community of helpers.
- Have fun!